MultiThreading
MultiThreading
MultiThreading
Chapter 11
Multithreading
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
2
Thread
• Thread: single sequential flow of control within a
program
• Single-threaded program can handle one task at
any time.
• Multithreading allows two or more parts that
can run concurrently.
• Multithreading is specialized form of
multitasking.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
3
Multitasking
• Multitasking is the ability of a computer's
operating system to run several programs (or
processes) concurrently on a single CPU.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
4
Multitasking
• Process based multitasking (heavyweight tasks)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
5
Multitasking
• Thread based multitasking(Lightweight tasks)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
6
Advantages of Multithreading
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
7
Threads Concept
Multiple
threads on
multiple
CPUs
Multiple
threads
sharing a
single CPU
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
8
Thread States
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
9
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
10
Thread Priorities
Java Assigns priority to each thread. Thread priorities are integers.
•MIN_PRIORITY (a constant of 1)
•MAX_PRIORITY (a constant of 10).
•NORM_PRIORITY (a constant of 5, default).
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
11
Threads in Java
Creating threads in Java:
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
12
Extending Thread
Follow below steps to create thread by extending
Thread class,
1. Extend Sub class by Thread class
2. Override run( ) method of Thread class inside sub
class
3. Create sub-class object in main() in another class
4. Call start( ) method by sub-class object
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
13
Example
class thrExample extends Thread
{
public void run()
{
System.out.println("---- Execution by Child Thread ----");
}
}
public class Demo
{
public static void main(String [] args)
{
thrExample t1 = new thrExample();
thrExample t2 = new thrExample();
t1.start();
t2.start();
}
}
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
14
Example
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
15
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
16
Methods
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
17
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
20
Example
class threadRun implements Runnable {
public void run() {
System.out.println("Thread is executing using runnable
interface");
}
}
Class Demo {
public static void main(String[] args) {
threadRun tr = new threadRun();
Thread t = new Thread(tr);
Thread t1 = new Thread(tr);
Thread t2 = new Thread(tr);
t.start();
t1.start();
t2.start();
}
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
} rights reserved. 0136012671
21
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
22
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
23
Main Thread
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
24
Synchronization
• When two or more threads needs to access shared data, then
data inconsistency problem arises. Imagine a scenario,
▫ Thread T1 is writing data into a file numbers.txt.
▫ Thread T2 is reading data from a file numbers.txt.
• In this scenario, when T1 is writing, if T2 reads the file
concurrently, data may not be correct. Hence, when one
thread is using shared resource, other threads should not be
allowed to use until first thread completes the task.
• In Java, synchronization is achieved using monitor or lock.
• Each object has unique lock associated with them.
• A thread that needs to access shared resource, first acquires
the lock, as soon as completes the execution, thread
automatically releases the lock.
• To synchronize the function, prefix the keyword
synchronized in function definition
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
25
Synchronized Blocks
synchronized(Object){
//Statements to be synchronized
}
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
26
Synchronized Blocks
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
27
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
28
wait( )
• The wait method releases the lock of current thread and moves it to waiting
state. Thread will awake, when other thread calls notify( ) or notifyAll( )
method for the object, or till specified time elapses.
Prototype:
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
29
notify( )
•Notifies the thread in waiting state on the same object to wake up.
Prototype:
1. public final void notify( )
notifyAll( )
•Notifies all threads in waiting state on the same object to wake up.
Prototype:
1. public final void notifyAll( )
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
30
• Now thread goes to waiting state if you call wait() method on the
object. Otherwise it releases the lock and exits.
• After completion of the task, thread releases the lock and exits the
monitor state of the object.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
31
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
32
• The producer should go to sleep when buffer is full. Next time when
consumer removes data it notifies the producer and producer starts
producing data again. The consumer should go to sleep when buffer is
empty. Next time when producer add data it notifies the consumer and
consumer starts consuming data. This solution can be achieved using
semaphores.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
33
Solution
The producer is to either go to sleep or discard data if the buffer is full. The
next time the consumer removes an item from the buffer, it notifies the
producer, who starts to fill the buffer again. In the same way, the consumer
can go to sleep if it finds the buffer to be empty. The next time the producer
puts data into the buffer, it wakes up the sleeping consumer.
•Refer notepad for example
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
34
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671