Multithreading
Multithreading
MultiThreading
Main thread in Java
Que:
1. Define Thread in Java.
2. Define thread. State two ways to create a thread.
3. Explain ‘Extending Thread class’ in Java.
4. What is Thread? List different methods used to create Thread. Explain Thread life cycle.
Ans:
Java provides built-in support for multithreaded programming. A multi-threaded program
contains two or more thread that can run concurrently.
Definition:
Thread can be called lightweight process. Thread requires less resources to create and
exists in the process, thread shares the process resources.
When a Java program starts up, one thread begins running immediately. This is usually
called the main thread of our program, because it is the one that is executed when our
program begins.
Properties :
It is the thread from which other “child” threads will be spawned.
Often, it must be the last thread to finish execution because it performs various shutdown
actions.
Creating Thread
To implement Runnable interface, a class need only implement a single method called
run( ), which is declared like this:
public void run( );
Inside run( ), we will define the code that constitutes the new thread.
To execute the run() method by a thread, pass an instance of ABC to a Thread in its
constructor.
Page 1 of 11
Example:
class ABC implements Runnable
{
public void run()
{
System.out.println("ABC running");
}
}
class ThreadRunnable
{
Public static void main(String args[])
{
ABC ob1=new ABC();
Thread t1 = new Thread(ob1);
t1.start();
}
}
When the thread is started it will call the run() method of the MyClass instance instead of
executing its own run() method.
The above example would print out the text “ABC running“.
class ThreadExtend
{
public static void main(String args[])
Page 2 of 11
{
ABC t1 = new ABC();
t1.start();
}
}
When the run() method executes it will print out the text “ABC running“.
Multiple Threads
Our program can affect as many threads as it needs. Let’s see how we can create multiple
threads.
ABC(String str)
{
name=str;
Thread t1=new Thread(this);
t1.start();
}
public void run()
{
try
{
for(int i=0;i<5;i++)
{
System.out.println(name + " is Running: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Thread is Interrupted"+e);
}
}
}
class multiThread
{
Page 3 of 11
public static void main(String args[])
{
ABC ob1=new ABC("Thread1");
ABC ob2=new ABC("Thread2");
String name;
ABC(String str)
{
name=str;
start();
try
{
for(int i=0;i<5;i++)
{
}
}
catch(InterruptedException e)
{
System.out.println("Thread is Interrupted"+e);
}
}
}
class multiThread
{
public static void main(String args[])
{
ABC ob1=new ABC("Thread1");
Page 4 of 11
ABC ob2=new ABC("Thread2");
}
}
Que:
1. Explain life cycle of a thread.
2. Explain life cycle of thread in Java.
3. Describe the complete life cycle of a thread.
Ans:
Following diagram shows Life Cycle of Thread.
New:
o The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
Runnable:
o The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread.
Running:
o The thread is in running state if the thread scheduler has selected it.
Waiting:
o 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.
Terminated:
o A runnable thread enters the terminated state when it completes its task or
otherwise terminates.
Thread Priority
Que:
Page 5 of 11
1. Explain thread priorities with suitable example.
2. List different thread priorities.
3. How do we set priorities for thread?
Ans:
1. public static int MIN_PRIORITY: This is minimum priority that a thread can have. Value for
this is 1.
2. public static int NORM_PRIORITY: This is default priority of a thread if do not explicitly
define it. Value for this is 5.
3. public static int MAX_PRIORITY: This is maximum priority of a thread. Value for this is 10.
class ThreadExtend
{
public static void main(String args[])
{
ABC t1 = new ABC();
t1.start();
System.out.println(t1.getPriority());
Page 6 of 11
t1.setPriority(2);
System.out.println(t1.getPriority());
System.out.println(Thread.currentThread().getName());
}
Synchronization
Que:
1. Explain Synchronization in Thread.
2. What is Synchronization? When do we use it?
3. Explain thread synchronization.
Ans:
When two or more threads need access to a shared resource,they need some way to
ensure that the resource will be used by only one thread at a time. The by which this is
achieved is called Synchronization.
There are two way to achieve synchronizarion
Example:
class XYZ
{
synchronized public void display(String name)
{
try
{
for(int i=0;i<5;i++)
{
Thread.sleep(1000);
}
}
catch(InterruptedException e)
Page 7 of 11
{
System.out.println("Thread is Interrupted"+e);
}
}
}
String name;
XYZ syob1;
syob1.display(name);
}
}
class SyncMethodThread
{
public static void main(String args[])
{
XYZ syob1=new XYZ();
class XYZ
{
public void display(String name)
{
try
{
for(int i=0;i<5;i++)
{
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Thread is Interrupted"+e);
}
}
}
String name;
XYZ syob1;
synchronized(syob1)
{
syob1.display(name);
Page 9 of 11
}
}
}
class SyncThread
{
public static void main(String args[])
{
XYZ syob1=new XYZ();
}
}
Method Description
Page 10 of 11
\
Page 11 of 11