1 / 25

308-203A Introduction to Computing II Lecture 17: Programming with Threads in Java

308-203A Introduction to Computing II Lecture 17: Programming with Threads in Java. Fall Session 2000. Multi-tasking. It is desireable to have a single computer run more than one program at a time, e.g. running Netscape in one window while editing a word-processor document in another.

berne
Download Presentation

308-203A Introduction to Computing II Lecture 17: Programming with Threads in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 308-203AIntroduction to Computing IILecture 17: Programming with Threads in Java Fall Session 2000

  2. Multi-tasking It is desireable to have a single computer run more than one program at a time, e.g. running Netscape in one window while editing a word-processor document in another. This goes by the names “multi-tasking” or “course- grained parallelism.” It’s managed autaomatically by all modern Operating Systems (even Windows).

  3. Threads = Multi-taskingwithin a Program Reasons we might want to do this: - Efficiency: different activities can move forward at their own speed - Parallel processing: A computer may have more than one processor to utilize, or more than one computer on a network may participate in a computation - Natural for some programs, like…

  4. Event-driven programming Many programs must react in some way to events which come from their environment. Example 1 : Graphical User Interfaces respond to mouse-clicks and keystrokes Example 2 : Programs which run over the Internet respond to receiving network packets Example 3 : Robots respond to signals detected by sensors

  5. Event-driven programming All these examples naturally suggest the use of threads. Take the example of Netscape, with two windows open. Window A: Loading some page that’s taking forever (waiting for an event from the network) Window B: Has some on-line form to fill out (waiting for events from the mouse /keyboard) No fun if we have to wait for A to load to fill in B

  6. A Bad Use of Threads Ever notice that when Microsoft Word™ marks spelling and grammatical errors, there is sometimes a delay between the time you make a change and the appearance of the green squiggly line to tell you what Microsoft thinks of your command of basic grammar? That grammar checker is likely running in another thread to avoid slowing down user input.

  7. Creating threads in Java Java provides a JDK class to let you create threads: The recipe: 1) Extend a class from Thread 2) Override the method run() to do whatever you want it to do 3) Create an object of your class and call a pre-defined method called start()

  8. An example Create a thread class: class helloWorldThread extends Thread { run() { wait(5000); // wait 5 seconds System.out.println(“Hello world”); } }

  9. An example Use the thread class: void main( String args [] ) { helloWorldThread hwt = new helloWorldThread( ); System.out.println(“Starting the thread…”); hwt.start(); System.out.println(“Done with the main method”); }

  10. An example Output: Starting the thread… Done with the main method Hello world Immediate Five seconds later

  11. Race Conditions Take two threads which both use the following code: run( ) { int j = 1; while (true) System.out.println(“Count = “+ j); } What is the output?

  12. Race Conditions Answer: Almost anything! Count = 1 // Thread “a” Count = 2 // Thread “a” Count = 3 // Thread “a” Count = 1 // Thread “b” Count = 2 // Thread “b” Count = 4 // Thread “a” Count = 3 // Thread “b” Count = 4 // Thread “b” Count = 5 // Thread “b”

  13. Sharing of data When two threads share data, things can become problematic. Consider the following: run( ) { for (int i = 0; i < 10000; i++) { x = i; // say x is static if (x != i) System.out.println(“Error!”); } }

  14. Sharing of data If you run two such threads , there will be lots of “errors” Thread “a” x = 1; if (x != 1) “error” x = 2 if (x != 2) “error” Thread “b” x = 1; if (x != 1) “error” x = 2 if (x != 2) “error”

  15. Data Hazards If threads could be run in an order where data becomes corrupted, we say that there is a “Data Hazard.” This is almost universally the case when two threads access data in an unprotected way.

  16. Data Hazard - example Consider two threads inserting elements at the head of a linked-list: Recall the code to do this is: newNode.next = list.head ; list = newNode ;

  17. Data Hazard - example list head First node newNode B newNode A Thread A newNode.next = list.head ; list.head = newNode ; Thread B newNode.next = list.head ; list.head = newNode ;

  18. Data Hazard - example list head First node newNode B newNode A Thread A newNode.next = list.head ; list.head = newNode ; Thread B newNode.next = list.head ; list.head = newNode ;

  19. Data Hazard - example list head First node newNode B newNode A Thread A newNode.next = list.head ; list.head = newNode ; Thread B newNode.next = list.head ; list.head = newNode ;

  20. Data Hazard - example list head First node newNode B newNode A Thread A newNode.next = list.head ; list.head = newNode ; Thread B newNode.next = list.head ; list.head = newNode ;

  21. Data Hazard - example list head First node newNode B newNode A Thread A newNode.next = list.head ; list.head = newNode ; Thread B newNode.next = list.head ; list.head = newNode ;

  22. Data Hazard - example list head First node newNode B newNode A The effect is that the newNode for thread A got lost: it is not in the resulting list at all.

  23. Mutual exclusion A solution is to designate access to certain portions of code, known as critical sections, as being mutually exclusive. For example, if only one thread at a time is allowed to perform list operations like insertion, the problem from the previous slides cannot occur.

  24. Mutual exclusion in Java“synchronized” In Java, you can specify that certain methods are synchronized, which tells the Java runtime environment to guarantee mutually exclusive access to those methods. For example, in Hashtable.java you find the actual definitions of put and get are: public synchronized Object get(Object key) … public synchronized Object put(Object key, Object value)…

  25. Any questions?

More Related