Unit 4 Final
Unit 4 Final
Topics : Multithreading
Introduction
Thread Life Cycle
Creating Thread
Thread Methods
Thread Synchronization
Introduction
Thread Characteristics:
New − A new thread begins its life cycle in the new state. It remains in this state until the
program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A thread
in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for
another thread to perform a task. A thread transitions back to the runnable state only
when another thread signals the waiting thread to continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for occurs. This state can be
achieved by sleep() method.
Terminated (Dead) − A runnable thread enters the terminated state when it completes
its task or otherwise terminates.
• The functionality that is expected by the Thread to be executed is written in the run()
method.
• void start(): Creates a new thread and makes it runnable.
• void run(): The new thread begins its life inside this
method. Example:
public class MyThread extends
Thread { public void run(){
System.out.println("thread is
running...");
}
public static void main(String[]
args) { MyThread obj = new
MyThread(); obj.start();
}
By Implementing Runnable interface
• The class should implement the Runnable interface
• The class should implement the run() method in the Runnable interface
• The functionality that is expected by the Thread to be executed is put in the
run() method Example:
public class MyThread implements Runnable
{ public void run()
{
System.out.println("thread is running..");
}
public static void main(String[] args)
{
Thread t = new Thread(new MyThread());
t.start();
}
Thread Methods
Method Description
String getName() Retrieves the name of running thread in the current context in
String format
void setName(String s) Setting thread name
Thread.State It returns the state of the thread.
getState()
void start() This method will start a new thread of execution by calling run()
method of Thread/runnable object.
void run() This method is the entry point of the thread. Execution of thread
starts from this method.
void join() This method used to queue up a thread in execution. Once
called on thread, current thread will wait till calling thread
void sleep(int time) This method suspend the thread for mentioned time duration in
argument (sleeptime in milliseconds)
void yield() By invoking this method the current thread pause its execution
temporarily and allow other threads to execute.
void wait() when wait() method is invoked on an object, the thread
executing that code gives up its lock on the object immediately
and moves the thread to the wait state.
void notify() This wakes up threads that called wait() on the same object and
moves the thread to ready state.
void notifyAll() This wakes up all the threads that called wait() on the same
object.
void interrupt() Interrupts this thread.
boolean interrupted() Tests whether the current thread has been interrupted.
Thread Scheduling
• Execution of multiple threads on a single CPU, in some order, is called scheduling.
• In general, the runnable thread with the highest priority is active (running)
• Java is priority-preemptive. If a high-priority thread wakes up, and a low-priority
thread is running then the high-priority thread gets to run immediately Allows on-
demand processing.
• Efficient use of CPU
Thread Synchronization
• Synchronization in java is the capability to control the access of multiple threads to
any shared resource.
• Java Synchronization is better option where we want to allow only one thread to
Compiled By : Tulsi Shah Page 7
US05CBCA25 OOP-III UNIT - 4
Types of Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread
communication.
1. Mutual Exclusive : Mutual Exclusive helps keep threads from interfering with
one another while sharing data. This can be done by three ways in java:
1.1). Synchronized method.
1.2). Synchronized block.
1.3). static synchronization.
2. Cooperation (Inter-thread communication in java) : Cooperation (Inter-thread
communication) is a mechanism in which a thread is paused running in its critical
section and another thread is allowed to enter (or lock) in the same critical section
to be executed.
} }
Disclaimer: The study material is compiled by Ms. Tulsi Shah. The basic objective of this
material is to supplement teaching and discussion in the classroom in the subject. Students are
required to go for extra reading in the subject through Library books recommended by Sardar
Patel University, Vallabh Vidyanagar. Students should also consult the subject teacher for the
solution of their problems in order to enhance their subject knowledge.
Question Bank
MCQs
1. Which are the methods of the object class?
A. notify(); C. wait(long msces);
B. notifyAll(); D. All of them
2. Which will contain the body of the thread?
A. run(); C. start();
B. wait(); D. terminate();
3. Which is the name of the method used to start a thread execution?
A. wait(); C. terminate();
B. start(); D. notify();
4. Which class or interface defines the wait(), notify() and notifyAll() methods?
A. Runnable C. Object
B. Thread D. None of Above
5. What are the two valid constructor for Thread?
A. Thread() , Thread(int priority) C. Thread(), Thread(Runnable r, String
name)
B. Thread() ,Thread(Runnable r, D. Thread(), Thread(int pripority)
ThreadGroup g)
6. Which is not the method defined in class Thread?
A. wait(): C. start();
B. run(); D. stop();
7. Which method of the thread class is used to find out the priority given to a thread?
A. get(); C. getPriority();
B. threadPriority(); D. getThreadPriority();
8. What is the output in the following thread java program?
Class thread_example
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
System.out.println(t.isAlive());
}
}
A. 0 C. false
B. 1 D. true
9. Which function of pre defined class Thread is used to check if current thread being
checked is still running?
A. join(); C. alive();
B. isRunning(); D. isAlive();
10. When a class extends the thread class, it should override ____________ method of thread
class to start the thread.
A. start(); C. run();
B. go(); D. init();
11. In java a thread can be created by ___________
A. Extended the thread class C. Implementing Runnable
interface.
Short Question
1) What is Java Thread?
2) Write down Thread characteristics.
3) List Thread implementation in java. Explain any one.
[ or explain Java Thread implementation By extending thread class / By Implementing
Runnable interface]
4) List Java Thread Methods.
5) Define the following.[Any]
a. String getName()
b. void setName(String s)
c. Thread.State getState()
d. void start()
e. void run()
f. void join()
g. boolean isAlive()
h. void sleep(int time)
i. void yield()
j. void wait()
k. void notify()
l. void notifyAll
m. void interrupt()
n. boolean interrupted()
o. void setPriority(int p)
p. int getPriority()
6) What is Thread Scheduling?
Long Question
1) 1. What is Threads? Which are the Characteristics of Thread and why we use Thread?
2) 2. Draw Life Cycle of a Thread and explain in detail.
3) 3. Explain Thread creation in Java.
4) 4. List Thread methods and explain any five.
5) 5. Explain Thread Synchronization and how to Implement synchronization with example.