0% found this document useful (0 votes)
82 views63 pages

Unit-2 PPT-2

- Multithreading allows Java programs to execute multiple threads concurrently. Each thread defines a separate path of execution. - Threads are lightweight subprocesses that use shared memory, which saves memory compared to processes. Context switching between threads is also faster than between processes. - Common uses of multithreading include games, animations, and performing multiple operations simultaneously to save time.

Uploaded by

Santhosh B M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views63 pages

Unit-2 PPT-2

- Multithreading allows Java programs to execute multiple threads concurrently. Each thread defines a separate path of execution. - Threads are lightweight subprocesses that use shared memory, which saves memory compared to processes. Context switching between threads is also faster than between processes. - Common uses of multithreading include games, animations, and performing multiple operations simultaneously to save time.

Uploaded by

Santhosh B M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Multithreading

Multithreading
• Java provides built-in support for multithreaded programming. A multithreaded
program contains two or more parts that can run concurrently. Each part of such a
program is called a thread, and each thread defines a separate path of execution.
Thus, multithreading is a specialized form of multitasking.
• Multithreading in Java is a process of executing multiple threads
simultaneously.
• A thread is a lightweight sub-process, the smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.
• However, we use multithreading than multiprocessing because threads use a
shared memory area. They don't allocate separate memory area so saves memory,
and context-switching between the threads takes less time than process.
• Java Multithreading is mostly used in games, animation, etc.
Advantages of Java Multithreading
1) It doesn't block the user because threads are independent and you
can perform multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an
exception occurs in a single thread.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use
multitasking to utilize the CPU. Multitasking can be achieved in two ways:
Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)
1) Process-based Multitasking (Multiprocessing)
Each process has an address in memory. In other words, each process
allocates a separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for saving and
loading registers, memory maps, updating lists, etc.
2) Thread-based Multitasking (Multithreading)
Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low.
What is Thread in java
A thread is a lightweight subprocess, the smallest unit of processing. It
is a separate path of execution.
Threads are independent. If there occurs exception in one thread, it
doesn't affect other threads. It uses a shared memory area.
Difference between Multithreading and multitasking
The Java Thread Model
• The Java run-time system depends on threads for many things, and all the class
libraries are designed with multithreading in mind. In fact, Java uses threads to enable
the entire environment to be asynchronous. This helps reduce inefficiency by
preventing the waste of CPU cycles.
• Single-threaded systems use an approach called an event loop with polling. In this
model, a single thread of control runs in an infinite loop, polling a single event queue
to decide what to do next. Once this polling mechanism returns with, say, a signal that
a network file is ready to be read, then the event loop dispatches control to the
appropriate event handler. Until this event handler returns, nothing else can happen in
the program.
• This wastes CPU time. It can also result in one part of a program dominating the
system and preventing any other events from being processed. In general, in a single-
threaded environment, when a thread blocks (that is, suspends execution) because it is
waiting for some resource, the entire program stops running.
• The benefit of Java’s multithreading is that the main loop/polling
mechanism is eliminated.
• One thread can pause without stopping other parts of your program.
• most readers know, over the past few years, multi-core systems have
become commonplace. Of course, single-core systems are still in
widespread use. It is important to understand that Java’s multithreading
features work in both types of systems. In a single core system,
concurrently executing threads share the CPU, with each thread
receiving a slice of CPU time.
• Therefore, in a single-core system, two or more threads do not actually
run at the same time, but idle CPU time is utilized. However, in multi-
core systems, it is possible for two or more threads to actually execute
simultaneously. In many cases, this can further improve program
efficiency and increase the speed of certain operations.
Thread Life Cycle
• Thread is a program which has separate path of execution. A program
would contain many threads. Multi-threading ensures that the CPU time is
not wasted.
• The JVM will create main thread and this main thread is the one which is
going to run main method. Any thread that is created by the user is called
has child thread.
• For each thread there will be the priority, we can set it using setPriority(int
i) method, where i is the value from 1-10, 1 means low priority and 10 is
the maximum priority. The main thread priority is 5.
• We can get the priority of the thread by calling getPriority( ) method. If we
don’t set any priority the default priority will be 5.
States of thread
• Threads exist in several states. Here is a general description. A thread
can be running.
• It can be ready to run as soon as it gets CPU time. A running thread
can be suspended, which temporarily halts its activity.
• A suspended thread can then be resumed, allowing it to pick up where
it left off.
• A thread can be blocked when waiting for a resource. At any time, a
thread can be terminated, which halts its execution immediately. Once
terminated, a thread cannot be resumed.
Thread States in Java

