Java Threads Outline Motivation The class Thread Example

















- Slides: 17
Java Threads (Outline) Motivation The class Thread Example program: Thread. Race The Runnable interface Example: Clock applet Thread state Thread priority Thread groups Application to animation CSE 341 -- S. Tanimoto Java Threads 1
Motivation Take advantage of multiprocessors Separate processing that might block on I/O or other conditions. Naturally express computations that have multiple activities going on. CSE 341 -- S. Tanimoto Java Threads 2
Forms of Parallel Processing Distributed computing: coarse-grained parallelism with relatively high communication cost (“cluster computing”) Fine-grained parallel processing: massive parallelism, single-instruction-stream multipledata-stream processing. (e. g. , for in-camera image processing). MIMD Multiprocessors with separate memories. Parallel Random Access Machines (shared memory) Multithreaded architectures (shared memory) CSE 341 -- S. Tanimoto Java Threads 3
The Class Thread provides a constructor plus methods start, stop, run, interrupt, suspend, set priorities, get status, and resume threads. public Thread() -- constructor for a new thread. This or the constructor for a subclass is called by the client process to create a new thread. public void start() -- Called by client process to request that the Java runtime system run this thread instance. public void run() -- We override this method to perform the work that we want done by the thread. public void stop() -- (deprecated) Usually called by the client process to stop the thread. public void interrupt() -- Usually called by the client process to get the thread’s attention, e. g. , so that it can exit its run method and thereby stop. CSE 341 -- S. Tanimoto Java Threads 4
Example: Thread. Race. java // Thread. Race. java Steve Tanimoto public class Thread. Race { public static void main( String [] args) { Counting. Thread a. Counter = new Counting. Thread("a"); Counting. Thread b. Counter = new Counting. Thread("b"); a. Counter. start(); b. Counter. start(); } } CSE 341 -- S. Tanimoto Java Threads 5
Class Counting. Thread (for Thread. Race) public class Counting. Thread extends Thread { String context; public Counting. Thread(String context) { this. context = context; } public void run() { for (int i = 0; i < 50; i++) { System. out. print(context + i + context + " "); } } } CSE 341 -- S. Tanimoto Java Threads 6
Sample Output from Thread. Race C: WINDOWSDesktopThreads>d: jdk 1. 2. 1binjava Thread. Race a 0 a b 0 b a 1 a b 1 b a 2 a b 2 b b 3 b b 4 b b 5 b b 6 b b 7 b b 8 b b 9 b b 10 b b 11 b b 12 b a 3 a b 13 b a 4 a b 14 b a 5 a b 15 b b 16 b b 17 b b 18 b b 19 b b 20 b b 21 b b 22 b b 23 b a 6 a b 24 b b 25 b a 7 a b 26 b a 8 a b 27 b a 9 a b 28 b a 10 a b 29 b a 11 a b 30 b a 12 a b 31 b a 13 a b 32 b a 14 a b 33 b a 15 a b 34 b a 16 a b 35 b a 17 a b 36 b b 37 b b 38 b b 39 b b 40 b b 41 b b 42 b b 43 b b 44 b a 18 a b 45 b a 19 a b 46 b a 20 a b 47 b a 21 a b 48 b a 22 a b 49 b a 23 a a 24 a a 25 a a 26 a a 27 a a 28 a a 29 a a 30 a a 31 a a 32 a a 33 a a 34 a a 35 a a 36 a a 37 a a 38 a a 39 a a 40 a a 41 a a 42 a a 43 a a 44 a a 45 a a 46 a a 47 a a 48 a a 49 a CSE 341 -- S. Tanimoto Java Threads 7
The Runnable Interface Solves the problem that we might not be able to subclass Thread because we want to subclass something else, like Applet public class my. Applet extends Applet implements runnable {} CSE 341 -- S. Tanimoto Java Threads 8
Handling threads in an Applet Declare your Applet subclass to implement Runnable. public void run() {. . . } //Add this to do the work. What if the browser goes to another web page or quits? Provide a way for the browser to not only suspend the main thread, but also to suspend and resume any extra threads: public void start() { extra. Thread. resume(); } public void stop() { extra. Thread. time. To. Sleep=true; extra. Thread. interrupt(); } public void destroy() { extra. Thread. time. To. Quit=true; extra. Thread. interrupt(); } CSE 341 -- S. Tanimoto Java Threads 9
Example: Clock Applet // Clock. java Steve Tanimoto import java. awt. *; import java. applet. *; import java. util. *; public class Clock extends Applet implements Runnable { class Time. Thread extends Thread { boolean time. To. Quit = false; public Time. Thread(Runnable r) { super(r); } } Time. Thread time. Thread; public Clock() { } public void init() { time. Thread = new Time. Thread(this); time. Thread. start(); } CSE 341 -- S. Tanimoto Java Threads 10
Clock Applet (continued) public void paint(Graphics g) { g. draw. String("The time is: " + (new Date()), 100, 50); } public void run() { while (true) { try { Thread. sleep(1000); } catch (Interrupted. Exception e) { if (time. Thread. time. To. Quit) return; } repaint(); } } public void start() {} public void stop() {} public void destroy() { time. Thread. time. To. Quit = true; time. Thread. interrupt(); } } CSE 341 -- S. Tanimoto Java Threads 11
Appearance of the Clock Applet This is a test of the Clock applet. The time is Fri Apr 09 01: 32: 00 PDT 1999 CSE 341 -- S. Tanimoto Java Threads 12
Thread State (with deprecated methods) create new start running ready yield suspend or sleep stop resume inactive stop CSE 341 -- S. Tanimoto Java Threads finished 13
Thread State (with new methods) create new start running ready yield, interrupt wait, join, or sleep interrupt return inactive finished CSE 341 -- S. Tanimoto Java Threads 14
Thread Priority 1 = MIN_PRIORITY, 10 = MAX_PRIORITY int P = my. Thread. get. Priority(); my. Thread. set. Priority(Thread. MAX_PRIORITY); As long as a thread of priority P is in the ready state, no thread of priority less than P will be moved to the runnable state. For threads of equal priority, CPU resources are shared, round-robin style. CSE 341 -- S. Tanimoto Java Threads 15
Thread Groups When a number of threads are to be handled in the same manner, they can be put in a thread group. Thread. Group tg = new Thread. Group("my sprites"); There’s a Thread constructor that takes a group as an arg. public Thread(Thread. Group g, Runnable target, String name); There are many methods for Thread. Group objects such as get. Max. Priority, destroy, etc. Thread. Groups can contain Thread. Groups. Trees of Thread. Groups are therefore possible. CSE 341 -- S. Tanimoto Java Threads 16
Application to Animation requires controlled timing. A thread that uses the sleep(int) method can handle timed updates to a display. Multiple threads can concurrent tasks, but synchronization may become an issue. Therefore a single animation thread (but separate from the main thread) often works best. The main thread handles the user interface and other activities that are logically separate from those belonging to the animation. CSE 341 -- S. Tanimoto Java Threads 17