We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
MULTITHREADING AND
GENERIC PROGRAMMING
41_DIFFERENCES BETWEEN MULTI-THREADING AND MULTITASKING
Multi-Threading
Multi-Tasking
Multitasking is to run multiple processes on al
computer concurrently,
Multithreading is to execute multiple
threads in a process concurrently,
Multithreading is light-weight and casy
Multitasking is heavy-weight and harder tol
to create. create,
InMultithreading, resources are shared
among multiple threads in a Process,
In Multithreading, the CPU switches
between multiple threads in the same
process,
In Multitasking, resources are shared among|
multiple processes,
In Multitasking, the CP
'U switches between|
multiple processes to complete the execution,
_|Each process have its own address in
memory i.e. each process allocates
separate memory area.
‘Threads share the same address space,
4.2 THREAD LIFE CYCLE
-Y — Athread goes through various stages in its life cycle,
For example, a thread is born, started, runs, and then dies.
The life cycle of the thread in java is controlled by JVM.
The java thread states are as follows:
Thread State Description
A thread that has not begun execution.
IRUNNABLE A thread that either is currently executing or will execute when it
gains access to the CPU. ____
(BLOCKED A thread that has suspended execution because it is waiting to
| acquire a lock.
A thread that has suspended execution because it is waiting a
some action to occur. For example, it is waiting because ofa ca
to anon-timeout version of wait() orjoin().a Object Orienteg Prog
Thread State
ided execution foraspoy
TIMED_WAITING | A thread that has suspen Speciticg
- time, suchas when thas called sleep), This sei oe
when atimeout version of wait() orjoin( is cates 2%
Waiting
or
(Blocked JO Runnable J
Lock acquired
Thread ends
Y — Givena Thread instance, you can use getState( ) to obtain the state of a thre:
Thread.State ts= thrd.getStateQ;
if(tts= Thread.State,RUNNABLE) Wa
Note:
Y — Thread state may change at any time during the execution so we can't able?
the exact state of thread, Hence
“_I’sprimarily used for debugging or for profiling a thread’s run-time character
4.3_ CREATING THREADS
Java defines two ways in which this can be accomplished:
+ Implementing the Runnable interface,
+ Extending the Thread class,ert:
Multithreading and Generic Programming 4
eee
43.4 Create Thread by Implementing Runnable
v
ai
The easiest way to create a thread is to create a clas!
Runnable interface,
To implement Runnable, a class need only implement a
run( ), which is declared like this:
public void run()
You will define the code that constitutes the new
Itis important to understand that tun() can call other methods, use other
and declare variables, just like the main thread can, After you create a
implements Runnable, you will instantiate an object of type,
Thread from within that class,
The one that we will use is shown here:
8 that implements the
single method o:
thread inside runt) method,
thas
Thread defines several constructors,
Thread(Runnable threadOb, String thread Name);
Here threadOb is an instance of a class that implements the BR:
interface and the name of the new thread is. specified by threadName,
unnable
After the new thread is created, it will not start running until you call its start )
method, which is declared within Thread. The start( ) method is shown here: void
start().
Example Program:
class NewThread implements Runnable
{
Thread t;
NewThread(Q)
{
// Create a new, second thread :
t=new Thread(this, “Demo Thread”), _
System.outprintIn(“Child thread:“+);
tstart(); // Start the thread3
public void run() {
try
{ .
for(int i = 5; i > 0; i--)
f
t
System.out.printIn(“Child Thread: “ + i);
// Let the thread sleep for a while,
Thread.sleep(500);
3
3
catch (InterruptedException e} ) £
System.out.println(“Child interrup
3
System.out.printIn(“Exiting child thread.”);
3
y
5
class ThreadDemo {
Public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i=5; i> 0; i--)
{
System.out.printin(“Main Thread: “+ j);
Thread.sleep(i 000);
}
} catch CnterruptedException e){
' System. out printin“Main thread interrupted.”);
System.out printin(“Main thread exiting.”);