17
CSE 341 -- S. Tanimoto 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

Java Threads (Outline)

Embed Size (px)

DESCRIPTION

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. Motivation. Take advantage of multiprocessors - PowerPoint PPT Presentation

Citation preview

Page 1: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

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

Page 2: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

2

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.

Page 3: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

3

Forms of Parallel ProcessingDistributed 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)

Page 4: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

4

The Class ThreadThread 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.

Page 5: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

5

Example: ThreadRace.java

// ThreadRace.java Steve Tanimoto, 9 April 1999.

public class ThreadRace {

public static void main( String [] args) {

CountingThread aCounter = new CountingThread("a"); CountingThread bCounter = new CountingThread("b"); aCounter.start(); bCounter.start(); }}

Page 6: Java Threads (Outline)

CSE 341 -- 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 + " "); } }}

Page 7: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

7

Sample Output from ThreadRace

C:\WINDOWS\Desktop\Threads>d:\jdk1.2.1\bin\java ThreadRacea0a 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

Page 8: Java Threads (Outline)

CSE 341 -- 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 {}

Page 9: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

9

Handling threads in an AppletDeclare 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(); }

Page 10: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

10

Example: Clock Applet// Clock.java Steve Tanimoto, 25 October 2000.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(); }

Page 11: Java Threads (Outline)

CSE 341 -- 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(); }}

Page 12: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

12

Appearance of the Clock Applet

The time is Fri Apr 09 01:32:00 PDT 1999

This is a test of the Clock applet.

Page 13: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

13

Thread State(with deprecated methods)

inactive

new

readyrunning

finished

create

start

run

stop

stop

stopsuspend or sleepresume

yield

Page 14: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

14

Thread State(with new methods)

inactive

new

readyrunning

finished

create

start

run

return

wait, join, or sleepinterrupt

yield, interrupt

Page 15: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

15

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.

Page 16: Java Threads (Outline)

CSE 341 -- S. Tanimoto Java Threads -

16

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.

Page 17: Java Threads (Outline)

CSE 341 -- 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.