Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.

Similar presentations


Presentation on theme: "Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority."— Presentation transcript:

1 Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority Thread Priority Thread Groups Thread Groups Synchronization Synchronization

2 Threads Concept Multiple threads on multiple CPUs Multiple threads sharing a single CPU

3 The Thread Class //User Defined Thread Class //Client Class class UserThread extends Thread public class Client {... public UserThread() main() { UserThread ut =... new UserThread(); }...... public void run() ut.start(); {...... } } } }

4 Thread Constructors and Methods Thread() Thread() Creates a runnable object. void run() void run() Invoked by the Java runtime system to execute the thread. You must override this method and provide the code you want your thread to execute. void start() void start() Starts the thread, which causes the run() method to be invoked. Called by the runnable object in the client class.

5 Thread Constructors and Methods, cont. void stop() void stop() Stops the thread. (deprecated in JDK 1.2) void suspend() (deprecated in JDK 1.2) void suspend() (deprecated in JDK 1.2) Suspends the thread. Use the resume() method to resume. void resume() (deprecated in JDK 1.2) void resume() (deprecated in JDK 1.2) Resumes the thread suspended with the suspend() method. static void sleep(long millis) throws InterruptedException static void sleep(long millis) throws InterruptedException Puts the runnable object to sleep for a specified time in milliseconds.

6 Thread Constructors and Methods, cont. void interrupt() void interrupt() Interrupts the running thread. static boolean interrupted() static boolean interrupted() Tests to see whether the current thread has been interrupted. boolean isAlive() boolean isAlive() Tests to see whether the thread is currently running. void setPriority(int p) void setPriority(int p) Sets priority p for this thread.

7 Example 13.1 Using the Thread Class Objective: Create and run three threads: Objective: Create and run three threads:  The first thread prints the letter a 100 times.  The second thread prints the letter b 100 times.  The third thread prints the integers 1 through 100. TestThreadsRun

8 Implementing the Runnable Interface Sketch class MyApplet extends JApplet implements Runnable { private Thread timer = null; public void init() public void init() { timer = new Thread(this); { timer = new Thread(this); timer.start(); timer.start();}...... public void run() public void run() {... } {... }}

9 The Runnable Interface, cont. public void run() { while (true) { while (true) { repaint(); repaint(); try { try { timer.sleep(1000); timer.sleep(1000); synchronized (this) { synchronized (this) { while (suspended) while (suspended) wait(); wait(); } } catch (InterruptedException ex) { catch (InterruptedException ex) { } }}

10 The Runnable Interface, cont. public synchronized void resume() { if (suspended) if (suspended) { suspended = false; suspended = false; notify(); notify(); }} public synchronized void suspend() { suspended = true; suspended = true;}

11 Example 13.2 Implementing the Runnable Interface in an Applet Objective: Simulate a running clock by using a separate thread to repaint the clock. Objective: Simulate a running clock by using a separate thread to repaint the clock. ClockApplet Run Applet Viewer

12 Example 13.3 Controlling a Group of Clocks Objective: Using threads to control a group of clocks. Objective: Using threads to control a group of clocks. ClockGroup Run

13 Thread States

14 Thread Priority Each thread is assigned a default priority of Thread.NORM_PRIORITY. You can reset the priority using setPriority(int priority). Each thread is assigned a default priority of Thread.NORM_PRIORITY. You can reset the priority using setPriority(int priority). Some constants for priorities include Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY Some constants for priorities include Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

15 Thread Groups Construct a thread group using the ThreadGroup constructor: Construct a thread group using the ThreadGroup constructor: ThreadGroup g = new ThreadGroup("timer thread group"); Place a thread in a thread group using the Thread constructor: Place a thread in a thread group using the Thread constructor: Thread t = new Thread(g, new ThreadClass(), "This thread");

16 Thread Groups, cont. To find out how many threads in a group are currently running, use the activeCount() method: To find out how many threads in a group are currently running, use the activeCount() method: System.out.println("The number of runnable threads in the group "+ g.activeCount());

17 Synchronization

18 Example 13.5 Resource corruption Resource corruption TestTransferWithoutSync TestTransferWithSync  The keyword synchronized Run


Download ppt "Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority."

Similar presentations


Ads by Google