• A thread is a program in execution created to perform a specific task. Life


cycle of a Java thread starts with its birth and ends on its death.
• The start() method of the Thread class is used to initiate the execution of a
thread and it goes into runnable state and the sleep() and wait() methods
of the Thread class sends the thread into non runnable state.
• After non runnable state, thread again comes into runnable state and starts
its execution. The run() method of thread is very much important. After
executing the run() method, the lifecycle of thread is completed.
• All these phases of threads are the states of thread in Java.
• To work with threads in a program, it is important to identify thread state.
The following figure shows thread states in Java thread life cycle.
Thread States in Java
• A thread is a path of execution in a program that goes
through the following states of a thread. The five states are as
follows:
1.New
2.Runnable
3.Running
4.Blocked (Non-runnable state)
5.Dead
New (Newborn State)
• When an instance of the Thread class is created a new thread is born
and is known to be in New-born state. That is, when a thread is born, it
enters into new state but its execution phase has not been started yet
on the instance.
• In simpler terms, Thread object is created but it cannot execute any
program statement because it is not in an execution state of the thread.
Only start() method can be called on a new thread; otherwise,
an IllegalThreadStateException will be thrown.
Runnable State
• The second phase of a new-born thread is the execution phase. When the
start() method is called on a the new instance of a thread, it enters into a
runnable state.
• In the runnable state, thread is ready for execution and is waiting for
availability of the processor (CPU time). There are many threads that are
ready for execution, they all are waiting in a queue (line).
• If all threads have equal priority, a time slot is assigned for each thread
execution on the basis of first-come, first-serve manner by CPU. The
process of allocating time to threads is known as time slicing. A thread
can come into runnable state from running, waiting, or new states.
Running State
• Running means Processor (CPU) has allocated time slot to thread for its execution. When
thread scheduler selects a thread from the runnable state for execution, it goes into
running state. Look at the above figure.
• In running state, processor gives its time to the thread for execution and executes its run
method. It is the state where thread performs its actual functions. A thread can come into
running state only from runnable state.
• A running thread may give up its control in any one of the following situations and can
enter into the blocked state.
1.When sleep() method is invoked on a thread to sleep for specified time period, the thread
is out of queue during this time period. The thread again reenters into the runnable state
as soon as this time period is elapsed.
2.When a thread is suspended using suspend() method for some time in order to satisfy
some conditions. A suspended thread can be revived by using resume() method.
3.When wait() method is called on a thread to wait for some time. The thread in wait state
can be run again using notify() or notifyAll() method.
Blocked State
• A thread is considered to be in the blocked state when it is suspended, sleeping,
or waiting for some time in order to satisfy some condition.
Dead State
• A thread dies or moves into dead state automatically when its run() method
completes the execution of statements. That is, a thread is terminated or dead
when a thread comes out of run() method. A thread can also be dead when the
stop() method is called.
• During the life cycle of thread in Java, a thread moves from one state to another
state in a variety of ways. This is because in multithreading environment, when
multiple threads are executing, only one thread can use CPU at a time.
• All other threads live in some other states, either waiting for their turn on CPU
or waiting for satisfying some conditions. Therefore, a thread is always in any
of the five states.
Life cycle of a Thread (Thread States)

• In Java, a thread always exists in any one of the following states.


