Sleeping a thread (sleep() method):
The sleep() method of Thread class is used to sleep a thread for the specified time.
Syntax of sleep() method:
The Thread class provides two methods for sleeping a thread:
public static void sleep(long miliseconds) throws InterruptedException
public static void sleep(long miliseconds, int nanos) throws InterruptedException
Example of sleep method:
class Test extends Thread{
public void run(){
for(int a=1;a<6;a++){
try{
Thread.sleep(500);
}
catch(InterruptedException ex){
System.out.println(ex);
}
System.out.println(a);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
Output:
1
1
2
2
3
3
4
4
5
5
6
6
As you know well that at a time only one thread is executed. If you sleep a thread for the specified time,the thread shedular picks up another thread to run and so on.
Can we start a thread twice ?
No. After staring a thread,it can never be started again.If you does so, an IllegalThreadStateException is thrown.
For Example:
class Test extends Thread{
public void run(){
System.out.println("this is run method");
}
public static void main(String []args){
Multi t1=new Multi();
t1.start();
t1.start();
}
}
Output: this is run method
Exception in thread "main" java.lang.IllegalThreadStateException
What if we call run() method directly instead start() method ?
Each thread starts in a separate call stack.
Invoking the run() method from main thread,the run() method goes onto the current call stack rather than at the beginning of a new call stack.
class Test extends Thread{
public void run(){
System.out.println("this is run method");
}
public static void main(String []args){
Test t1=new Test();
t1.run(); //fine it will run , but does not start a separate call stack
}
}
Output: this is run method
Problem if you direct call run() method:
class Test extends Thread{
public void run(){
for(int a=1;a<6;a++){
try
{
Thread.sleep(500);
}
catch(InterruptedException ex){
System.out.println(ex);}
System.out.println(a);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
t1.run();
t2.run();
}
}
Output:
1
2
3
4
5
1
2
3
4
5
6
Note: As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not a thread object.
The sleep() method of Thread class is used to sleep a thread for the specified time.
Syntax of sleep() method:
The Thread class provides two methods for sleeping a thread:
public static void sleep(long miliseconds) throws InterruptedException
public static void sleep(long miliseconds, int nanos) throws InterruptedException
Example of sleep method:
class Test extends Thread{
public void run(){
for(int a=1;a<6;a++){
try{
Thread.sleep(500);
}
catch(InterruptedException ex){
System.out.println(ex);
}
System.out.println(a);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
Output:
1
1
2
2
3
3
4
4
5
5
6
6
As you know well that at a time only one thread is executed. If you sleep a thread for the specified time,the thread shedular picks up another thread to run and so on.
Can we start a thread twice ?
No. After staring a thread,it can never be started again.If you does so, an IllegalThreadStateException is thrown.
For Example:
class Test extends Thread{
public void run(){
System.out.println("this is run method");
}
public static void main(String []args){
Multi t1=new Multi();
t1.start();
t1.start();
}
}
Output: this is run method
Exception in thread "main" java.lang.IllegalThreadStateException
What if we call run() method directly instead start() method ?
Each thread starts in a separate call stack.
Invoking the run() method from main thread,the run() method goes onto the current call stack rather than at the beginning of a new call stack.
class Test extends Thread{
public void run(){
System.out.println("this is run method");
}
public static void main(String []args){
Test t1=new Test();
t1.run(); //fine it will run , but does not start a separate call stack
}
}
Output: this is run method
Problem if you direct call run() method:
class Test extends Thread{
public void run(){
for(int a=1;a<6;a++){
try
{
Thread.sleep(500);
}
catch(InterruptedException ex){
System.out.println(ex);}
System.out.println(a);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
t1.run();
t2.run();
}
}
Output:
1
2
3
4
5
1
2
3
4
5
6
Note: As you can see in the above program that there is no context-switching because here t1 and t2 will be treated as normal object not a thread object.
No comments:
Post a Comment