Thread sleep interruptedexception

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"); 
} 
} 
}
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"); 
} 
} 
}
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("freetimelearn"); 
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(); 
} 
};
Output :
freetimelearn 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()); 
} 
};
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()); 
} 
};
Output :
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(); 
} 
};
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(" freetmielearn 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()); 
} 
};
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
freetmielearn thread is running
freetmielearn 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(); 
} 
};
Output :
i am sleeping
i got interupted by interrupt() call