Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

Page no- 1

UNIT-4, THREADS

What is a Thread
“A thread is a lightweight sub process, and the smallest unit of processing”. It is a separate path of
execution. A thread is similar to a program that has a single flow of control. It has a beginning, a
body, and an end, and executes commands sequentially. In fact main program in our programs
can be called as Thread.

Threads in java are subprograms of a main application program, they can share the same
memory space, and saves the CPU time, they are known as Lightweight Processes or Lightweight
Threads.
All the threads are running on a single processor, the flow of execution is shared between the
threads.
Benefits of threads:
 It enables the programmers to do multiple things at one time.

 This approach will improve the speed of our programs.


 Any application requires two or more things to be done at the same time are
probably a best one to use is thread.

Multi thread (or) Main Thread

Main method
module

star star star


t t t

switchin switchin
g g

Thread A Thread B Thread C


Fig: A multithreaded program
The main thread is actually the main thread module, which is designed to create and start the
other threads, namely A, B, and C. Once initiated by the main thread, the threads A, B, and C

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 2

concurrently and share the resources jointly.

Q). Explain about single tasking using a Thread?

A thread can be employed to execute one task at a time. Suppose there are 3 taks to be executed.
We can create a thread and pass the 3 tasks one by one to the thread.

For this purpose, we can write all these tasks separately in separate methods: task1(), task2(),
task3(). Then these methods should be called from run() method, one by one.

Remember, a thread executes only the code inside the run() method. It can never execute other
methods unless they are called i run ().

Program: Create single tasking use a thread.

class MyThread implements Runnable


{
public void run()
{
//execute the tasks one by one by calling the methods.
task1():
task2():
task3():
}

void task1()
{
System.out.println ("This is taks 1");
}

void task2()
{
System.out.println ("This is task 2");
}

void taxk3()
{
System.out.println("This is task 3");
}

class Single
{
public static void main(String args[])
{
MyThread obj= new MyThread();
Thread t1=new Thread(obj);
}
}
}
Output:
This is taks1
This is taks2

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 3

This is taks3

Q). Explain about Multi tasking using Threads

In multi tasking, several tasks are executed at a time. For this purpose, we need more than one
thread. For example, to perform 2 tasks, we can take 2 threads and attach them to the 2 tasks.
Then those tasks are simultaneously executed by the two threads. Using more than one thread is
called 'multi threading’.

When we go to a movie theatre, generally a person is there at the door-checking and cutting the
tickets. When we enter the hall, there is another person who shows the seats to us. Suppose there
is only one person (1 thread) doing these two tasks. He has to first cut the ticket and then come
along with us to show the seat. Then he goes back to the door to cut the second ticket and then
again enter the hall to show the seat for the second ticket. Like this, if he is does the things one by
one, it takes a lot of time, and even though the show is over, there will be still a few people left
outside the door waiting to enter the hall! This is pretty well known to the theatre management. So
what they do? They employ two persons (2 threads) for this purpose

The first person will cut the ticket, and the second one will show the seat. When the second person
is showing the seat, the first person cuts the second ticket. Like this, both the persons can act
simultaneously and hence there will be no wastage of time.

