1 MultiThreading
1 MultiThreading
Objectives
What are Threads?
Interrupting threads
Thread properties
Threads priorities
Synchronization
Callables and Futures
Threads and Swing
1
HO CHI MINH UNIVERSITY OF INDUSTRY
is termed as Multithreading.
• Appears that the processes are running
2
HO CHI MINH UNIVERSITY OF INDUSTRY
are created.
• Program is terminated when main thread
stops execution.
• Main thread can be controlled through
Thread objects.
• Reference of the main thread can be
Why Multithreading?
Increases performance of single-processor
system, as it reduces the CPU idle time.
Multithreading encourages faster execution of
a program
Multithreading introduces the concept of
parallel processing
4
HO CHI MINH UNIVERSITY OF INDUSTRY
Creating Thread
Two ways:
1. Declare a class that is a sub-class of the
class Thread defined in java.lang
package:
Creating Thread
6
HO CHI MINH UNIVERSITY OF INDUSTRY
Creating Thread
7
HO CHI MINH UNIVERSITY OF INDUSTRY
Creating Thread
8
HO CHI MINH UNIVERSITY OF INDUSTRY
Running Thread
After a new thread has been initiated, we use
the start() method to start the thread
Otherwise it is an empty Thread object with no
Running Thread
Output
10
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Properties
• Thread States :
– New
– Runnable
– Blocked
– Dead
11
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Properties
12
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Properties
13
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Properties
14
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Properties
Output
15
HO CHI MINH UNIVERSITY OF INDUSTRY
16
HO CHI MINH UNIVERSITY OF INDUSTRY
situation.
17
HO CHI MINH UNIVERSITY OF INDUSTRY
18
HO CHI MINH UNIVERSITY OF INDUSTRY
Daemon Thread
• Two types of threads in Java:
1. User threads:
• Created by the user
2. Daemon threads:
• Threads that work in the background
providing service to other threads
(e.g. – the garbage collector thread)
19
HO CHI MINH UNIVERSITY OF INDUSTRY
Daemon Thread
• When user thread exits, JVM checks to
find out if any other thread is running.
– If there are, it will schedule the next
thread.
– If the only executing threads are
daemon threads, it exits.
• We can set a thread to be a Daemon if we
do not want the main program to wait until
a thread ends.
20
HO CHI MINH UNIVERSITY OF INDUSTRY
• “isAlive()" Method
– Use to find out whether a specific thread is
running or not.
• “join()" Method
– Causes the current thread to wait until the thread
on which it is called terminates.
– Allows specifying the maximum amount of time
that the program should wait for the particular
thread to terminate.
– It throws InterruptedException if another thread
interrupts it.
– The calling thread waits until the specified thread
terminates.
21
HO CHI MINH UNIVERSITY OF INDUSTRY
Interrupting Thread
• There is no longer a way to force a thread
to terminate.
• The interrupt method can be used to
request termination of a thread.
• Checking one thread is interrupted:
Thread.currentThread().isInterrupted()
• If a thread is blocked, it cannot check the
interrupted status. This is where the
InterruptedException comes in.
22
HO CHI MINH UNIVERSITY OF INDUSTRY
Interrupting Thread
public void run()
{ Pattern for interrupting an thread
try{ . . .
while (more work to do){
do more work
}
}
catch(InterruptedException exception){
// thread was interrupted during sleep or wait
}
finally{
cleanup, if required
}
// exit run method and terminate thread
}
23
HO CHI MINH UNIVERSITY OF INDUSTRY
Thread Synchronization
• What happens if two threads have access to
the same object and each calls a method
that modifies the state of the object?
• In such a case, data may become
inconsistent.
• Situation is often called a race condition.
• To avoid simultaneous access of a shared
object by multiple threads, you must learn
how to synchronize the access.
24
HO CHI MINH UNIVERSITY OF INDUSTRY
26
HO CHI MINH UNIVERSITY OF INDUSTRY
28
HO CHI MINH UNIVERSITY OF INDUSTRY
29
HO CHI MINH UNIVERSITY OF INDUSTRY
30
HO CHI MINH UNIVERSITY OF INDUSTRY
31
HO CHI MINH UNIVERSITY OF INDUSTRY
32
HO CHI MINH UNIVERSITY OF INDUSTRY
notify()
wakes up or
notifies the
first thread.
notifyAll() Thread 2
notifyAll()
wakes up or Thread 1
notifies all the
Thread 3
threads that
called wait( ) on
the same object. Java Simplified / Session 16 / 33 of 32
33
HO CHI MINH UNIVERSITY OF INDUSTRY
Example :
public static synchronized getInstance()
35
HO CHI MINH UNIVERSITY OF INDUSTRY
37
HO CHI MINH UNIVERSITY OF INDUSTRY
38
HO CHI MINH UNIVERSITY OF INDUSTRY
39
HO CHI MINH UNIVERSITY OF INDUSTRY
40
HO CHI MINH UNIVERSITY OF INDUSTRY
41
HO CHI MINH UNIVERSITY OF INDUSTRY
42
HO CHI MINH UNIVERSITY OF INDUSTRY
44
HO CHI MINH UNIVERSITY OF INDUSTRY
Deadlocks
Analyzing following situation
47
HO CHI MINH UNIVERSITY OF INDUSTRY
Deadlocks
• If all threads in an application are blocked. The
system has deadlocked.
• Unfortunately, there is nothing in the Java
programming language to avoid or break these
deadlocks.
• You must design your threads to ensure that a
deadlock situation cannot occur.
• Notify/notifyAll method can unblock thread(s).
48
HO CHI MINH UNIVERSITY OF INDUSTRY
50
HO CHI MINH UNIVERSITY OF INDUSTRY
51
HO CHI MINH UNIVERSITY OF INDUSTRY
52
HO CHI MINH UNIVERSITY OF INDUSTRY
53
HO CHI MINH UNIVERSITY OF INDUSTRY
54
HO CHI MINH UNIVERSITY OF INDUSTRY
55
HO CHI MINH UNIVERSITY OF INDUSTRY
END
56