Threads and Multithreading 15 Jun21 Multiprocessing n n

  • Slides: 22
Download presentation
Threads and Multithreading 15 -Jun-21

Threads and Multithreading 15 -Jun-21

Multiprocessing n n n Modern operating systems are multiprocessing Appear to do more than

Multiprocessing n n n Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches: n n n Cooperative multiprocessing Preemptive multiprocessing Really having multiple processors 2

Multithreading n n Multithreading programs appear to do more than one thing at a

Multithreading n n Multithreading programs appear to do more than one thing at a time Same ideas as multiprocessing, but within a single program More efficient than multiprocessing Java tries to hide the underlying multiprocessing implementation 3

Why multithreading? n Allows you to do more than one thing at once n

Why multithreading? n Allows you to do more than one thing at once n n Play music on your computer’s CD player, Download several files in the background, while you are writing a letter Multithreading is essential for animation n n One thread does the animation Another thread responds to user inputs 4

Threads n A Thread is a single flow of control n n n When

Threads n A Thread is a single flow of control n n n When you step through a program, you are following a Thread Your previous programs all had one Thread A Thread is an Object you can create and control 5

Sleeping n Every program uses at least one Thread n Thread. sleep(int milliseconds); n

Sleeping n Every program uses at least one Thread n Thread. sleep(int milliseconds); n n n A millisecond is 1/1000 of a second try { Thread. sleep(1000); } catch (Interrupted. Exception e) { } sleep only works for the current Thread 6

States of a Thread n A Thread can be in one of four states:

States of a Thread n A Thread can be in one of four states: n n n Ready: all set to run Running: actually doing something Waiting, or blocked: needs something Dead: will never do anything again State names vary across textbooks You have some control, but the Java scheduler has more 7

State transitions waiting start ready running dead 8

State transitions waiting start ready running dead 8

Two ways of creating Threads n You can extend the Thread class: n n

Two ways of creating Threads n You can extend the Thread class: n n n Or you can implement the Runnable interface: n n n class Animation extends Thread {…} Limiting, since you can only extend one class Animation implements Runnable {…} requires public void run( ) I recommend the second for most programs 9

Extending Thread n n class Animation extends Thread { @Override public void run( )

Extending Thread n n class Animation extends Thread { @Override public void run( ) { code for this thread } Anything else you want in this class } Animation anim = new Animation( ); n n A newly created Thread is in the Ready state To start the anim Thread running, call anim. start( ); start( ) is a request to the scheduler to run the Thread --it may not happen right away The Thread should eventually enter the Running state 10

Implementing Runnable n n class Animation implements Runnable {…} The Runnable interface requires run(

Implementing Runnable n n class Animation implements Runnable {…} The Runnable interface requires run( ) n n Animation anim = new Animation( ); Thread my. Thread = new Thread(anim); To start the Thread running, call my. Thread. start( ); n n This is the “main” method of your new Thread You do not write the start() method—it’s provided by Java As always, start( ) is a request to the scheduler to run the Thread--it may not happen right away 11

Starting a Thread n n n Every Thread has a start( ) method Do

Starting a Thread n n n Every Thread has a start( ) method Do not write or override start( ) You call start( ) to request a Thread to run The scheduler then (eventually) calls run( ) You must supply public void run( ) n This is where you put the code that the Thread is going to run 12

Extending Thread: summary class Animation extends Thread { public void run( ) { while

Extending Thread: summary class Animation extends Thread { public void run( ) { while (ok. To. Run) {. . . } } } Animation anim = new Animation( ); anim. start( ); 13

Implementing Runnable: summary class Animation extends Applet implements Runnable { public void run( )

Implementing Runnable: summary class Animation extends Applet implements Runnable { public void run( ) { while (ok. To. Run) {. . . } } } Animation anim = new Animation( ); Thread my. Thread = new Thread(anim); my. Thread. start( ); 14

Things a Thread can do n n n n Thread. sleep(milliseconds) yield( ) Thread

Things a Thread can do n n n n Thread. sleep(milliseconds) yield( ) Thread me = current. Thread( ); int my. Priority = me. get. Priority( ); me. set. Priority(NORM_PRIORITY); if (other. Thread. is. Alive( )) { … } join(other. Thread); 15

Animation requires two Threads n n n Suppose you set up Buttons and attach

Animation requires two Threads n n n Suppose you set up Buttons and attach Listeners to those buttons. . . …then your code goes into a loop doing the animation… …who’s listening? n n Not this code; it’s busy doing the animation sleep(ms) doesn’t help! 16

How to animate n Create your buttons and attach listeners in your first (original)

How to animate n Create your buttons and attach listeners in your first (original) Thread Create a second Thread to run the animation Start the animation The original Thread is free to listen to the buttons n However, n n n Whenever you have a GUI, Java automatically creates a second Thread for you You only have to do this yourself for more complex programs 17

Things a Thread should NOT do n n The Thread controls its own destiny

Things a Thread should NOT do n n The Thread controls its own destiny Deprecated methods: n n n my. Thread. stop( ) my. Thread. suspend( ) my. Thread. resume( ) Outside control turned out to be a Bad Idea Don’t do this! 18

How to control another Thread n n Don’t use the deprecated methods! Instead, put

How to control another Thread n n Don’t use the deprecated methods! Instead, put a request where the other Thread can find it boolean ok. To. Run = true; animation. start( ); public void run( ) { while (controller. ok. To. Run) {…} 19

A problem int k = 0; Thread #1: k = k + 1; n

A problem int k = 0; Thread #1: k = k + 1; n n Thread #2: System. out. print(k); What gets printed as the value of k? This is a trivial example of what is, in general, a very difficult problem 20

Tools for a solution n You can synchronize on an object: n n You

Tools for a solution n You can synchronize on an object: n n You can synchronize a method (uses this): n n n synchronized (obj) {. . . code that uses/modifies obj. . . } No other code can use or modify this object at the same time Notice that synchronized is being used as a statement synchronized void add. One(arg 1, arg 2, . . . ) { code } Only one synchronized method in a class can be used at a time (other methods can be used simultaneously) Synchronization is a tool, not a solution— multithreading is in general a very hard problem 21

The End Debugging is twice as hard as writing the code in the first

The End Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. — Brian W. Kernighan Measuring programming progress by lines of code is like measuring aircraft building progress by weight. — Bill Gates 22