Program: Create a program showing two threads working simultaneously upon two objects.
//Two threads performing two tasks at a time-Theatre example
class MyThread implements Runnable
{
String str;
MyThreads(String str)
{
this.str=str;
}

public void run()


{
for(int i=1; i<10; i++)
{
System.out.println(str+":"+i);
try{
Thread.sleep(2000);
//cease thread execution for 2000 milliseconds
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
class Theatre
{
public static void main (String args[])
{
//create two objects to represent two tasks
MyThread obj1= new MyThread ("Cut the ticket");
MyThread obj2= new MyThread ("Show the seat");
//create two threads and attach them to the two objects

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 4

Thread t1= new Thread(obj1);


Thread t2= new Thread(obj2);
t1.start();
t2.start();
}
}
Output:

Cut the ticket: 1


Show the seat : 1
Cut the ticket: 2
Show the seat : 2
Cut the ticket: 3
Show the seat : 3
.......
Cut the ticket: 10
Show the seat : 10

Ways of Creating a Thread

New thread can be created in two ways.


1. By extending the class thread
2. By implementing the interface Runnable

1). Creating a new thread extending thread:

The first method of creating a thread is simply by extending the thread class. The Thread class is
defined in the package java.lang.Thread. This gives access to all the thread methods directly. It
includes the following steps:

1. Declare the class as extending the thread class.


2. Implement the run() method
3. Create a thread object and call the start() method

1. Declaring the Class: The Thread class can be extended as follows:

class MyThread extends Thread


{
……….
……….
……….
}

2. Implementing the run() Method :

The run() method has been inherited by the class MyThread. This method order to
implement the code to be executed by our thread. The basic implementation of run() will look
like this:

public void run()

{
………..
……….. // Thread code here

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 5

………..
}

When we start the new thread, Java calls the thread's run() method, so it is the run() method,
where all the action takes place.
3. Starting New Thread:

To actually create and run an instance of our thread class, we must write the following:
MyThread aThread = new MyThread( );
aThread. start(); // invokes run() method

The first line instantiates a new object of class MyThread. This statement just creates the
object. The thread is in a newborn state.
The second line calls the start() method causing the thread to move into the runnable state.

Example program:

import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5; i++)
{
System.out.println("\tFrom ThreadA: i=” +i);
}
System.out.println("Exit form A);
}
}

class B extends Thread


{
public void run()
{
for(int j=1; j<=5; j++)
{
System.out.println("\tFrom Thread B :j=” +j);
}
System.out.println("Exit from B ");
}
}

class ThreadTest
{

public static void main(String args[])


{
new A().start();
new B().start();
}
}

2). Creating a thread implementing Runnable interface:

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 6

The Runnable interface declares the run() method that is required for implementing threads in
our programs. To do this, we must perform the steps listed below.

1. Declare the class as implementing the Runnable interface.


2. Implement the run() method.
3. Create a thread by defining an object that is instantiated from this "runnable" class
as the target of the thread.
4. Call the thread's start() method to run the thread.

Example:
class XYZ implements Runnable
{
public void run()
{
for(int i=1;i<10; i++)
{
System.out.println("Thread XYZ:” +i);
}
System.out.println("End of ThreadXYZ");
}
}

class RunnableTest
{
public static void main(String args[])
{
XYZ runnable= new XYZ();
Thread tx=new Thread(runnable);
tx.start();
System.out.println(“End of main Thread”);
}
}

Q). Terminating a Thread? Explain.


A thread will terminate automatically when it comes out of run() method. To terminate the thread
on our own, we have to derive our own logic. For this purpose, the following steps can be used:

1. Create a boolean type variable and initialize it to false.

boolean stop=false;

2. Let us assume that we want to terminate the thread when the user presses <Enter> key. So,
when the user presses that button, make the boolean type variable as true

stop = true;

3. Check this variable in run() method and when it is true, make the thread return from the run()
method.

public void run()


{

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 7

if (stop ==true)
return;
}
Program : How to terminate the thread by pressing the Enter button.

//To create a thread and run it, then stop it


import java.io.*;
class MyThread extends Thread
{
boolean stop = false;
public void run()
{
for (int i=1;i<=100000; i++)
System.out.println (i);
if(stop) return; //come out of run ()
}
}

class Demo1
{
public static void main(String args[]) throws IOException
{
MyThread obj = new MyThread();
Thread t = new Thread (obj);
t.start();
System.in.read();
obj.stop()=true;
}
}

Output:

Q). Explain about Multiple Threads Acting a Single Object?


First let us see why 2 threads should share the same object (same run() method). We write an
object to represent one task. If there is a different task, we take another object. When two
people (threads) perform same task, then they need same object (run() method) to be executed
each time.

Take the case of railway reservation. Every day several people want reservation of a berth for
them. The procedure to reserve the berth is same for all the people. So we need same object
with same run() method to be executed repeatedly for all the people (threads).

Let us think that only one berth is available in a train, and two passengers (threads) are asking for
that berth. In reservation counter no.1, the clerk has sent a request to the server to allot that
berth to his passenger. In counter no.2, the second clerk has also sent a request to the server to
allot that berth to his passenger. Let us see now see to whom that berth is allotted.

Program: Create a program showing two threads acting upon a single object.

//Thread unsafe-Two threads acting on same object

class Reserve implements Runnable

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 8

{
int available=1;
int wanted;
Reserve (int i)
{
wanted = i;
}
public void run()
{
System out.println ("Available Berths="+available);
if(available>= wanted)
{
string name= Thread.currentThread(). getName();
System.out.println(wanted+" Berths reserved for "+name);
try{
Thread.sleep(1500); // wait for printing the ticket
available =available - wanted;
}
catch (InterruptedException ie)
{}
} else System.out.println ("Sorry, no berths");
}
}

class Unsafe
{
Reserve obj = new Reserve (1);
Thread t1= new Thread (obj);
Thread t2= new Thread(obj);
t1.setName ("First person");
t2.setName ("Second person");
t1.start();
t2.start();
}

Output:

Available Berths = 1
1 Berths reserved for First Person
Available Berths = 1
1 Berths reserved for Second Person

Q). What Is Thread Deadlock?


