Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Threads (Outline)

Similar presentations


Presentation on theme: "Java Threads (Outline)"— Presentation transcript:

1 Java Threads (Outline)
Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock applet Thread state Thread priority Thread groups Application to animation CSE S. Tanimoto Java Threads

2 CSE 341 -- S. Tanimoto Java Threads
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 S. Tanimoto Java Threads

3 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 multiple-data-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 S. Tanimoto Java Threads

4 CSE 341 -- S. Tanimoto Java Threads
The Class Thread 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 S. Tanimoto Java Threads

5 Example: ThreadRace.java
// ThreadRace.java Steve Tanimoto public class ThreadRace { public static void main( String [] args) { CountingThread aCounter = new CountingThread("a"); CountingThread bCounter = new CountingThread("b"); aCounter.start(); bCounter.start(); } CSE S. Tanimoto Java Threads

6 Class CountingThread (for ThreadRace)
public class CountingThread extends Thread { String context; public CountingThread(String context) { this.context = context; } public void run() { for (int i = 0; i < 50; i++) { System.out.print(context + i + context + " "); CSE S. Tanimoto Java Threads

7 Sample Output from ThreadRace
C:\WINDOWS\Desktop\Threads>d:\jdk1.2.1\bin\java ThreadRace a0a b0b a1a b1b a2a b2b b3b b4b b5b b6b b7b b8b b9b b10b b11b b12b a3a b13b a4a b14b a5a b15b b16b b17b b18b b19b b20b b21b b22b b23b a6a b24b b25b a7a b26b a8a b27b a9a b28b a10a b29b a11a b30b a12a b31b a13a b32b a14a b33b a15a b34b a16a b35b a17a b36b b37b b38b b39b b40b b41b b42b b43b b44b a18a b45b a19a b46b a20a b47b a21a b48b a22a b49b a23a a24a a25a a26a a27a a28a a29a a30a a31a a32a a33a a34a a35a a36a a37a a38a a39a a40a a41a a42a a43a a44a a45a a46a a47a a48a a49a CSE S. Tanimoto Java Threads

8 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 myApplet extends Applet implements runnable {} CSE S. Tanimoto Java Threads

9 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() { extraThread.resume(); } public void stop() { extraThread.timeToSleep=true; extraThread.interrupt(); } public void destroy() { extraThread.timeToQuit=true; extraThread.interrupt(); } CSE S. Tanimoto Java Threads

10 CSE 341 -- S. Tanimoto Java Threads
Example: Clock Applet // Clock.java Steve Tanimoto import java.awt.*; import java.applet.*; import java.util.*; public class Clock extends Applet implements Runnable { class TimeThread extends Thread { boolean timeToQuit = false; public TimeThread(Runnable r) { super(r); } } TimeThread timeThread; public Clock() { } public void init() { timeThread = new TimeThread(this); timeThread.start(); CSE S. Tanimoto Java Threads

11 Clock Applet (continued)
public void paint(Graphics g) { g.drawString("The time is: " + (new Date()), 100, 50); } public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { if (timeThread.timeToQuit) return; } repaint(); public void start() {} public void stop() {} public void destroy() { timeThread.timeToQuit = true; timeThread.interrupt(); } CSE S. Tanimoto Java Threads

12 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 S. Tanimoto Java Threads

13 Thread State (with deprecated methods)
create new start running run ready yield suspend or sleep stop resume stop inactive finished stop CSE S. Tanimoto Java Threads

14 Thread State (with new methods)
create new start running run ready yield, interrupt wait, join, or sleep interrupt return inactive finished CSE S. Tanimoto Java Threads

15 CSE 341 -- S. Tanimoto Java Threads
Thread Priority 1 = MIN_PRIORITY, 10 = MAX_PRIORITY int P = myThread.getPriority(); myThread.setPriority(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 S. Tanimoto Java Threads

16 CSE 341 -- S. Tanimoto Java Threads
Thread Groups When a number of threads are to be handled in the same manner, they can be put in a thread group. ThreadGroup tg = new ThreadGroup("my sprites"); There’s a Thread constructor that takes a group as an arg. public Thread(ThreadGroup g, Runnable target, String name); There are many methods for ThreadGroup objects such as getMaxPriority, destroy, etc. ThreadGroups can contain ThreadGroups. Trees of ThreadGroups are therefore possible. CSE S. Tanimoto Java Threads

17 Application to Animation
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 S. Tanimoto Java Threads


Download ppt "Java Threads (Outline)"

Similar presentations


Ads by Google