Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday, 5 May 2017

How To Sleep A Thread In Java Multithreading With Example

Here we will learn how to sleep a thread in java multithreading but before that we will learn " what is thread scheduler in multithreading ". Let's starts..


java sleep



Thread Scheduler

Thread scheduler decides which thread should run and thread scheduler is the part of the JVM. It is a implementation and platform independent therefore which thread will run is unpredictable.

By the help of preemptive and time slicing or round robin scheduling, thread scheduler scheduled the thread.


Sleeping A Thread In Java Multithreading

In this sleeping thread example we are going to use sleep() method of thread class which is used to sleep any thread for the specified amount of time.

There are 2 sleep methods of thread class, these are :

1) public static void sleep(long miliseconds)throws InterruptedException.

2) public static void sleep(long miliseconds, int nanos)throws InterruptedException.


Example of Sleep A Thread

class SleepThreadExample extends Thread
{
public void run()
{
for(int i = 1; i<5; i++)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[])
{
SleepThreadExample s = new SleepThreadExample();
SleepThreadExample ss = new SleepThreadExample();

s.start();
ss.start();
}
}

output :1
              1
              2
              2
              3
              3
              4
              4


Can we start a Thread twice in java

The answer is no, We can't start a thread twice in java multithreading. If we will start a thread twice it will throw an IllegalThreadStateException. First time thread will run but second time it will throw exception. Let's take an example of it

class TestThread extends Thread
{
public void run()
{
System.out.println("run method");
}
public static void main(String args[])
{
TestThread t = new TestThread();

t.start();
t.start();
}
}

output : run method
              Exception in thread "main" java.lang.IllegalThreadStateException.


Call run() method directly

What happens when we call run() method directly, not through start() method ?

Yes, we can call run() method directly but When we call a run method directly it will behave like normal method, If it will behave like normal method then we cannot take advantage of multithreading in java.

Each thread starts in separate call stack. If we call run() method from main thread,  run() method goes into the current call stack rather than at the begging of a new call stack.

class SimpleRunCalling extends Thread
{
public void run()
{
for(int i = 1; i<5; i++)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[])
{
SimpleRunCalling s = new SimpleRunCalling();
SimpleRunCalling ss = new SimpleRunCalling();

s.run();
ss.run();
}
}

output :1
              2
              3
              4
              1
              2
              3
              4

In the above example the object is not treated as thread object. Its behave like normal object. There is no context switching between s and ss thread.


Naming Thread In Java

We can get the name of thread and we can change the name of thread also by using thread class's getName() and setName() methods.

By default the each thread name is thread-0 and thread-1 and so on.

public String getName() : return the name of thread.

public void setName(String name) : Used to change the name of thread.

Naming Thread Example

class NamingThreadExample extends Thread
{
public void run()
{
System.out.println("thread running");
}
public static void main(String args[])
{
NamingThreadExample n = new NamingThreadExample();
NamingThreadExample n1 = new NamingThreadExample();

System.out.println("name of first thread n " +n.getName());
System.out.println("name of second thread n1 " +n1.getName());

n.start();
n1.start();

n.setName("Anurag Singh");
System.out.println("After changing name of first thread n " +n.getName());
}
}

output : name of first thread n Thread-0
              name of second thread n1 Thread-1
              After changing name of first thread n Anurag Singh
              thread running
              thread running


Current Thread In Java Multithreading

By the help of currentThread() method we can get the reference of currently executing thread in java.

public static Thread currentThread()

Example of currentThread

class CurrentThreadExample extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[])
{
CurrentThreadExample c = new CurrentThreadExample();
CurrentThreadExample c1 = new CurrentThreadExample();

c.start();
c1.start();

}


output : Thread-0
              Thread-1

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate