Introduction
to Threads
Introduction to Threads
Learning Objective
Creating Threads
Thread States
Runnable Interface
Priority of Thread
Interrupting Threads
ThreadGroup
Introduction of Thread
A thread is a single sequential flow of control within a
program.
A single thread also has a beginning, an end, a sequence,
and at any given time during the runtime of the thread
there is a single point of execution.
Multithreading in java is a process of executing multiple
threads simultaneously.
Multiprocessing and multithreading, both are used to
achieve multitasking.
Java Multithreading is mostly used in games, animation
etc.
Life-cycle of Thread
Creating Threads
Threads can be created using either of the two ways:
Extending Thread Class
Thread class provide constructors and methods to
create and perform operations on a thread.
Thread class extends Object class and implements
Runnable interface.
Implementing Runnable Interface
Runnable Interface
A Thread can be created by extending Thread class .
However, Java allows only one class to extend, it wont
allow multiple inheritance.
This can be done by implementing Runnable interface.
To implement Runnable interface , implement a run() .
This method provides entry point for the thread
Runnable Interface
Thread object is instantiated using the Thread
class constructor.
Thread(Runnable threadObj, String threadName);
The run() is executed by calling the start().
Example
public class FirstThread implements Runnable
{
public void run()
{
for ( int i=1; i<=10; i++)
{
System.out.println( "Message from First Thread : " +i);
try
{
Thread.sleep (1000);
}
catch (InterruptedException obj)
{
System.out.println( “Thread is interrupted when it is
sleeping "+obj);
}
}
}
Example : Runnable Interface
public class ThreadDemo
{
public static void main(String args[])
{
FirstThread firstThread = new FirstThread();
Thread thread1 = new Thread(firstThread);
thread1.start();
}
}
o/p
Message from First Thread : 1
Message from First Thread : 2
.
Message from First Thread : 10
Priority Of Thread
Priorities are represented by a number between 1 and
10.
public static int MIN_PRIORITY
public static int NORM_PRIORITY
public static int MAX_PRIORITY
Example
class Test implements Runnable
{
Thread t;
boolean runn = true;
Test(String st , int p)
{
t = new Thread(this,st);
t.setPriority(p);
t.start();
}
public void run()
{
System.out.println("Thread name : " + t.getName());
System.out.println("Thread Priority : " + t.getPriority());
}
Example
Program conti…
class priority
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Test t1 = new Test("Thread1",Thread.NORM_PRIORITY + 2);
Test t2 = new Test("Thread2",Thread.NORM_PRIORITY - 2);
System.out.println("Main Thread : " + Thread.currentThread());
System.out.println("Main Thread Priority
:"+Thread.currentThread().getPriority());
}
}
Example
Output
Thread name : Thread1
Main Thread : Thread[main,10,main]
Thread name : Thread2
Thread Priority : 7
Main Thread Priority : 10
Thread Priority : 3
Interrupting Thread
If any thread is in sleeping or waiting state (i.e.
sleep() or wait() is invoked), calling the interrupt()
method on the thread, breaks out the sleeping or
waiting state throwing InterruptedException.
If the thread is not in the sleeping or waiting state,
calling the interrupt() method performs normal
behaviour and doesn't interrupt the thread but sets
the interrupt flag to true.
ThreadGroup
Thread groups offer a convenient way to manage
groups of threads as a unit.
Used to suspend and resume a number of related
threads.
Syntax
ThreadGroup groupA = new ThreadGroup("Group
A");
NewThread ob1 = new NewThread("One", groupA);
NewThread ob2 = new NewThread("Two", groupA);
Interview Questions
What is the difference between Process and Thread?
What are the different ways to start a thread?
What are different states in lifecycle of Thread?
What are the benefits of multi-threaded programming?
What is difference between user Thread and daemon
Thread?
How do you change the thread name?
What is context-switching in multi-threading?
How is synchronization achieved in Java?
How do you make a class thread safe?
Why Thread sleep() and yield() methods are static?
Any Questions