Chapter-9-Introduction to Thread
Chapter-9-Introduction to Thread
Introduction to Thread
2018
2
Multitasking ASTU
4
Cont.. ASTU
5
Multithreading in java ASTU
8
Thread state ASTU
10
Java Thread Example by extending Thread class
ASTU
15
Example of sleep method in java ASTU
The sleep() method of Thread class is used to sleep a thread for the specified
amount of time.
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
System.out.print(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start(); Output:
} 1122334
} 4C
16
Call Run method Using start vs Using objectASTU
class TestCallRun2 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestCallRun2 t1=new TestCallRun2();
TestCallRun2 t2=new TestCallRun2();
t1.run();
t2.run(); Output :
12341234
}
}
17
Join method in a thread class ASTU
t2.start(); Output:1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
t3.start();
}
}
18
Change Thread name ASTU
t1.start();
t2.start();
t1.setName("CSE Thread");
System.out.println("After changing name of t1:"+t1.getName());
}
}
19
Multiple task with multiple Thread ASTU
class TestMultitasking3{
public static void main(String args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();
t1.start(); Output:
t2.start(); Task one
} task two
}
20
Synchronization in Java ASTU
21
Thread Synchronization ASTU
22
with out synchronized method ASTU
} 23
with synchronized method ASTU
} 24
Inter-thread communication in Java ASTU
25
Understanding the process of inter-thread communica-
ASTU
tion
The point to point explanation of the above diagram
is as follows:
• Threads enter to acquire lock.
• Lock is acquired by on thread.
• Now thread goes to waiting state if you call wait()
method on the object. Otherwise it releases the
lock and exits.
• If you call notify() or notifyAll() method, thread
moves to the notified state (runnable state).
• Now thread is available to acquire lock.
• After completion of the task, thread releases the
lock and exits the monitor state of the object.
26
Inter-Thread Communication example ASTU
class Customer{
class Test{
int amount=10000; public static void main(String
synchronized void withdraw(int amount){
args[]){
System.out.println("going to withdraw..."); final Customer c=new Custome
if(this.amount<amount){
r();
new Thread(){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
} public void run()
this.amount-=amount;
{c.withdraw(15000);} }.start();
System.out.println("withdraw completed...");
} new Thread(){
synchronized void deposit(int amount){ public void run()
System.out.println("going to deposit..."); {c.deposit(10000);} }.start();
this.amount+=amount;
System.out.println("deposit completed... "); }}
Output:
notify();
} going to withdraw...
} Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed...
27
ASTU