Introduction To Threads: C o R e J A V A
Introduction To Threads: C o R e J A V A
C Introduction to Threads
o
r
e
J
a
v
a
Topics to be discussed
Introduction to Threads
Creating Threads
Thread States
Methods of Thread class
Managing Threads
Daemon Threads
Code
Snippet
class MyThread extends Thread //Extending Thread
class
{
//class definition
. . .
}
Constructor Description
Thread (ThreadGroup group, Creates a new Thread object so that it has
Runnable objRun, String objRun as its run object threadName as its
threadName) name and belongs to ThreadGroup referred
to by group.
objNamedThread.setName(“Thread1”);
//Display the status of the thread, whether alive or not
System.out.println(Thread.currentThread().isAlive());
System.out.println(objNamedThread.isAlive());
/*invokes the start method which in turn will call
run and begin thread execution
*/
objNamedThread.start();
System.out.println(Thread.currentThread().isAlive());
System.out.println(objNamedThread.isAlive());
}
}
Java Tutorial / Introduction to Threads Sameer Jha 18
Constructor and Methods of Thread Class [4-
4]
NamedThread is declared as a derived class of Thread.
In the main() method of this class, a thread object objNamedThread is
created by instantiating NamedThread and its name is set to Thread1.
The code checks if the current thread is alive by invoking the isAlive()
method and displays the return value of the method.
This will result in true being printed because the main(default) thread
has begun execution and is currently alive.
The code also checks if objNamedThread is alive by invoking the same
method on it.
Next, the start() method is invoked on objNamedThread which will
cause the thread to invoke the run() method which has been overridden.
The run() method prints the total number of threads running then checks
the name of the thread running and prints Marimba if the currently running
thread’s name is Thread1.
The method performs this checking three times.
Declare a class that implements the Runnable interface.
Code
Snippet
// Declaring a class that implements Runnable
interface
class MyRunnable implements Runnable
{
. . .
}
The Runnable interface defines a method, run(), to contain the code that
will be executed by the thread object.
The class implementing the Runnable interface should override the run()
method. The following code snippet implements the run() method:
Code
Snippet
// Declaring a class that implements Runnable
interface class MyRunnable implements Runnable
{
public void run() // Overriding the
Run()
{
. . .
// implementation
}
}
Code
Snippet
class ThreadTest
{
public static void main(String args[])
{
MyRunnable r=new Runnable();
Thread thObj=new Thread(r);
thObj.start(); //Starting a thread
}
}
Blocked
State
su
waispen
t( d()
) ,
sl
eep
()
,
notify(), notifyAll()
Waiting Runnable
New State
Thread start() State wait()
)
o y(
r
e st
) ,d
o p(
st
Terminat
ed State
Code
Snippet
. . .
Thread thObj = new Thread();
. . .
Code
Snippet
. . .
MyThreadClass myThread = new MyThreadClass();
myThread.start();
. . .
Code
Snippet
public void run()
{
for(int i = 0;i < 5;i++)
{
Thread t = Thread.currentThread();
System.out.println(“Name = “ + t.getName());
...
Code
Snippet
. . .
NewThread thObj = new NewThread();
thObj.start();
. . .
Code
Snippet
class myRunnable implements Runnable {
. . .
public void run() {
System.out.println(“Inside the run method.”);
}
....}
Java Tutorial / Introduction to Threads Sameer Jha 35
sleep() Method
The sleep() method has the following characteristics:
It suspends the execution of the current thread for a specified period of time.
It makes the processor time available to other threads of an application or other
applications that might be running on the computer system.
It stops the execution if the active thread for the time specified in milliseconds or
nanoseconds. It raises InterruptedException when it is interrupted using
the interrupt() method.
Syntax: void sleep(long millis)
Code
Snippet
Try
{
myThread.sleep (10000);
}
catch (InterruptedException e)
{
}
process
T1 T2 T3 T4 T5 T6 T7 T8
Managing Threads
Code
Snippet
. . .
Thread threadA = new Thread(“Meeting deadlines”);
threadA.setPriority(8);
. . .
isDaemon()
The isDaemon() method determines if a thread is a daemon thread or not.
It returns true if this thread is a daemon thread, else returns false.