Sei sulla pagina 1di 17

CIT3154 ADVANCED PROGRAMMING

WEEK 8to edit Click Multithreading

Master subtitle style

Ashley Ng Sok Choo ashleyng@segi.edu.my 11

LEARNING OBJECTIVES
q

Understand the concept of multithreading and apply it in system development.

q q

Write thread by extending the Thread class. Write threads by implementing the Runnable interface in cases of multiple inheritance.

Understand the life cycle of thread states and set thread priorities.

Use thread synchronization to avoid resource conflicts.

22

TOPICS
q q q

Threads Concept Creating Threads by Extending the Thread class Creating Threads by Implementing the Runnable Interface Controlling Threads and Thread Status Thread Groups Synchronization

q q q

33

Threads concept
Multiple threads on multiple CPUs
Ta hd r 1 e Ta hd r 2 e Ta hd r 3 e

Multiple threads sharing a single CPU

Ta hd r 1 e Ta hd r 2 e Ta hd r 3 e

44

Creating Threads by Extending the Thread class

// Custom thread class // Client class public class CustomThread extends Thread public class Client { { ... ... public CustomThread(...) public someMethod() { { ... ... } // Create a thread CustomThread thread = new CustomThread(.. // Override the run method in Thread public void run() // Start a thread { thread.start(); // Tell system how to run custom thread ... ... } } ... ... } }

55

Using the Thread Class to Create and Launch Threads


q

Objective: Create and run three threads:


q q

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.

66

Creating Threads by Implementing the Runnable Interface


// Custom thread class public class CustomThread implements Runnable { ... public CustomThread(...) { ... } // Client class public class Client { ... public someMethod() { ... // Create an instance of CustomThread CustomThread customThread = new CustomThread(...); // Create a thread Thread thread = new Thread(customThr ead); // Start a thread thread.start(); ... } ... }

// Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... }

77

Using the Runnabel Interface to Create and Launch Threads


q

Objective: Create and run three threads:


q q

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.

88

Controlling Threads and Thread States


q

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() Starts the thread, which causes the run() method to be invoked. Called by the runnable object in the client class. static void sleep(long millis) throws InterruptedException Puts the runnable object to sleep for a specified time in milliseconds.

99

Controlling Threads and Thread States


q

void stop() Stops the thread. void suspend() Suspends the thread. Use the resume() method to

resume.
q

void resume() Resumes the thread suspended with the suspend()

method.

1010

Thread Priority
q

Each thread is assigned a default priority of


Thread.NORM_PRIORITY. You can reset the

priority using setPriority(int priority).


q

Some constants for priorities include


Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

1111

Thread States

Thread created start new

ready

stop

finished

resume, notify, or notifyAll

run

yield, or time expired

stop or complete stop

running suspend, sleep, or wait

blocked

1212

Thread Groups
q

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:

Thread t = new Thread(g, new ThreadClass(), "This thread");

1313

Thread Groups
q

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());

1414

Synchronization
A shared resource may be corrupted if it is accessed simultaneously by multiple threads. For example, two unsynchronized threads accessing the same bank account causes conflict.
Step 1 2 3 4 balance 0 0 1 1 thread[ i]
newBalance = bank.getBalance() + 1; newBalance = bank.getBalance() + 1; bank.setBalance( newBalance); bank.setBalance( newBalance);

thread[j]

1515

Showing Resource Conflict


q

Objective: create and launch 100 threads, each of which adds a penny to a piggy bank. Assume that the piggy bank is initially empty.
Object
-char token +getToken +setToken +paintComponet +mouseClicked PiggyBankWithoutSync -PiggyBank bank -Thread[] thread +main

1
Thread
-char token +getToken +setToken 100 +paintComponet +mouseClicked AddAPennyThread +run()

1
Object
-char token +getToken +setToken +paintComponet +mouseClicked PiggyBank -balance +getBalance +setBalance

1616

The synchronized keyword


To avoid resource conflicts, Java uses the keyword synchronized to synchronize method invocation so that only one thread can be in a method at a time.

1717

Potrebbero piacerti anche