A deadlock occurs when two or more threads wait forever for a lock (or) resource held by
another of the threads. Consequently, an application may stall (or) fail as the deadlocked threads
cannot progress.

Deadlock Example

First, let's take a look into a simple Java example to understand deadlock. In this example, we'll
create two threads, T1 and T2.  Thread T1 calls operation1, and thread T2 calls operations.

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 9

To complete their operations, thread T1 needs to acquire lock1 first and then lock2, whereas


thread T2 needs to acquire lock2 first and then lock1. So, basically, both the threads are trying to
acquire the locks in the opposite order.

Now, let's write the DeadlockExample class:

1. public class TestDeadlockExample1 {  
2.   public static void main(String[] args) {  
3.     final String resource1 = "ratan tata";  
4.     final String resource2 = "vimal tata";  
5.         
6. Thread t1 = new Thread() {   // t1 tries to lock resource1 then resource2  
7.        public void run() {  
8.            synchronized (resource1) {  
9.             System.out.println("Thread 1: locked resource 1");  
10.   
11.              try { Thread.sleep(100);} 
12. catch (Exception e) {}  
13.    synchronized (resource2) {  
14.               System.out.println("Thread 1: locked resource 2");  
15.            }   }  }   };  
16.   
17.   Thread t2 = new Thread() {   // t2 tries to lock resource2 then resource1  
18.        public void run() {  
19.          synchronized (resource2) {  
20.            System.out.println("Thread 2: locked resource 2");  
21.   
22.            try { Thread.sleep(100);} 
23. catch (Exception e) {}  
24.            synchronized (resource1) {  
25.              System.out.println("Thread 2: locked resource 1");  
26.           }  }   }   };  
27.     t1.start();  
28.     t2.start();  
29.   }  }       

Once we run the program, we can see that the program results in a deadlock and never exits. The log
shows that thread T1 is waiting for lock2, which is held by thread T2. Similarly, thread T2 is waiting
for lock1, which is held by thread T1.

Avoiding Deadlock

Deadlock is a common concurrency problem in Java. Therefore, we should design a Java application
to avoid any potential deadlock conditions.

Q). Explain about thread communication?


In some cases, two or more threads should communicate with each other. For example, a
Consumer thread is waiting for a Producer to produce the data (or some goods). When the
Producer thread completes production of data, then the Consumer thread should take that data

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 10

and use it.

We improve the efficiency of communication between threads. Java.lang. Object class provides
3 methods for this purpose.

1. notify():

This method releases an object (obj) and sends a notification to a waiting thread that the
object is available.
Ex: public final void notify()  

2. notifyAll():
This method is useful to send notification to all waiting threads at once that the object (obj) is
available.
Ex: public final void notifyAll() 
 
3. wait():
This method makes a thread wait for the object (obj) till it receives a notification from a
notify() or notify All() methods. It is recommended to use the above methods inside a
synchronized block.

