Friday, February 13, 2009

Thread class yield , join , sleep.

Thread Priorities:
• Thread class contains the fallowing methods for setting and getting priority of a thread.
public final int getPriority();
public final void setPriority(int i);
• The valid range for the thread priorities 1 to 10 (1 is atleast and 10 is the highest)
• Thread class contains the fallowing predefined priority constraints.
MAX-PRIORITY ----> 10
NORM-PRIORITY ----> 5 (default)
MIN-PRIORITY ----> 10
• If you are trying to set the priority as greater than 10 or lessthan1, then we will get a RTE saying “Illegal Argument Exception”
Example:
class MyThread extends Thread
{}
class Sample
{
public static void main(String a[])
{
Thread.currentThread().setPriority(10);
System.out.pritnln(Thread.currentThread().getPriority());
MyThread t= new MyThread();
System.out.pritnln(t.getPriority());//10 10
}
}
• The default priority for the maintained is 5. But we are allowed to change the prority by using setPriority()
• The priority of any the thread is inherited from the parent thread.
• Thread scheduler uses these proroites to allocate CPU. The thread which is having highest priority will get executes first. t1.setPriority(10);
Yield() :

  • We can prevent a thread from execution by using one of the fallowing methods.
1. yield()
2. join()
3. sleep()
• The thread which is executing yield() causes the current thread temperarly pause and allow other waiting threads of same or high priority to execute.
• If there is no waiting thread, the same thread will execute immediately.
• If all the remaining threads are having low priority, then the same thread once again will execute.
Method Signature:-
public static native yield();



• If the thread calls yield(), the thread going to ready state.
yield method example:
Class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.pritnln(“child thread”);
Thread.yield();
}
}
}
class SampleDemo
{
public static void main (String a[])
{
MyThread t = new MyThread();
t.start();
for(int i=0;i<10;i++);
{
System.out.pritnln(“main thread”);
}
}
}

• The yield() depends upon underlying platform. Some platforms may not support .Breaks java’s programme independency.
join:
• If any executing thread calls join method on amy thread t, the current thread will go to the blocked state until t completes.
Method Signature:
public final void join() throws InterruptedException
public final void join(long ms)throws InterruptedException
public final void join(long ms, int nanosec)throws InterruptedException


join Example:
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(“sita thread”);
try
{
Thred.sleep(1000);
}
catch(InterruptedException e){}
}
}
}
class JoinDemo
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start();
t.join(); //or t.join(3000);
for(int i=0;i<10;i++);
{
System.out.println(“Rama thread”);
}
}
}
sleep():-
• The thread which is executing sleep() will go to the sleep for the specified amount of time.
MethodSignature:
public static void sleep(long ms) throws InterruptedException
public static void sleep(long ms, int nanoseconds)throws InterruptedException



sleep example:
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(“child thread”);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}
class SleepDemo
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start();
}
}
interrupt():
• The thread can interrupt any sleeping/waiting/blocked for joining by the fallowing thread class method.
public void interrupt(); //this is instance method
interrupt example:
class MyThread extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(“child thread”);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(“I got interrupted”);
}
}
}
}
class SleepDemo
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start();
t.interrupt();
}
}


summsrization table:

0 comments:

Post a Comment