Tasks and Multithreading
Tasks and Multithreading
MULTITHREAD
ING
LIFECYCLE
OF A
THREAD
STATES OF A THREAD IN
JAVA
Structure
VARIAT • static, dynamic
IONS Level
IN THE • top level tasks only
TASK (flat)
• multilevel (nested)
MODEL Initialization
• with or without
parameter passing
Granularity
• fine or coarse grain
VARIAT
Termination
IONS • natural, suicide
IN THE • abortion, untrapped error
TASK • never, when no longer
needed
MODEL Representation
• fork/join, cobegin, explicit
task declarations
Granularity — coarse (Ada,
Java), fine (occam2)
oamount of work performed by that task
ousually measured in terms of the number
of instructions executed, or
oThe ratio of computation time to
GRANUL communication time
oFine-grained - a program is broken down
ARITY to a large number of small tasks
oCoarse-grained - a program is split into
large tasks
oDiscussion:
oWhat is the impact of granularity to
computation and communication time?
Termination
completion of execution of the task body;
suicide, by execution of a self-terminate
statement;
abortion, through the explicit action of
Allows individual
Multithreading
programs to perform
extends the concept
several tasks
of multitasking.
concurrently.
A multithreaded program
must be very careful about
the way that threads access
and modify data, or else
unpredictable behaviour
may occur.
HOW TO CREATE
THREADS IN JAVA
SUBCLA // ...
SSING
THE public void run() {
// All code to be executed within the
THREAD thread goes here.
CLASS }
}
Create a new thread by instantiating the
SUBCLA class, and we run it by calling the start()
method inherited from class Thread.
SSING Technically OK, conceptually does not
THE make much sense
MyClass "is a" Thread?
THREAD We just need a run() method that the Thread
CLASS
class can execute.
SUBCLA
SSING MyClass a = new MyClass();
THE a.start();
THREAD
CLASS
Write a class that implements the
Runnable interface.
The Runnable interface requires us
IMPLEMENTING THE RUNN to implement a single method
ABLE INTERFACE named run(), within which we
place all code that is to be executed
within the thread.
class MyClass implements Runnable
{
// ...
void start()
THREAD
This method will start a new thread of
execution by calling run() method of
Thread/runnable object.
METHO void run()
DS IN This method is the entry point of the thread.
Execution of thread starts from this method.
JAVA void sleep(int sleeptime)
This method suspends the thread for mentioned
time duration in argument (sleeptime in ms)
void interrupt()
Interrupts this thread.
void yield()
By invoking this method the current thread
pause its execution temporarily and allow other
threads to execute.
void join()
THREAD This method is used to queue up a thread in
execution. Once called on thread, current
METHO thread will wait till calling thread completes its
execution.
DS IN boolean isAlive()
JAVA This method will check if thread is alive or
dead.
ON
class A extends Thread {
public void run() {
for (int i=1; i<=5; i++) {
EXAMP if (i==2) yield();
YIELD() }
System.out.println(“Exit from A”);
}
}
class B extends Thread {
public void run() {
EXAMP }
System.out.println(“C: ” + i);
LE:
System.out.println(“Exit from C”);
}
}
SLEEP() class yieldTest {
public static void main(String[] args) {
C c = new C();
c.start();
}
}
Every thread has a priority.
The OS refers to the priority level to
schedule the threads.
PRIORIT c.setPriority(1);
Y c.start();
a.start();
}
}
EXAMP isAlive() tests if a thread is alive.
LE: A thread is alive if it has been started and has
not yet died.
ISALIVE
A a = new A();
a.start();
try {
() AND }
a.join();
JOIN() }
catch (InterruptedException e) {}
System.out.println(“Status: ” + isAlive());
}
class MyClass2 implements Runnable{
@Override
public void run() {
Thread t = Thread.currentThread();
LE: try {
Thread.sleep(4000);
EXAMP
/* Start second thread(th2) once first thread(th1) is dead */
try { th1.join();
LE:
} catch (InterruptedException ie) { ie.printStackTrace(); }
th2.start();
/* Start third thread(th3) once second thread(th2) is dead */