Method Description
public final void wait() throws InterruptedException It waits until object is notified.
public final void wait(long timeout) throws It waits for the specified amount of time.
InterruptedException

Q). Priority of a Thread (Thread Priority)


In java, each thread is assigned a priority, which affects the order in which it is scheduled for
running. The Threads of the same priority are given equal treatment by the java scheduler and,
therefore, they share the processor on a First-Come, First-Serve basis.

Java permits us to set the priority of a thread using the setPriority() method as follows.

ThreadName.setPriority(Number);

The Number is an integer value to which the threads priority is set. The Thread class defines several
priority constants of 3 types that is.

 public static int MIN_PRIOITY=1

 public static int NORM_PRIORITY=5

 public static int MAX_PRIORITY=10

The Number may assume one of these constants or any value between 1 and 10. The default setting
is NORM _ PRIORITY.

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 11

Whenever multiple Threads are ready for execution, the Java system chooses the highest priority
thread and executes it. For a thread of lower priority to gain control, one of the following things
should happen.

1. It stops running at the end of run().


2. It is made to sleep using sleep().
3. It is told to wait using wait().

Example of priority of a Thread:


class TestPriority extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}

public static void main(String args[]){


TestPriority m1=new TestPriority();
TestPriority m2=new TestPriority();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
} }

Q). Briefly explain about Thread Group?


A thread group represents several threads as a single group. The main advantage of taking several
threads as a group is that by using a single method, we will be able to control all the threads in the
group.

1. To create a thread group, we should simply create an object to ThreadGroup

class as:

ThreadGroup tg=new ThreadGroup("groupname");

Here, tg is the thread group object, and groupname is its name.


2. To add a thread to this group (tg);
Thread t1 = new Thread(tg, targetobj, "threadname");
Here, t1 thread is created and added to the thread group tg. This thread acts on targetobj,
which is the target object for the thread. The threadname represents the name of the thread
t1.
3. To add another tg1 = new ThreadGroup (tg, "groupname");

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 12

Here we are creating and adding the thread group tg1 to the thread group tg. The name of the
added thread group is represented by groupname.
4. To know the parent of a thread or a thread group, we can use getParent().
tg.getParent ();
This method returns ThreadGroup object which is the parent of tg.
5. To know the parent thread group of a thread, we can use:
t.getThreadGroup();
This return a ThreadGroup object to which the thread t belongs.
6. To know the number of threads actively running in a thread group :
tg.setMaxPriority ();
Normally, the maximum priority of a thread group will be 10. But this method can set it as any other
number between 1 and 10.
Example: To demonstrate the creation of thread groups and some methods which act on thread
groups.
class TGroups
{
public static void main(String[] args) throws Exception
{
Reservation res = new Reservation ();
Cancellation can = new Cancellation ();

ThreadGroup tg= new ThreadGroup ("First Group");


Thread t1 = new Thread(tg, res, "First thread");
Thread t2 = new Thread(tg, res, "Second thread");

ThreadGroup tg1 = new ThreadGroup(tg, "Second Group");


Thread t3 = new Thread (tg1, can, "Third thread");
Thread t4 = new Thread (tg1, can "Fourth thread")

System.out.println ("Parent of tg1="+tg1.getParent());


tg1.setMaxPriority (7);
System.out.println("Thread group of t1="+t1.getThreadGroup());
System.out.println("Thread group of t1="+t3.getThreadGroup());
t1.start();
t2.start();
t3.start();
t4.start();
System.out.println("No of threads active in tg = "+tg.activeCount());
}
}

class Reservation extends Thread


{
public void run ()
{
System.out.println("I am reservation thread")
}
}
class Cancellation extends Thread
{
public void run ()
{
stem.out.println("I am cancellation thread");
}
}

Q). What is Daemon Threads?

A daemon thread is a thread that executes continuously. Daemon threads are service providers for
other threads (or) objects. It generally provides a background processing

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 13

1. To make a thread t as a daemon thread, we can use setDaemon() method as:


t. setDaemon(true);

2. 2 To know if a thread is daemon or not, isDaemon() is useful.

