Documente Academic
Documente Profesional
Documente Cultură
1
Java
Contents
2
Java
Contents
11 Lifecycle
12 sleep()
13 join()
14 Interruption
15 Thread priorities
16 Selfish threads
17 IllegalArgumentException
18 Synchronization
19 Solution to the Account problem
20 Locking
3
Java
Contents
21 Synchronized Code
22 A few more points !
23 Deadlocks
24 Inter-thread communication
25 Methods of Object class used for thread communication
26 wait() and notify()
27 Daemon threads
28 Challenge your mind
4
Java
Know
• What threads are
• The difference between thread and
process
• How to create threads in java
5
Java
Know
• The Thread life cycle
• The Thread class methods sleep(),
interrupt(), yield() and join() do
• Thread priority
• Synchronization
• Inter-Thread-Communication
• About daemon threads
6
Java
Be Able To
• Implement threads java programs
7
Java
Thread in a simple java program
8
Java
Concurrent execution
// include all necessary imports
public class PrintQuestionPaper{
10
Java
Multitasking is multiprocessing
…
A process is a thread….
I am confused!
12
Java
I never knew one processor can
execute multiple tasks
simultaneously !
13
Java
Others places where threads play
important roles
• In client-server based systems, the server
program creates threads that allows it to
respond to multiple users at the same time.
• GUI programs have a separate thread to
gather user’s interface events from the host
OS.
• Do other things while waiting for slow I/O
operation.
• Animations
14
Java
Syntax for creating Thread
or java.lang.Thread
class SimpleThread extends Thread {
public void run(){
1. Inherit from
/* code that is executed when the Thread
class
thread executes */
2. Override run method
}} Creates thread
Calls run() method
Thread t= new SimpleThread();
t.start();
15
Java
class Thread{…
public void start(){
// create OS level thread
run(); }
public void run(){…} 2
}
class SimpleThread {
…
public void run(){}}
SimpleThread c= new SimpleThread();
c.start();
1
16
Java
(It’s my turn to ask now.)
So, what will happen if you
call run() method instead of
start() method ? Think about
it. If you cannot answer this
now, wait. There is another
hint coming up shortly.
17
Java
Complete Example
class PrimeOddThread extends Thread {
int num;
public void run() {
if( prime())
System.out.println("Prime");
else System.out.println("Non-
Prime");
Folder 2
}
18
Java
public boolean prime(){
for(int i=2;i<num/2;i++){
if(num%i==0) return false;}
return true;}
public boolean odd(){
if(num%2==0) return false;
else return true;
}
19
Java
Thread no. 2
21
Java
Another way to create thread
class SimpleThreadR implements
Runnable{
public void run(){…}
} Calls run method of the
Runnable instance
Creation of thread – using a
passed via constructor
constructor that expects a
Thread t= Runnable object
Folder 3
24
Java
Can you list down all
the methods and
constructors of
Thread class we
have covered so far?
25
Java
Naming threads
• Using Constructors :
Thread(Runnable target, String name)
• Methods :
final void setName(String name)
final String getName()
• To find out which thread is running right
now:
Thread.currentThread().getName();
static Thread currentThread()
returns the current thread.
26
Java
Lifecycle
yield() After run()
method
start() run() completes
new ready running dead
sleep(), wait()
Waiting for IO device
Waiting /
Up to the thread scheduler to pick a Blocked
thread for execution from ready state.
27
Java
If I start two threads one after
another like the code below, then
can we guarantee that the t1 thread
will execute first and then the t2?
psvm(){
Thread t1= new
Thread(new
SimpleThreadR() );
Thread t2= new
Thread(new
SimpleThreadR() );
t1.start();
t2.start();}
28
Java
After t1 has been created there are
two active threads - main thread and
t1. Now let us say that thread
scheduler chooses t1. In that case
what you said is right- t1 executes
before t2. But now let us say main
executes, then t2 gets created. So,
now we have three threads- main, t1,
t2. If thread scheduler chooses t2 then
here is a chance that t2 executes
before t1!
29
Java
sleep()
• static void sleep(long millis) throws
InterruptedException
• static void sleep(long millis,
int nanos) throws InterruptedException
• Example:
• public class Appear implements
Runnable{
• char c[]={‘H',‘E',‘L',‘L‘,’O’};
• public void run() {
Folder 4
30
Java
int i=0;
try{
while (i<5){
System.out.print(c[i++]);
Thread.sleep(1000);
}
}catch(InterruptedException e){}
}
public static void main(String str[]){
Thread t =new Thread(new Appear());
t.start();}} 31
Java
join()
32
Java
class Join implements Runnable{
int num;
public void run() {
if( prime())System.out.print("Prime");
else System.out.print("Non-Prime");
}
public boolean prime(){
for(int i=2;i<num/2;i++){
if(num%i==0) return false;}
Folder 5
33
return true; Java }
public boolean odd(){
if(num%2==0)return false;
else return true; }
public static void main(String str[]){
Join s=new Join();
Thread t= new Thread(s);
s.num=55;
t.start();
boolean b=s.odd();
34
Java
main thread waits for t
try{ t.join(); to finish
}
catch(InterruptedException e){
System.out.println("main interrupted");}
if(b)
System.out.print(" and Odd");
else
System.out.print("and Even");
} }
35
Java
Are there any chances of you
letting us know why sleep() and
join() methods throw this
InterruptedException.
36
Java
Interruption
• A thread can be interrupted by calling interrupt ()
method on it.
• If this thread is blocked on account of sleep() or join()
methods, then when interrupt method is called, its
interrupt status will be cleared and it will receive an
InterruptedException.
void interrupt()
static boolean interrupted()
boolean isInterrupted()
37
Java
Why will I want a thread
to be interrupted ?
40
Java
Illegal Argument Exception
• This is a RunTimeException and it indicates that
a method has been passed an illegal or
inappropriate argument.
• The setPriority(int newPriority)method can
take values between 1 to 10. Any other int value
will result in IllegalArgumentException
exception
41
Java
Synchronization
Thread A calls withdraw(500)
Account
Thread B calls withdraw(750) money: Rs.1000
Thread A waits for the transaction to complete
Thread B waits for the transaction to complete
Code…
42
Java
public class ThreadTest implements Runnable{
Account a;
int amt;
public static void main(String str[]){
Account lb= new Account(1000);
new ThreadTest(lb,"A",500);
new ThreadTest(lb,"B",750);}
public ThreadTest(Account a,String name,int
amt){
this.a=a;
Folder 7
this.amt=amt;
new Thread(this,name).start();} 43
Java
public void run(){ a.withdraw(amt);}}
class Account{
private int money;
Account(int amt){ money=amt;}
void deposit(int amt){
try{
// doing io operation
Thread.sleep(1000); }
catch(Exception e){}
money=money+amt;
System.out.println("Balance "+ money);} 44
Java
void withdraw(int amt){
if(amt<money){
try{
// doing io operation
Thread.sleep(1000); }
catch(Exception e){}
money=money-amt;
System.out.println("Received "+ amt +" by " +
Thread.currentThread().getName());
}
else 45
Java
System.out.println("Sorry "+
Thread.currentThread().getName()+ "Requested
amt ("+ amt +") is not available.");
System.out.println("Balance "+ money);
}
}
Result:
Received 500 by A
Balance 500
Received 750 by B
Balance -250
46
Java
Solution to the Account problem
Account
Thread A locks the object and calls withdraw(500) money:
Thread B waits for A to release the lock Rs.1000
Thread A waits for the transaction to complete
Thread A updates the money to (1000- Rs 500
500)=500 and releases the lock.
47
Java
Locking
• In java, the object gets locked in two situations:
• When a thread calls a synchronized method of an
object. In this case, lock is released as soon as
the method finishes its execution. No other thread
can access any synchronized methods of that
object, till the lock on the object is released.
synchronized void deposit(int amt)
48
Java
B. When an object is explicitly locked by the
statement
synchronized(object){… }
In such cases, all the methods of the object
remain locked until the block of statement
within synchronized block finishes execution.
49
Java
Synchronized Code
• Solution using approach A:
Add synchronized keyword to deposit and withdraw
methods of the Account class, compile and execute.
synchronized void deposit(int amt) synchronized
void withdraw(int amt)
•Solution using approach B:
Change the run method of ThreadTest class to
public void run(){
synchronized(a){
a.withdraw(amt); } }
Folder 8
50
Java
Result :
Received 500 by A
Balance 500
Sorry B Requested amt (750) is
not available.
Balance 500
51
Java
So what about static
methods? Can they be
synchronized?
52
Java
A few more points !
• synchronized methods of super class can be
overridden to be unsynchronized.
• Constructors cannot be declared as synchronized.
• A non-static inner class can lock it’s containing
class using a synchronized block.
• Methods in the interface cannot be declared as
synchronized.
• The locking does not prevent threads from
accessing unsynchronized methods.
• Synchronized methods are also called thread-safe
methods.
53
Java
Do you recall I told you StringBuffer
is thread-safe. Using StringBuffer
doesn’t allow you to take full
advantage of the multithreading. So,
if you are sure that your threads can
simultaneously call methods of same
string without causing unnecessary
side effects – use StringBuilder.
54
Java
Deadlocks
55
Java
Inter-thread communication
• Inter-thread communication is required when
execution of one thread depends on another thread.
In such case, the second thread intimates or
notifies the first thread when it has finished what the
first thread is waiting for.
• The best suited situation to understand this is a
producer-consumer problem.
consumer
Producer
56
Java
Methods of Object class used for
thread communication
final void wait() throws InterruptedException
final void wait(long timeout) throws
InterruptedException
The above methods causes current thread to wait until either
another thread invokes the notify() method or the notifyAll()
method for this object, or a specified amount of time has
elapsed.
57
Java
wait() and notify()
• Both of these methods must be called from a
synchronized context. If this is not done
IllegalMonitorStateException is thrown at
runtime.
• When a thread calls wait() method, it relinquishes
the lock on the object (unlike sleep method).
• When a thread calls notify() method, the lock is not
released.
• Another word frequently used for lock is monitor.
58
Java
public class ProducerConsumer implements
Runnable{
int apples;
public static void main(String s[]){
ProducerConsumer pc = new ProducerConsumer();
Thread producer= new Thread(pc, "Tree");
Thread consumer1= new Thread(pc, "Earthworm");
Thread consumer2= new Thread(pc, "Man");
producer.start();
consumer1.start();
consumer2.start();}
59
Java
synchronized void produce(){
while(true){
if(apples>100) Produce apples only if there
are <=100 apples
try{
System.out.println("Waiting for apples to be
eaten");
wait(); }catch(InterruptedException e){}
try{
int i=(int)(Math.random()*500); Produce apples
Thread.sleep(i);
apples=apples+i; Simulating time taken to
produce apples. 60
Java
System.out.println("Produced apples ="+ apples);
}catch(InterruptedException e){}
notifyAll(); } }
Wake all the consumers
64
Java
Challenge your mind
65
Java
class A extends Thread {
String sa;
public A(String sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while(!sa.equals("Done")){
try{sa.wait();}
catch(Exception e){}
}
}
System.out.println(sa);
}
}
66
Java
class B {
private static String sa = new
String("Not Done");
public static void main (String[]
args) {
synchronized (sa) {
Thread t1 = new A(sa);
t1.start();
sa="Done";
sa.notify();
}
}}
On executing the above code, it throws
IllegalMonitorStateException . Why?
67
Java
Thread Groups
• Thread groups are collection of threads into one object .
• This helps in manipulating a collection of threads all at a
time instead of manipulating each of them individually .
• For instance all the threads in a collection can be started
together.
• Every Java thread is a member of some thread group.
When a thread is created, unless it is explicitly put in a
thread group, the runtime system automatically places
the new thread in the same group as the thread that
created it.
• When a Java application first starts up, the Java runtime
system creates a ThreadGroup named "main".
68
Java
Creating the ThreadGroup
• ThreadGroup(String name)
• ThreadGroup(ThreadGroup parent,
String name)
69
Java
Methods of a ThreadGroup
• Methods to enumerate:
– int enumerate(Thread[] list)
– int enumerate(Thread[] list,
boolean recurse)
• Methods with respect to the group
– int activeCount()
– ThreadGroup getParent()
– String getName()
– boolean parentOf(ThreadGroup g)
70
Java
class EnumeratedThreads {
public static void main(String s[]) {
ThreadGroup currentGroup =
Thread.currentThread().getThreadGroup();
int no;
Thread listOfThreads[];
no = currentGroup.activeCount();
listOfThreads = new Thread[no];
currentGroup.enumerate(listOfThreads);
for (int i = 0; i < no; i++) {
System.out.println("Thread #" +
i + " = " + listOfThreads[i].getName());
}
}} 71
Java
Methods to with respect to the
threads as a group
• int getMaxPriority()
• void setMaxPriority(int pri)
• void interrupt()
• void destroy()
• boolean isDaemon()
• void setDaemon(boolean daemon )
• void list()
72
Java
class ThreadGroupDemo{
public static void main (String [] args){
ThreadGroup tg = new ThreadGroup ("group
1");
Thread t1 = new Thread (tg, "thread 1");
Thread t2 = new Thread (tg, "thread 2");
Thread t3 = new Thread (tg, "thread 3");
tg = Thread.currentThread
().getThreadGroup ();
73
Java
int agc = tg.activeGroupCount ();
System.out.println ("Active thread
groups in " + tg.getName () +" thread
group: " + agc);
tg.list ();
}
}