These states are:
1.New
2.Active
3.Blocked / Waiting
4.Timed Waiting
5.Terminated
• New: Whenever a new thread is created, it is always in the new state.
For a thread in the new state, the code has not been run yet and thus
has not begun its execution.
• Active: When a thread invokes the start() method, it moves from the
new state to the active state. The active state contains two states within
it: one is runnable, and the other is running.
• Runnable: A thread, that is ready to run is then moved to the runnable state.
In the runnable state, the thread may be running or may be ready to run at any
given instant of time. It is the duty of the thread scheduler to provide the
thread time to run, i.e., moving the thread the running state.
A program implementing multithreading acquires a fixed slice of time to each
individual thread. Each and every thread runs for a short span of time and
when that allocated time slice is over, the thread voluntarily gives up the CPU
to the other thread, so that the other threads can also run for their slice of
time. Whenever such a scenario occurs, all those threads that are willing to
run, waiting for their turn to run, lie in the runnable state. In the runnable
state, there is a queue where the threads lie.
• Running: When the thread gets the CPU, it moves from the runnable to the
running state. Generally, the most common change in the state of a thread is
from runnable to running and again back to runnable.
• Blocked or Waiting: Whenever a thread is inactive for a span of time
(not permanently) then, either the thread is in the blocked state or is in
the waiting state.
• Timed Waiting: Sometimes, waiting for leads to starvation. For
example, a thread (its name is A) has entered the critical section of a
code and is not willing to leave that critical section. In such a scenario,
another thread (its name is B) has to wait forever, which leads to
starvation. To avoid such scenario, a timed waiting state is given to
thread B.
• Terminated: A thread reaches the termination state because of the
following reasons:
• When a thread has finished its job, then it exists or terminates
normally.
• Abnormal termination: It occurs when some unusual events such as
an unhandled exception or segmentation fault.
• A terminated thread means the thread is no more in the system. In
other words, the thread is dead, and there is no way one can respawn
(active after kill) the dead thread.
Thread priorities
• Java assigns to each thread a priority that determines how that thread
should be treated with respect to the others.
• Thread priorities are integers that specify the relative priority of one
thread to another.
• As an absolute value, a priority is meaningless; a higher-priority
thread doesn’t run any faster than a lower-priority thread if it is the
only thread running.
• Instead, a thread’s priority is used to decide when to switch from one
running thread to the next. This is called a context switch.
The rules that determine when a context switch takes place are
simple:

• A thread can voluntarily relinquish control. This is done by explicitly


yielding, sleeping, or blocking on pending I/O. In this scenario, all
other threads are examined, and the highest-priority thread that is
ready to run is given the CPU.
• A thread can be pre-empted by a higher-priority thread. In this case, a
lower-priority thread that does not yield the processor is simply pre-
empted—no matter what it is doing— by a higher-priority thread.
Basically, as soon as a higher-priority thread wants to run, it does. This
is called pre-emptive multitasking.
Synchronization
• Because multithreading introduces an asynchronous behaviour to your programs,
there must be a way for you to enforce synchronicity when you need it.
• For example, if you want two threads to communicate and share a complicated data
structure, such as a linked list you need some way to ensure that they don’t conflict
with each other
• That is, you must prevent one thread from writing data while another thread is in
the middle of reading it. For this purpose, Java implements an elegant twist on an
age-old model of inter process synchronization: the monitor.
• The monitor is a control mechanism first defined by C.A.R. Hoare. You can think
of a monitor as a very small box that can hold only one thread. Once a thread enters
a monitor, all other threads must wait until that thread exits the monitor. In this
way, a monitor can be used to protect a shared asset from being manipulated by
more than one thread at a time.
• Java, there is no class “Monitor”; instead, each object has its
own implicit monitor that is automatically entered when one
of the object’s synchronized methods is called. Once a thread
is inside a synchronized method, no other thread can call any
other synchronized method on the same object.
Messaging
• After you divide your program into separate threads, you need to define
how they will communicate with each other.
• When programming with some other languages, you must depend on the
operating system to establish communication between threads. This, of
course, adds overhead.
• By contrast, Java provides a clean, low-cost way for two or more threads to
talk to each other, via calls to predefined methods that all objects have.
• Java’s messaging system allows a thread to enter a synchronized method
on an object, and then wait there until some other thread explicitly
notifies it to come out.
The Thread Class and the Runnable Interface

