Download presentation
Presentation is loading. Please wait.
Published byBerenice Greene Modified over 8 years ago
3
A running program is a collection of “threads” – essentially independent execution streams When the main() method is called, the instructions within the method begin to execute in sequence on a primary thread The program terminates when the primary thread, and any additional threads, finish executing
7
Java application: the JVM creates a Process and a Primary Thread
8
Single-threaded application
10
1.Create a JFrame window containing JButtons, JTextField, etc. 2.Connect the JButtons, etc. to an ActionListener 3.Make the window visible Once the window is visible, a second thread is created All calls to actionPerformed() occur on the second thread The Event-Dispatching Thread
12
Explicitly creating additional threads
16
This method gets executed in place of the run (when t.start() is executed)
19
See https://faculty-web.msoe.edu/hasker/se2811/samples/https://faculty-web.msoe.edu/hasker/se2811/samples/
20
Fortunately, Java supports several mechanisms for synchronizing the execution of multiple threads Keeping code thread-safe
21
The Thread class’s join() method is one way of synchronizing two threads:
23
Once a thread enters a synchronized method, no other threads can enter until the first thread completes execution of the method, and exits the method. Thread x Thread yThread z When Thread x leaves the method, the Scheduler arbitrarily allows one of the other threads to enter the method. When the second thread exits, the Scheduler allows another waiting thread to enter. If all threads get to the method at the same time, the thread that gets to enter the method first is determined arbitrarily by the Scheduler.
24
Once a thread enters a synchronized method, no other threads can enter until the first thread completes execution of the method, and exits the method. Thread x Thread yThread z When Thread x leaves the method, the Scheduler arbitrarily allows one of the other threads to enter the method. When the second thread exits, the Scheduler allows another waiting thread to enter. If all threads get to the method at the same time, the thread that gets to enter the method first is determined arbitrarily by the Scheduler. Add syncronized to count_up()
26
If only a few statements within a method need to be guarded against simultaneous execution, use a synchronized block instead of making the entire method synchronized. Thread x Thread yThread z When Thread x leaves the block, the Scheduler arbitrarily allows one of the other threads to enter.
28
Since every class derives from Object, the class containing a synchronized block can act as the Monitor for the block: Thread x Thread yThread z
29
Or any generic Object can act as a Monitor Thread x Thread yThread z
30
Consider the following code. Suppose all threads reach the for() loop simultaneously. How do the threads compete to run the for() loop? Thread x Thread yThread z
31
After each thread executes the synchronized section, it can notify the Monitor that another thread can be allowed to enter the synchronized block as soon as it relinquishes ownership of the synchronized section by entering a wait (or exiting the synchronized section) Thread x Thread yThread z
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.