boolean x = t. isDaemon ();


If isDaemon() returns true, then the thread t is a daemon thread, otherwise not.

Lifecycle of a Thread (Thread states)


During the lifetime of a thread, there are many states it can enter. They include:
1) Newborn State
2) Runnable State
3) Running State
4) Blocked State
5) Dead State

A thread is always in one of these five states. It can move from one state to another via a variety
of ways as shown in below figure.

1. NewBorn State
When we create a thread object, the thread is born and is said to be in newborn state. The

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 14

thread is not yet scheduled for running. At this state, we can do only one of the following
things with it:
 Schedule it for running using start() method

2. Runnable State

The Runnable state means that the thread is ready for execution and is waiting for the
availability of the processor. If all the threads have equal priority, then they are given time
slots for execution in first-come, first-serve manner. If we want a thread to relinquish (hand
over) control to another thread to equal priority before its turn comes, we can do so by using
the yield().

3. Running State

Running means that the processor has given its time to the thread for its execution. The
thread runs until it loses control on its own or it is preempted by a higher priority thread. A
Thread may be control its control in one of the following situations.

 It has suspended by suspend() method. A suspended thread can be revived by


using the resume() method.
 It has been made for sleep by using sleep() method. The thread re-enters the
runnable state as soon as this time period is elapsed.
4. Blocked State

A thread is said to be Blocked when it is prevented from entering into the runnable state
and subsequently the running state. This happens when the thread is suspended, sleping, or
waiting in order to satisfy certain requirements. A blocked thread is considered “not
runnable” but not dead and therefore fully qualified to run again.
5. Dead State

Every thread has a lifecycle. A running thread ends its life when it has completed executing
its run() method. It is natural death. However, we can kill it by sending the stop message to it
at any state thus causing a premature death to it. It is done by stop() method.

Q). Thread Class Methods


Before we begin with the programs(code) of creating threads, let’s have a look at these methods
of Thread class. We have used few of these methods in the example below.

1. getName(): It is used for Obtaining a thread’s name

Syntax: String getName()

2. getPriority(): Obtain a thread’s priority

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 15

Syntax: setPriority(int priority)

3. isAlive(): Determine if a thread is still running

Syntax: boolean isAlive()

4. join(): Wait for a thread to terminate

Syntax: void join()

5. run(): Entry point for the thread

Syntax: void run()


{
//Thread ststement;
}
6. sleep(): suspend a thread for a period of time
Syntax: void sleep(long time-in milliseconds)

7. start(): start a thread by calling its run() method

Syntax: void start()

8. stop() stops the running thread

Syntax: void stop()

Thread class also defines many methods for managing threads. Some of them are,

Method Description
1)setName() to give thread a name
2)getName() return thread's name
3)getPriority() return thread's priority
4)isAlive() checks if thread is still running or not
5)join() Wait for a thread to end
6)run() Entry point for a thread
7)sleep() suspend thread for a specified time
8)start() start a thread by calling run() method
Returns an estimate of the number of active threads in
9)activeCount()
the current thread's thread group and its subgroups.
Determines if the currently running thread has
10) checkAccess()
permission to modify this thread.
Returns a reference to the currently executing thread
11) currentThread()
object.
Prints a stack trace of the current thread to the
12) dumpStack()
standard error stream.
13) getId() Returns the identifier of this Thread.
14) getState() Returns the state of this thread.
15) getThreadGroup() Returns the thread group to which this thread belongs.

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science


Page no- 16

Method Description
16) interrupt() Interrupts this thread.
17) interrupted() Tests whether the current thread has been interrupted.
18) isAlive() Tests if this thread is alive.
19) isDaemon() Tests if this thread is a daemon thread.
20) isInterrupted() Tests whether this thread has been interrupted.
21) setDaemon(boolean Marks this thread as either a daemon thread or a user
on) thread.
22) setPriority(int
Changes the priority of this thread.
newPriority)
A hint to the scheduler that the current thread is willing
23) yield()
to yield its current use of a processor.

Prepared by: M.VENKAT (MCA, M-Tech) Lecturer in Computer Science

You might also like