CS3135/CS2135
Object Oriented Programming (Java)
BSCS-3 / MCS-3
Lecture # 20
Multi Threading
Concurrency
Thread
• A portion of a program that can run independently of and concurrently with other portions of the
program.
• Multithreading is the ability to do multiple things at once within the same application.
• Individual and separate unit of execution that is part of a process.
• Multiple threads can work together to accomplish a common goal
Multi Threading
• It is easy to confuse multithreading with multitasking or multiprogramming, which are somewhat
different ideas.
• Multithreading is the ability of a program or an operating system process to manage its use by more
than one user at a time and to even manage multiple requests by the same user without having
multiple copies of the program running in the computer.
• Java includes built-in support for threading.
Instructor: Tanzila Kehkashan
2
MultiThreading
Advantages
• Easier to program. (1 thread per task)
• Can provide better performance
• Thread only runs when needed
• No polling to decide what to do
• Multiple threads can share resources
• Utilize multiple processors if available
Disadvantages
• Multiple threads can lead to deadlock
• Overhead of switching between threads
Instructor: Tanzila Kehkashan
3
Thread States: Life Cycle of Thread
New
• Life cycle started. Just created but not started execution.
Runnable
• Created, started, and executing.
Waiting
• Sometimes a runnable thread transitions to waiting state while it
waits for another thread to perform a task. A waiting thread
transitions back to the runnable state only when another thread
notifies it to continue executing.
• Cannot use processor.
Timed Waiting
• Also called Sleep state.
• A runnable thread can enter timed waiting state for specified interval of time (sleep interval) . It transitions back to
runnable state when that time interval expires.
• Cannot use processor.
Blocked
• In block state, a runnable thread is unable to run because it is waiting for some event to occur. (for example I/O)
Dead / Terminated
• Runnable thread has successfully completed its task or otherwise terminates perhaps due to error.
Instructor: Tanzila Kehkashan
4
Multi-Threading
• There are two ways to create a new thread of execution.
• One is to declare a class to be a subclass of Thread.
• This subclass should override the run method of class Thread.
• An instance of the subclass can then be allocated and started.
• For example;
class myClass extends Thread
{
long minPrime;
myClass(long minPrime)
{
this.minPrime = minPrime;
}
public void run()
{
// compute primes larger than minPrime
. . .
}
}
myClass p = new myClass(143);
p.start();
Instructor: Tanzila Kehkashan
5
Multi-Threading
• The other way is to declare a class that implements the Runnable interface.
• That class then implements the run method.
• An instance of the class can then be allocated, passed as an argument when creating Thread, and
started.
• For example; class myClass implements Runnable
{
long minPrime;
PrimeRun(long minPrime)
{
this.minPrime = minPrime;
}
public void run()
{
// compute primes larger than minPrime
. . .
}
}
myClass obj = new myClass(143);
Thread t=new Thread(obj);
t.start(); //new Thread(obj).start();
Instructor: Tanzila Kehkashan
6
Multi-Threading
java.lang.Thread
• Thread()
• Thread(String) //name of thread
• long getId() //returns ID of thread
• String getName() //name of thread
• void run()
• static void sleep(long) //milliseconds //temporarily cease execution
• void start() //calls run method
Instructor: Tanzila Kehkashan
7
Instructor: Tanzila Kehkashan
8
Instructor: Tanzila Kehkashan
9

OOP Lecture 20-MultiThreading.pptx

  • 1.
    CS3135/CS2135 Object Oriented Programming(Java) BSCS-3 / MCS-3 Lecture # 20 Multi Threading
  • 2.
    Concurrency Thread • A portionof a program that can run independently of and concurrently with other portions of the program. • Multithreading is the ability to do multiple things at once within the same application. • Individual and separate unit of execution that is part of a process. • Multiple threads can work together to accomplish a common goal Multi Threading • It is easy to confuse multithreading with multitasking or multiprogramming, which are somewhat different ideas. • Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having multiple copies of the program running in the computer. • Java includes built-in support for threading. Instructor: Tanzila Kehkashan 2
  • 3.
    MultiThreading Advantages • Easier toprogram. (1 thread per task) • Can provide better performance • Thread only runs when needed • No polling to decide what to do • Multiple threads can share resources • Utilize multiple processors if available Disadvantages • Multiple threads can lead to deadlock • Overhead of switching between threads Instructor: Tanzila Kehkashan 3
  • 4.
    Thread States: LifeCycle of Thread New • Life cycle started. Just created but not started execution. Runnable • Created, started, and executing. Waiting • Sometimes a runnable thread transitions to waiting state while it waits for another thread to perform a task. A waiting thread transitions back to the runnable state only when another thread notifies it to continue executing. • Cannot use processor. Timed Waiting • Also called Sleep state. • A runnable thread can enter timed waiting state for specified interval of time (sleep interval) . It transitions back to runnable state when that time interval expires. • Cannot use processor. Blocked • In block state, a runnable thread is unable to run because it is waiting for some event to occur. (for example I/O) Dead / Terminated • Runnable thread has successfully completed its task or otherwise terminates perhaps due to error. Instructor: Tanzila Kehkashan 4
  • 5.
    Multi-Threading • There aretwo ways to create a new thread of execution. • One is to declare a class to be a subclass of Thread. • This subclass should override the run method of class Thread. • An instance of the subclass can then be allocated and started. • For example; class myClass extends Thread { long minPrime; myClass(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } myClass p = new myClass(143); p.start(); Instructor: Tanzila Kehkashan 5
  • 6.
    Multi-Threading • The otherway is to declare a class that implements the Runnable interface. • That class then implements the run method. • An instance of the class can then be allocated, passed as an argument when creating Thread, and started. • For example; class myClass implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } myClass obj = new myClass(143); Thread t=new Thread(obj); t.start(); //new Thread(obj).start(); Instructor: Tanzila Kehkashan 6
  • 7.
    Multi-Threading java.lang.Thread • Thread() • Thread(String)//name of thread • long getId() //returns ID of thread • String getName() //name of thread • void run() • static void sleep(long) //milliseconds //temporarily cease execution • void start() //calls run method Instructor: Tanzila Kehkashan 7
  • 8.
  • 9.