Multithreading
Multithreading
….
public void main(..) begin start
start start
{
… body
..
end Thread A Thread B Thread C
}
}
Threads may switch or exchange data/results
5 6
1
How does it all work?
Each thread is given its own "context"
Java Threads
A thread's context includes virtual registers and its own calling stack
• The "scheduler" decides which thread executes at any given time • Java has built in thread support for Multithreading
The VM may use its own scheduler • Synchronization
Since many OSes now directly support multithreading, the VM may use
the system's scheduler for scheduling threads • Thread Scheduling
• The scheduler maintains a list of ready threads (the run queue) and a list of • Inter-Thread Communication:
threads waiting for input (the wait queue)
– currentThread start setPriority
Each thread has a priority. The scheduler typically schedules between the – yield run getPriority
highest priority threads in the run queue
Note: the programmer cannot make assumptions about how threads are – sleep stop suspend
going to be scheduled. Typically, threads will be executed differently on – resume
different platforms.
• Java Garbage Collector is a low-priority thread
java.lang.Thread
• Two techniques to create threads in java
• 1) implementing the Runnable interface
An example
– The Runnable interface should be implemented by any class whose instances
are intended to be executed by a thread. The class must define a method,
called run, with no arguments. class MyThread extends Thread { // the thread
– invoke Thread constructor with an instance of this Runnable class public void run() {
• 2) extending Thread System.out.println(" this thread is running ... ");
– Define a subclass of java.lang.Thread }
• Define a run method } // end class MyThread
– In another thread (e.g., the main), create an instance of the Thread subclass
• Then, call start method of that instance class ThreadEx1 { // a program that utilizes the thread
public static void main(String [] args ) {
MyThread t = new MyThread();
// due to extending the Thread class (above)
// I can call start(), and this will call
// run(). start() is a method in class Thread.
t.start();
} // end main()
} // end class ThreadEx1
9 10
11 12
2
Life Cycle of Thread A Program with Three Java Threads
notify()
stop()
slept
resume()
dead unblocked
13 14
Run 1 Run2
• [test@root] threads [1:76] java ThreadTest • [raj@mundroo] threads [1:77] java ThreadTest
From ThreadA: i= 1 From ThreadA: i= 1
From ThreadA: i= 2 From ThreadA: i= 2
From ThreadA: i= 3 From ThreadA: i= 3
From ThreadA: i= 4 From ThreadA: i= 4
From ThreadA: i= 5 From ThreadA: i= 5
Exit from A From ThreadC: k= 1
From ThreadC: k= 1 From ThreadC: k= 2
From ThreadC: k= 2 From ThreadC: k= 3
From ThreadC: k= 3 From ThreadC: k= 4
From ThreadC: k= 4 From ThreadC: k= 5
From ThreadC: k= 5 Exit from C
Exit from C From ThreadB: j= 1
From ThreadB: j= 1 From ThreadB: j= 2
From ThreadB: j= 2 From ThreadB: j= 3
From ThreadB: j= 3 From ThreadB: j= 4
From ThreadB: j= 4 From ThreadB: j= 5
From ThreadB: j= 5 Exit from B
Exit from B Exit from A
17 18
3
the driver: 3rd Threads sharing the same
Shared Resources object
• If one thread tries to read the data and other thread class InternetBankingSystem {
tries to update the same date, it leads to inconsistent public static void main(String [] args ) {
state. Account accountObject = new Account ();
• This can be prevented by synchronising access to Thread t1 = new Thread(new MyThread(accountObject));
data. Thread t2 = new Thread(new YourThread(accountObject));
Thread t3 = new Thread(new HerThread(accountObject));
• In Java: “Synchronized” method: t1.start();
– syncronised void update() t2.start();
–{ t3.start();
• … // DO some other operation
–} } // end main()
}
19 20
for(int i=1;i<=4;i++)
threads so far had same default priority {
System.out.println("\t From ThreadA: i= "+i);
}
(ORM_PRIORITY) and they are served using FCFS System.out.println("Exit from A");
}
policy. }
23 24
4
Thread Priority Example
class C extends Thread
{
public void run()
{
System.out.println("Thread C started");
for(int k=1;k<=4;k++)
{
System.out.println("\t From ThreadC: k= "+k);
}
System.out.println("Exit from C");
}
}
class ThreadPriority
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Started Thread A");
threadA.start();
System.out.println("Started Thread B");
threadB.start();
System.out.println("Started Thread C");
threadC.start();
System.out.println("End of main thread");
25 }
}