Chapter-5 Multithreading in Java
Chapter-5 Multithreading in Java
METTU UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
Java programming course
Chapter-5
Multithreading Concepts in Java
By: Naol G. 2
Introduction
Human body performs variety of operations in parallel/concurrently.
What is Multithreading
Multitasking allows CPU to perform multiple tasks (process, threads)
simultaneously
Advantage of Multithreading
Responsiveness
• Multithreading an interactive application may allow a program to
continue running even if part of it is blocked or doing a lengthy
operation.
Resource Sharing
• Threads share the memory and the resources(code, data, and
files)of the process to which they belong.
Apart from this main thread, we can also create our own
threads in a program that is called child thread.
1. Newborn State:
• When a thread object is created a new thread is born and said to
be in Newborn state.
• Eg.: MyThread t1 = new MyThread();
2. Runnable State:
• If a thread is in this state it means that the thread is ready for
execution and waiting for the availability of the processor.
• If all threads in queue are of same priority then they are given
time slots for execution in round robin fashion.
03/03/2022 By: Naol G. 9
Cont…
3. Running State:
• It means that the processor has given its time to the thread for
execution.
• A thread keeps running until the following conditions occurs.
Eg.: After t1.start(); ,if thread scheduler allocate processor, automatically start()
will invoke run() method.
• Thread give up its control on its own and it can happen in the following
situations.
• A thread gets suspended using suspend() method which can only be
revived with resume() method.
• A thread is made to sleep for a specified period of time using sleep(time)
method, where time in milliseconds.
• A thread is made to wait for some event to occur using wait()method. In
this case a thread can be scheduled to run again using notify()method
• A thread is pre-empted by a higher priority thread
03/03/2022 By: Naol G. 10
…
4. Blocked State:
• If a thread is prevented from entering into runnable state and
subsequently running state, then a thread is said to be in Blocked
state.
• Eg.: t1.sleep(500); or t1.suspend(); or t1.wait();
5. Dead State:
Creating Threads
In the most general sense, you can create a thread by
instantiating an object of type Thread.
Extending Thread
One way to create a thread is to create a new class that extends
Thread, and then to create an instance of that class.
The extending class must override the run() method, which is the
entry point for the new thread.
Note:
• Everything inside run() is called job of the thread.
• mt.start(); - create new thread and execute run()
• mt.run(); - only execute run() as normal program execution
• start() method is considered as the ‘heart’ of multithreading concept.
• Register the thread with thread scheduler
• Invoke run() method
By: Naol G. 16
Implementing Runnable
The second way to create a thread is to create a class that
implements the Runnable interface.
After you create a class that implements Runnable, you will instantiate
an object of type thread from within that class.
Next, after the new thread is created, it will not start running until you
call its start( )method, which is declared within Thread.
• Why:
• We will have advantage of multiple inheritance benefit.
• Eg.:
class MyThreadOne extends MyThreadTwo implements Runnable
{
//code here
}
By: Naol G. 20
Controlling Threads
Controlling threads is the art of moving them from one state to another.
• Starting a thread:
• Once a Thread has been created it is started by calling start() method.
• The start() method puts the thread into the ready state.
• To start a thread we use the following syntax: thread.start();
• Stopping a thread:
• To stop a thread from running further, we may do so by calling its stop() method.
• This causes a thread to stop immediately and move it to its dead state.
• It forces the thread to stop abruptly before its completion
• It causes premature death.
• To stop a thread we use the following syntax: thread.stop();
By: Naol G. 22
Controlling Threads…
Blocking Threads:
• A thread can also be temporarily suspended or blocked from
entering into the runnable and subsequently running state,
• 1. sleep(t) // blocked for ‘t’ milliseconds
• 2. suspend() // blocked until resume() method is invoked
• 3. wait() // blocked until notify () is invoked
Threads Priority
Every Java thread has a thread priority that helps the operating
system to determine the order in which threads are scheduled
priorities range between:
• MIN_PRIORITY (a constant of 1)
• NORM_PRIORITY (a constant of 5, default) and
• MAX_PRIORITY (a constant of 10)
Each new thread inherits the priority of the thread that created it.
• Note: In case two threads have the same priority, the JVM will execute them in FIFO
order.
Thread Synchronization
When we start two or more threads within a program, there may be
a situation when multiple threads try to access the same resource
• Then they can produce unexpected result due to concurrency issues.
For example,
• if multiple threads try to write within a same file then they may corrupt the data
because
• one of the threads can override data or
• while one thread is opening the same file at the same time another thread
might be closing the same file.
synchronized(objectidentifier){
// Access shared variables and other shared resources;
}
Reading:
End
Thank you