We can
prevent the thread execution by using following methods of threads class.
1. yield ()– This is
to pause current executing thread, for giving chance to remaining waiting
threads of same priority. If there is no thread waiting or all the threads
waiting have low priority, then the same thread is once again given a chance
for the execution. The once again chance given to the thread is decided by
thread scheduler and we cannot expect anything exactly.
§ public static
native void yield();
Example –
public class
Yields extends Thread{
public void
run()
{
for(int i=0;i<5;i++)
{
Thread.yield(); //
Statement-1
System.out.println(getName()+" Run : "+i);
}
}
public static void
main(String[] args) {
Yields y1 = new
Yields();
y1.start();
for(int i=0;i<5;i++)
{
System.out.println("main thread - "+i);
}
}
}
Note – If we comment statement-1
then both main and child thread will be executed simultaneously and we can’t
expect the exact result. If we aren’t commenting statement-1 then the child
thread calls yield() method, so that main thread gets chance more number of
times to execute. Hence, there is a chance of completing main thread first. It
has higher priority compared to child threads.
2. join() – If a
thread wants to wait until completion of other thread, then we should go for
join() method. Suppose if a thread t1 executes t2.join(), then t1 will enter
into waiting state until t2 execution is completed.
§ public final
void join() throws InterruptedException
§ public
final void join(long msec) throws InterruptedException
§ public
final void join(long ms, int ns) throws InterruptedException
Example –
public class
Joins extends Thread{
public void
run()
{
for(int i=0;i<10;i++)
{
System.out.println(getName()+" Run : "+i);
try{
Thread.sleep(2000);
}
catch(InterruptedException
e){}
}
}
public static void
main(String[] args) throws InterruptedException {
Joins j1 = new
Joins();
j1.start();
j1.join();//
statement-1
for(int i=0;i<10;i++)
{
System.out.println("main thread "+i);
}
}
}
Note – If we comment
statement-1, then both main and child threads will be executed simultaneously
and we can’t expect exact output. If we aren’t commenting statement-1, then
main thread has to wait until the child thread execution is completed.
Thread-0 Run : 0
Thread-0 Run : 1
Thread-0 Run : 2
Thread-0 Run : 3
Thread-0 Run : 4
Thread-0 Run : 5
Thread-0 Run : 6
Thread-0 Run : 7
Thread-0 Run : 8
Thread-0 Run : 9
main thread 0
main thread 1
main thread 2
main thread 3
main thread 4
main thread 5
main thread 6
main thread 7
main thread 8
main thread 9
3. sleep() – If a
thread don’t want to perform any operation for a particular amount of time when
a pausing is required, then we should go for sleep() method.
§ public static
native void sleep(long ms) throws InterruptedException
§ public
static void sleep(long ms, int ns) throws InterruptedException
Example –
public class
Sleep extends Thread{
public void
run()
{
for(int i=0;i<5;i++)
{
System.out.println(getName()+" Run : "+i);
try{
Thread.sleep(2000);
}
catch(InterruptedException
e){}
}
}
public static void
main(String[] args) {
Sleep s1 = new
Sleep();
s1.start();
for(int i=0;i<5;i++)
{
System.out.println("main thread "+i);
}
}
}
No comments:
Post a Comment