Chapt7-Threads (Updated)
Chapt7-Threads (Updated)
Threads
- Threads
- Basic Control of Threads
- Thread Interaction
- Putting It Together
Suzana/Java II-Chapt2
Objectives
• Define thread
• Understanding multithreading concept
• Creating threads by extending the Thread
class
• Understand thread controls and
communication
• Define thread groups
Suzana/Java II-Chapt2
What is thread ?
Thread 1
Thread 2
Thread 3
Suzana/Java II-Chapt2
Multithreading
Thread 1
Thread 2
Thread 3
Suzana/Java II-Chapt2
Creating custom threads class
by extending Thread class
Solution :
The program has 3 independent threads. To run
them concurrently, it needs to create a runnable
object for each thread. Because the first two
threads have similar functionality, they can be
defined in one thread class.
Suzana/Java II-Chapt2
Creating custom threads class
by extending Thread class
public class TestThread
{ /** main method */
public static void main(String[] args)
{
// creates 3 threads
PrintChar printA = new PrintChar('a', 100);
PrintChar printB = new PrintChar('b', 100);
PrintNum print100 = new PrintNum(100);
//starts the thread means telling the JVM that the threads are ready to run
print100.start();
printA.start();
printB.start();
}
} // end class
Suzana/Java II-Chapt2
// The thread class for printing a specified character
// in specified times
class PrintChar extends Thread
{ private char charToPrint; // The character to print
private int times; // The times to repeat
Suzana/Java II-Chapt2
Program output
3 threads running simultaneously on a single-CPU system..
The 3 threads will share the CPU time and take turns printing numbers
and letters on the console..
Suzana/Java II-Chapt2
Thread Controls and Communications
java.lang.Runnable
java.lang.Thread
+sleep(millis: long): void Puts the runnable object to sleep for a specified time in milisecond
+yield(): void Causes this thread to pause temporarily and allow other threads to
execute
2) sleep()
public void run()
{ for (int i = 1; i <= lastNum; i++)
{ System.out.print(" " + i);
try {
if (i>=50) Thread.sleep(1);
}
catch(InterruptedException ex) {
}
}
}
Suzana/Java II-Chapt2
Thread operations (cont’):
3) join()
public void run()
{ for (int i = 1; i <= lastNum; i++)
{ System.out.print(" " + i);
try {
if (i==50) printA.join();
}
catch(InterruptedException ex) {
}
}
}
Suzana/Java II-Chapt2
join()
The joint() method is used to force one thread to wait for another thread to finish.
Thread t1 Thread t2
t2.join()
Wait for t2 to
finish
t2 finished
Suzana/Java II-Chapt2
Thread States
yield() / timeout run()
Running
returns
Thread
created start() run()
sleep() Finished
New Ready
join()
interrupt() wait()
Target
finishe
d
Wait for target Wait for Wait to be
to finish time out notified
notify()/notifyAll()
Time
Blocked out
interrupted()
A thread can be in one of the five states: New, Ready, Running, Blocked or Finished
Suzana/Java II-Chapt2
Thread States (cont)
Suzana/Java II-Chapt2
Thread States (cont)
Suzana/Java II-Chapt2
Thread Priorities
Suzana/Java II-Chapt2
Thread Priorities (cont)
Suzana/Java II-Chapt2
Thread Groups
3. To find out how many threads in a group are currently running, use the
activeCount(). The following statements displays the active number of
threads in group g :
System.out.println(“ The number of runnanble threads in the group”
+ g.activeCount());
Suzana/Java II-Chapt2