freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Thread Sleep

The sleep() method causes the current thread to sleep for a specified amount of time in milliseconds.
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis,int nanosec) throws InterruptedException

For example, the code below puts the thread in sleep state for 5 minutes:

class Test
{
public static void main(String[] args)
{
try
{
for (int i=0;i<10 ;i++)
{
System.out.println("hi");
Thread.sleep(5*1000);
Thread.sleep(5*60*1000);
System.out.println("bye");
}
}
catch (InterruptedException ie)
{
System.out.println("the thread is got innterupted");
}
}
}

intput and output

output:hi
bye

Java.lang.Thread.yield()

1)Yield() method causes to pause current executing Thread for giving the chance for waiting threads of same priority.
2)If there are no waiting threads or all threads are having low priority then the same thread will continue its execution once again.

yield program

import java.util.*;
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
Thread.yield();
System.out.println("child thread");
}
}
}
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
t1.start();
for(int i=0;i<10;i++)
{
System.out.println("main thread");
}
}
}

intput and output

output:main thread
child thread

Java.lang.Thread.join():-

If a Thread wants to wait until completing some other thread then we should go for join() method.
   1. Public final void join()throws InterruptedExcetion
   2. Public final void join(long ms)throws InterruptedException
   3. Public final void join(long ms, int ns)throws InterruptedException

join program

class MyThread extends Thread
{
public void run()
{
for (int i=0;i<5;i++ )
{
try{
System.out.println("freetimestudy");
Thread.sleep(3*1000);}
catch(InterruptedException iee)
{
System.out.println("gettting innterupted exctpion");
}
}
}
}
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
try
{
t1.join();
}
catch (InterruptedException ie)
{
System.out.println("interrupted Exception");
}
t2.start();
}
};

intput and output

output:freetimestudy 10times

isAlive():-

used to check whether the thread is live or not.
Public Boolean isAlive()

class MyThread extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
};
class Test
{
public static void main(String[] args)
{
MyThread t=new MyThread();
System.out.println(t.isAlive());
t.start();
System.out.println(t.isAlive());
}
};

intput and output

output:false
true
Thread-0

Java.lang.Thread.activeCount():-

This method is used to find out the number of methods in active state.
Public static int activeCount();

activeCount() program

class MyThread extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
};
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
t1.start();
t2.start();
t3.start();
System.out.println(Thread.activeCount());
}
};

intput and output

output:4
Thread-1
Thread-0
Thread-2

Java.lang.currentThread():-

This method is used to represent current thread class object.
Public static thread currentThread()

currentThread program

class MyThread extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
};
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
}
};

intput and output

output:Thread-1
Thread-0

Java.lang.Thread.getId():-

getId() is used to generate id value for each and every thread.
Public long getId()

getId program

class MyThread extends Thread
{
public void run()
{
System.out.println(" freetimestudy thread is running ");
}
};
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
System.out.println("the thread id :"+t1.getId());
System.out.println("the thread name is :"+t1.getName());
System.out.println("the thread priority is "+t1.getPriority());
System.out.println("the thread id :"+t2.getId());
System.out.println("the thread name is :"+t2.getName());
System.out.println("the thread priority is "+t2.getPriority());
}
};

intput and output

output:the thread id :8
the thread name is :Thread-0
the thread priority is 5
the thread id :9
the thread name is :Thread-1
the thread priority is 5
freetimestudy thread is running
freetimestudy thread is running

Interrupted():-

1)A thread can interrupt another sleeping or waiting thread.
2)For this Thread class defines interrupt() method.
Public void interrupt()

interrupt program

class MyThread extends Thread
{

public void run()
{
try
{
for (int i=0;i<10;i++ )
{
System.out.println("i am sleeping ");
Thread.sleep(5000);
}
}
catch (InterruptedException ie)
{
System.out.println("i got interupted by interrupt() call");
}
}
};
class Test
{
public static void main(String[] args)
{
MyThread t=new MyThread();
t.start();
t.interrupt();
}
};

intput and output

output:i am sleeping
i got interupted by interrupt() call