40
Engineering Technical college-Erbil Dep. of ISE Application Building Software Construction Lecture Slides #9: Multi threading S1 2014 Asst.Prof.Dr.Raghad Z.Yousif

Multithread msc lecture

Embed Size (px)

DESCRIPTION

msc lecture multithread - PowerPoint PPT Presentation

Citation preview

Engineering Technical college-ErbilDep. of ISE

Application Building Software Construction

Lecture Slides #9: Multi threading S1 2014

Asst.Prof.Dr.Raghad Z.Yousif

2

What is a Thread?

• A thread is a single sequential flow of control within a program.

• At any given time during the runtime of the thread, there is a single point of execution.

• However, a thread itself is not a program; a thread cannot run on its own. Rather, it runs within a program.

3

Multithreading

• Multiple threads can be executed at the same time within a single program. Such usage is called multi threading.

• Each thread may perform a different task in a single program

• Multiple threads create an illusion of several tasks being handled in parallel. Actually the processor allocates a fraction of its CPU cycle time to execute each thread.

4

Introduction: Where are Threads Used?

• Threads are used by virtually every computer user in the following instances:

– In Internet browsers

– In databases

– In operating systems (for controlling access to shared resources etc)

• Benefits of threads

– More productivity to the end user (such as responsive user interface)

– More efficient use of the computer (such as using the CPU while performing input-output)

– Sometimes advantageous to the programmer (such as simplifying program logic)

5

Creating Java Threads – Subclassing java.lang.Thread

• There are two ways to create threads in java. Assume you have a class “MyClass” which may contain several tasks to be executed as threads. One of the ways to create and execute a threaded application is as follows:

– “MyClass” should be declared to be a subclass of java.lang.Thread.

– “MyClass” should override the run method (public void run()) of class Thread.

– An instance of the “MyClass” can then be instantiated and executed as a thread using the start() method.

– Note that the code to be executed as thread should be in the run() method of “MyClass”.

– The following example shows how this can be done.

Asst.Prof.Dr.Raghad Z.Yousif

6

Asst.Prof.Dr.Raghad Z.Yousif

7

Asst.Prof.Dr.Raghad Z.Yousif

8

Asst.Prof.Dr.Raghad Z.Yousif

9

Asst.Prof.Dr.Raghad Z.Yousif

10

Asst.Prof.Dr.Raghad Z.Yousif11

Asst.Prof.Dr.Raghad Z.Yousif

12

Asst.Prof.Dr.Raghad Z.Yousif

13

14

Example 1

1. public class SimpleThread extends Thread {2. private String title;3. public SimpleThread(String str) {4. this.title = str;5. }6. 7. public void run() {8. for (int i = 0; i < 10; i++) {9. System.out.println(i + " " + title);10. try {11. sleep((long)(Math.random() * 1000));12. } catch (InterruptedException e) {}13. }14. System.out.println("DONE! " + getName());15. }16. 17. public static void main (String[] args) {18. new SimpleThread("First Thread").start();19. new SimpleThread("Second Thread").start();20. }21. }

15

Methods in java.lang.Thread

• The run() method contains the code which has to be executed as threads.

• The start() method is the method invoked in order for the threads to run in a multithreaded way.

• As soon as a call to start() is made, the thread is created and starts execution. Control returns back immediately to the next statement in the program while the thread may be in execution.

• Note that if a call new SimpleThread.run() is made, the run() method is executed in the current thread and the new thread is never started.

public static void main (String[] args) { new SimpleThread("First Thread").start(); new SimpleThread("Second Thread").start(); }

Thread is created and execution begins

………………..

………………

Control returns back to the next statement before completion of the current statement.

16

Methods in java.lang.Thread – Contd.

• The sleep() method (public static void sleep(long millis) throws InterruptedException) makes the current thread inactive for a specified number of milliseconds.

• The InterruptedException is thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt() method in class Thread.

• The getName() (public final String getName()) method returns back the name of the current thread as a String.

• Every thread has a name (including anonymous threads) for identification purposes

– More than one thread may have the same name

– If a name is not specified when a thread is created, a new name is generated.

17

Creating Java Threads – Implementing java.lang.Runnable

• Another way to create and execute Java threads is as follows. Assume you have a class “MyClass” which may contain several tasks to be executed as threads.

– “MyClass” should implement the interface java.lang.Runnable

– “MyClass” should implement the run method (public void run()) of the interface Runnable

– An instance of the Thread class can be created using an instance of “MyClass” as a parameter as follows:

Thread t = new Thread(new MyClass());

– Now the run() method of “MyClass” can be executed as a thread by invoking the start() method using t.start().

– Note that the code to be executed as thread should be in the run() method of MyClass.

– The following example shows how this can be done.

18

Example 2

1. import javax.swing.*;

2. public class TimeThread extends JFrame implements Runnable {3.4. private JTextField sec, min, hrs;5. private JPanel panel;6. private int time = 0;7. 8. public TimeThread()9. {10. super(“Time");11. 12. sec = new JTextField(3); sec.setEditable(false); //makes textfield uneditable13. min = new JTextField(3); min.setEditable(false);14. hrs = new JTextField(3); hrs.setEditable(false);15. 16. panel = new JPanel();17. 18. panel.add(hrs); panel.add(min); panel.add(sec);19. 20. getContentPane().add(panel); pack(); setVisible(true);21. }

19

Example – Contd.

22. public void run()23. {24. try {25. while(true) {26. Thread.sleep(1000); time++;27. sec.setText(time%60 + ""); //Display seconds28. min.setText((time – (time/3600)*3600)/60 + ""); //Display minutes29. hrs.setText(time/3600 + ""); //Display hours30. }31. } catch(InterruptedException e) {}32. }33. 34. public static void main(String[] args)35. {36. TimeThread t = new TimeThread();37. Thread mytime = new Thread(t);38. mytime.start();39. }40.}

20

Thread Priority

• Execution of multiple threads on a single CPU in some order is called scheduling.

• The Java runtime environment supports a very simple, deterministic scheduling algorithm called fixed-priority scheduling. This algorithm schedules threads on the basis of their priority relative to other Runnable threads.

• Thread priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY (constants defined in the Thread class).

• At any given time, when multiple threads are ready to be executed, the runtime system chooses for execution the Runnable thread that has the highest priority.

• If two threads of the same priority are waiting for the CPU, the scheduler arbitrarily chooses one of them to run.

21

Thread Priority

• The values of constants in java.lang.Thread are as follows:

• The methods int getPriority() and setPriority(int priority) are used the accessor and mutator methods used for the priority of a Java thread.

• The default priority value of a thread is 5 (java.lang.NORM_PRIORITY) or it is the priority of the thread that constructed it.

public static final int MAX_PRIORITY 10

public static final int MIN_PRIORITY 1

public static final int NORM_PRIORITY 5

22

Thread Priority – Example 1

1. public class SimplePThread extends Thread {2. 3. public SimplePThread(String str) {4. super(str); //Thread can be created using a string 5. } 6. public void run() {7. for (int i = 1; i < 400; i++) {8. for(int j = 0; j < 40000; j++)9. System.out.print(""); //Keeps thread busy10. System.out.print(getName()); 11. }12. System.out.print(".");13. } 14. public static void main (String[] args) {15. 16. Thread t1 = new SimplePThread("1");17. Thread t2 = new SimplePThread("2");18. 19. t1.setPriority(Thread.NORM_PRIORITY + 1);20. t2.setPriority(Thread.NORM_PRIORITY - 1);21. 22. System.out.println("Thread 1 has priority " + t1.getPriority());23. System.out.println("Thread 2 has priority " + t2.getPriority());24. 25. t1.start(); t2.start();26. }27. }

Asst.Prof.Dr.Raghad Z.Yousif

23

Asst.Prof.Dr.Raghad Z.Yousif

24

Asst.Prof.Dr.Raghad Z.Yousif

25

Asst.Prof.Dr.Raghad Z.Yousif

26

Asst.Prof.Dr.Raghad Z.Yousif

27

Asst.Prof.Dr.Raghad Z.Yousif

28

Asst.Prof.Dr.Raghad Z.Yousif

29

30

The interrupt() method

• The interrupt() method (public void interrupt()) interrupts the current thread. If thread is inactive, an InterruptedException is thrown which must be caught.

• The isInterrupted() method (public boolean isInterrupted()) checks if the current thread is interrupted or not.

• An example shows their use.

31

Example 2

1. import javax.swing.*;2. import java.awt.event.*;

3. public class TimeThread3 extends JFrame implements ActionListener {4.5. private JTextField sec, min, hrs; private JButton button; private JPanel panel;6. 7. private TimerTh t; private int time = 0;8. 9. public TimeThread3()10. {11. super("Time");12. 13. sec = new JTextField(3); sec.setEditable(false);14. min = new JTextField(3); min.setEditable(false);15. hrs = new JTextField(3); hrs.setEditable(false);16. 17. button = new JButton("Stop"); button.addActionListener(this);18. 19. panel = new JPanel(); panel.add(hrs); panel.add(min); panel.add(sec);panel.add(button);20. getContentPane().add(panel); pack(); setVisible(true);21. 22. t = new TimerTh(); t.start();23. }

32

Example 2 – Contd.

24. class TimerTh extends Thread //Inner Class25. { public void run()26. { try 27. {27. while(true)28. { Thread.sleep(1000); time++;29. sec.setText(time%60 + ""); //Display seconds30. min.setText((time - (time/3600)*3600)/60 + ""); //Display minutes31. hrs.setText(time/3600 + ""); //Display hours32. }33. } catch(InterruptedException e) { System.out.println("Timer Stops"); }34. }35. }

• Here the Thread appears as an Inner Class.

• Compare this with the example 2 of the previous lecture in which the thread was implemented by implementing the Runnable interface.

• As an Inner Class the thread can access the textfields hrs, min and sec of the enclosing class.

• Both usages are acceptable.

33

Example 2 – Contd.

36. public void actionPerformed(ActionEvent e)37. {38. t.interrupt(); //Stop the timer39. if(t.isInterrupted()) //If successful,40. {41. JOptionPane.showMessageDialog(this, 42. "Time stopped " + hrs.getText() + ":" + min.getText() + ":" + sec.getText());42. System.exit(0);43. }44. }45. 46. public static void main(String[] args) { new TimeThread3(); }47. }

34

Thread Synchronization

• When two or more threads access a shared resource, (for example a variable), the resource may

be corrupted e.g., unsynchronized threads accessing the same database

• Such blocks of code are usually called critical sections and must be executed in a mutually-exclusive way

• When a block of Java code guarded by the synchronized keyword, it can be executed by only one thread at any time

• You can synchronize an entire method or just a few lines of code by using the synchronized keyword

• Note that code that is thread-safe (i.e, guarded by synchronized) is slower than code that is not.

• Next, we present an example which shows problems due to shared thread access and then the synchronized thread version.

35

Example 2class BankAccount

{ private int balance;

public BankAccount(int balance)

{ this.balance = balance; }

public void doNothing()

{ depositWithdraw(10);

depositWithdraw(20);

depositWithdraw(30);

}

public void depositWithdraw(int money)

{ try

{ balance += money;

Thread.sleep((long)(Math.random()*1000));

balance -= money;

} catch(InterruptedException e){}

}

int get()

{ return balance; }

}

public class UnsafeBankAccount extends Thread{

BankAccount ba;

public UnsafeBankAccount(BankAccount ba)

{ this.ba = ba; }

public void run()

{ System.out.println(getName()+" got balance: "+ba.get());

ba.doNothing();

System.out.println(getName()+" got balance: "+ba.get());

}

public static void main(String[] args ){

BankAccount ba = new BankAccount(0);

for(int i=0; i<9; i++)

new UnsafeBankAccount(ba).start();

} }

36

Thread Synchronization

• When the program is executed, it may produce erroneous output.

• This is because many threads are executing the method depositWithdraw(int money) and get() at the same time.

• So it may be possible that while one thread is incrementing the variable balance, many others are decreasing it and vice versa.

• In the output snapshot shown here, at one point the balance is 190 although practically the user deposits and withdraws a maximum amount of 30 each time.

37

Thread Synchronization

• One solution to the problem pointed out is to use the synchronized keyword with all methods that deal with the variable balance.

• This is to ensure that only one thread accesses the variable balance at a time.

• Other threads may wait() while one thread is accessing and modifying the variable balance. The method wait() is inherited by the class java.lang.Thread from the class java.lang.Object.

• When the thread accessing and modifying the variable balance is done, it may notifyAll() threads waiting to execute the synchronized methods.

• The complete correct program appears on the next slide.

38

Example 3class MyBankAccount{ private int balance; private boolean busy = false; public MyBankAccount(int balance) { this.balance = balance; } public void doNothing() { depositWithdraw(10); depositWithdraw(20); depositWithdraw(30); } public synchronized void depositWithdraw(int money) { try { while(busy) wait(); busy = true; balance += money; Thread.sleep((long)(Math.random()*1000)); balance -= money; busy = false; notifyAll(); //notifies all waiting threads } catch(InterruptedException e){} } synchronized int get() { return balance; } synchronized boolean busy() { return busy; } }

public class SafeBankAccount extends Thread{ MyBankAccount ba; public SafeBankAccount(MyBankAccount ba) { this.ba = ba; } public void run() { System.out.println(getName()+" got initial balance: "+ ba.get()); ba.doNothing(); System.out.println(getName()+" got final balance: "+ ba.get()); } public static void main(String[] args ){ MyBankAccount ba = new MyBankAccount(0); for(int i=0; i < 9; i++) new SafeBankAccount(ba).start(); } }

39

Threads Animation

• In addition to applications in accessing databases, threads can also be used to create animations.

• In the next example we show a program where a thread is used to create an animation of a ball moving vertically.

• The run() method increments the y coordinate of the ball to be drawn. If the y-coordinate exceeds the height of the window, the ball is reflected back by negating the increment.

• The complete program appears in the next slide.

40

Example 4 – Thread Animations

import java.awt.*;

import java.applet.Applet;

public class UpDown extends Applet implements Runnable {

int radius, x, y;

Thread t;

public void init() {

radius = 20;

x = 30; //signifies x location

y = 20; //signifies y location

Thread t = new Thread(this);

t.start();

}

public void paint(Graphics g) {

g.setColor(Color.blue);

g.fillOval(x - radius, y - radius, 2*radius, 2*radius);

}

public void run(){

int yDir = +1;

int incr = 10;

int sleepFor = 50;

while(true) {

y += (incr * yDir);

repaint();

if(y - radius < incr ||

y + radius + incr > getSize().height)

yDir *= -1; //reflect the ball back

try{

t.sleep(sleepFor);

}catch(InterruptedException e) {}

} // end while

}

}