• Java’s multithreading system is built upon the Thread class, its


methods, and its companion interface, Runnable.
• Thread encapsulates a thread of execution. Since you can’t directly
refer to the ethereal state of a running thread, you will deal with it
through its proxy, the Thread instance that spawned it.
• To create a new thread, your program will either extend Thread or
implement the Runnable interface.
The Thread class defines several methods that help manage threads.
Several of those used in this chapter are shown here:
The Main Thread
When a Java program starts up, one thread begins running immediately. This is usually called the
main thread of your program, because it is the one that is executed when your program begins. The
main thread is important for two reasons:
• It is the thread from which other “child” threads will be spawned (created).
• Often, it must be the last thread to finish execution because it performs various shutdown actions.
• Although the main thread is created automatically when your program is started, it can be
controlled through a Thread object. To do so, you must obtain a reference to it by calling the
method currentThread( ), which is a public static member of Thread.
• Its general form is shown here:
static Thread currentThread( )
• This method returns a reference to the thread in which it is called. Once you have a reference to
the main thread, you can control it just like any other thread.
// Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
Current thread: Thread[main,5,main]
try { After name change: Thread[My Thread,5,main]
for(int n = 5; n > 0; n--) { 5
System.out.println(n); 4
3
Thread.sleep(1000);
2
} 1
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
• The sleep( ) method causes the thread from which it is called to suspend execution
for the specified period of milliseconds. Its general form is shown here:
static void sleep(long milliseconds) throws InterruptedException
• The number of milliseconds to suspend is specified in milliseconds. This method
may throw an InterruptedException.

final void setName(String threadName)


final String getName( )
Here, threadName specifies the name of the thread.
Creating a Thread

• In the most general sense, you create a thread by instantiating an object of type
Thread.
• You can implement the Runnable interface.
• You can extend the Thread class, itself.
Implementing Runnable

• The easiest way to create a thread is to create a class that implements the Runnable
interface.
• Runnable abstracts a unit of executable code. You can construct a thread on any
object that implements Runnable.
• To implement Runnable, a class need only implement a single method called run( ),
which is declared like this:
public void run( )
• Inside run( ), you will define the code that constitutes the new thread. It is important
to understand that run( ) can call other methods, use other classes, and declare
variables, just like the main thread can.
• The only difference is that run( ) establishes the entry point for another, concurrent
thread of execution within your program. This thread will end when run( ) returns.
• After you create a class that implements Runnable, you will instantiate an object of
type Thread from within that class. Thread defines several constructors. The one
that we will use is shown here:
Thread(Runnable threadOb, String threadName)
• In this constructor, threadOb is an instance of a class that implements the Runnable
interface. This defines where execution of the thread will begin. The name of the new
thread is specified by threadName.
• After the new thread is created, it will not start running until you call its start( )
method, which is declared within Thread.
• In essence, start( ) executes a call to run( ). The start( ) method is shown here:
void start( )
Here is an example that creates a new thread and starts it running:
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread // This is the entry point for the second thread.
} public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
Output:
Continued……. Child thread: Thread[Demo Thread,5,main]
class ThreadDemo { Main Thread: 5
Child Thread: 5
public static void main(String args[ ] ) {
Child Thread: 4
new NewThread(); // create a new thread Main Thread: 4
try { Child Thread: 3
Child Thread: 2
for(int i = 5; i > 0; i--) {
Main Thread: 3
System.out.println("Main Thread: " + i); Child Thread: 1
Thread.sleep(1000); Exiting child thread.
Main Thread: 2
} Main Thread: 1
} catch (InterruptedException e) { Main thread exiting.
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Extending Thread

• The second way to create a thread is to create a new class that extends
Thread, and then to create an instance of that class. The extending
class must override the run( ) method, which is the entry point for the
new thread. It must also call start( ) to begin execution of the new
thread.
// Create a second thread by extending Thread class ExtendThread {
class NewThread extends Thread { public static void main(String args[]) {
NewThread() {
new NewThread(); // create a new thread
try {
// Create a new, second thread for(int i = 5; i > 0; i--) {
super("Demo Thread"); System.out.println("Main Thread: " + i);
System.out.println("Child thread: " + this); Thread.sleep(1000);
}
start(); // Start the thread
} catch (InterruptedException e) {
} System.out.println("Main thread interrupted.");
// This is the entry point for the second thread. }
System.out.println("Main thread exiting.");
public void run() {
}
try { }
for(int i = 5; i > 0; i--) { Output:
System.out.println("Child Thread: " + i); Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Thread.sleep(500); Child Thread: 5
Child Thread: 4
} Main Thread: 4 Notice the call to super( ) inside NewThread. This
Child Thread: 3 invokes the following form of the
} catch (InterruptedException e) { Child Thread: 2 Thread constructor:
System.out.println("Child interrupted."); Main Thread: 3 public Thread(String threadName)
Child Thread: 1 Here, threadName specifies the name of the thread.
} Exiting child thread.
Main Thread: 2
System.out.println("Exiting child thread."); Main Thread: 1
Main thread exiting.
}
Creating Multiple Threads
• So far, you have been using only two threads: the main // This is the entry point for thread.
thread and one child thread. public void run() {
• However, your program can spawn as many threads as it try {
needs. For example, the following program creates three for(int i = 5; i > 0; i--) {
child threads: System.out.println(name + ": " + i);
// Create multiple threads. Thread.sleep(1000);
}
class NewThread implements Runnable { } catch (InterruptedException e) {
String name; // name of thread System.out.println(name + "Interrupted");
Thread t; }
System.out.println(name + " exiting.");
NewThread(String threadname) { }
name = threadname; }
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
Continued… New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
class MultiThreadDemo { New thread: Thread[Three,5,main]
One: 5
public static void main(String args[]) {
Two: 5
new NewThread("One"); // start threads Three: 5
new NewThread("Two"); One: 4
new NewThread("Three"); Two: 4
try { Three: 4
// wait for other threads to end One: 3
Three: 3
Thread.sleep(10000);
Two: 3
} catch (InterruptedException e) { One: 2
System.out.println("Main thread Interrupted"); Three: 2
} Two: 2
System.out.println("Main thread exiting."); One: 1
} Three: 1
Two: 1
}
One exiting.
Two exiting.
Three exiting.
Main thread exiting.
Using isAlive( ) and join( )
• As mentioned, often you will want the main thread to finish last. In the preceding
examples, this is accomplished by calling sleep( ) within main( ), with a long enough
delay to ensure that all child threads terminate prior to the main thread
• How can one thread know when another thread has ended? Fortunately, Thread
provides a means by which you can answer this question.
• Two ways exist to determine whether a thread has finished. First, you can call
isAlive( ) on the thread.
• This method is defined by Thread, and its general form is shown here:
• The isAlive( ) method returns true if the thread upon which it is called is still
running. It returns false otherwise.
• While isAlive( ) is occasionally useful, the method that you will more commonly
use to wait for a thread to finish is called join( ), shown here:
final void join( ) throws InterruptedException
• This method waits until the thread on which it is called terminates. Its name
comes from the concept of the calling thread waiting until the specified thread
joins it.
• Additional forms of join( ) allow you to specify a maximum amount of time that
you want to wait for the specified thread to terminate.
preceding example that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates
the isAlive( ) method.
// Using join() to wait for threads to finish. // This is the entry point for thread.
class NewThread implements Runnable { public void run() {
try {
String name; // name of thread for(int i = 5; i > 0; i--) {
Thread t; System.out.println(name + ": " + i);
NewThread(String threadname) { Thread.sleep(1000);
}
name = threadname; } catch (InterruptedException e) {
t = new Thread(this, name); System.out.println(name + " interrupted.");
}
System.out.println("New thread: " + t);
System.out.println(name + " exiting.");
t.start(); // Start the thread }
} }
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: " + ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
Continued……..
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
• New thread: Thread[One,5,main] One: 2
• New thread: Thread[Two,5,main] Two: 2
Three: 2
• New thread: Thread[Three,5,main]
One: 1
• Thread One is alive: true Two: 1
• Thread Two is alive: true Three: 1
Two exiting.
• Thread Three is alive: true Three exiting.
• Waiting for threads to finish. One exiting.
• One: 5 Thread One is alive: false
Thread Two is alive: false
• Two: 5 Thread Three is alive: false
• Three: 5 Main thread exiting.
As you can see, after the calls to join( ) return, the threads have
• One: 4
stopped executing
• Two: 4
• Three: 4
• One: 3
• Two: 3
• Three: 3
• Sometimes, suspending execution of a thread is useful. For example, a separate
thread can be used to display the time of day. If the user doesn’t want a clock, then
its thread can be suspended. Whatever the case, suspending a thread is a simple
matter. Once suspended, restarting the thread is also a simple matter.
• The suspend( ) method of the Thread class was deprecated by Java 2 several
years ago. This was done because suspend( ) can sometimes cause serious system
failures.
• The resume( ) method is also deprecated. It does not cause problems, but cannot
be used without the suspend( ) method as its counterpart.
• The stop( ) method of the Thread class, too, was deprecated by Java 2. This was
done because this method can sometimes cause serious system failures.
• Assume that a thread is writing to a critically important data structure and has
completed only part of its changes. If that thread is stopped at that point, that data
structure might be left in a corrupted state. The trouble is that stop( ) causes any
lock the calling thread holds to be released. Thus, the corrupted data might be used
by another thread that is waiting on the same lock.
• thread must be designed so that the run( ) method periodically checks to determine
whether that thread should suspend, resume, or stop its own execution. Typically,
this is accomplished by establishing a flag variable that indicates the execution state
of the thread. As long as this flag is set to “running,” the run( ) method must
continue to let the thread execute. If this variable is set to “suspend,” the thread must
pause. If it is set to “stop,” the thread must terminate. Of course, a variety of ways
exist in which to write such code, but the central theme will be the same for all
programs.
// Suspending and resuming a thread the modern way. // This is the entry point for thread.
class NewThread implements Runnable { public void run() {
try {
String name; // name of thread for(int i = 15; i > 0; i--) {
Thread t; System.out.println(name + ": " + i);
boolean suspendFlag; Thread.sleep(200);
synchronized(this) {
NewThread(String threadname) { while(suspendFlag) {
name = threadname; wait();
t = new Thread(this, name); }}}}
catch (InterruptedException e) {
System.out.println("New thread: " + t); System.out.println(name + " interrupted.");
suspendFlag = false; }
System.out.println(name + " exiting.");
t.start(); // Start the thread
}
} synchronized void mysuspend() {
suspendFlag = true;
}
synchronized void myresume() {
suspendFlag = false;
notify();
}
}
} catch (InterruptedException e) {
class SuspendResume { System.out.println("Main thread Interrupted");
public static void main(String args[]) { }
// wait for threads to finish
NewThread ob1 = new NewThread("One"); try {
NewThread ob2 = new NewThread("Two"); System.out.println("Waiting for threads to finish.");
ob1.t.join();
try {
ob2.t.join();
Thread.sleep(1000); } catch (InterruptedException e) {
ob1.mysuspend(); System.out.println("Main thread Interrupted");
}
System.out.println("Suspending thread One"); System.out.println("Main thread exiting.");
Thread.sleep(1000); }
}
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
Obtaining A Thread’s State

• a thread can exist in a number of different states. You can obtain the current state
of a thread by calling the getState( ) method defined by Thread. It is shown here:
Thread.State getState( )
• It returns a value of type Thread.State that indicates the state of the thread at the
time at which the call was made. State is an enumeration defined by Thread.
• An enumeration is a list of named constants.
Thread.State ts = thrd.getState();
if(ts == Thread.State.RUNNABLE) // ...
• It is important to understand that a thread’s state may change after the call to getState( ). Thus, depending on
the circumstances, the state obtained by calling getState( ) may not reflect the actual state of the thread only a
moment later.

You might also like