Friday, 26 August 2016

Thread Sleep Method

Sleep() method :-

The Sleep method is a static method of class Thread, we can use it to force thread to sleep mode before coming back to runabble
Try{
Thread.sleep(10*60*1000);
}catch(InterruptedException ex) {}

Example:

package multithreadingtc;

/**
 *
 * @author shashank
 */
class Multithreading implements Runnable
{
     public void run()
    {
        for(int i=1;i<=100;i++)
        {
            System.out.println("run by" + Thread.currentThread().getName() + " remainder is i is " +i%10);
            try
            {
                Thread.sleep(1000);
            }catch(InterruptedException ex){}
        }
    }
}
public class MultithreadingTC{
    public static void main(String[] args) {
        // TODO code application logic here
        Multithreading th = new Multithreading();
        Thread t = new Thread(th);
        t.setName(" first");
        t.start();
        Thread t1 = new Thread(th);
        t1.setName(" second");
        t1.start();
        Thread t2 = new Thread(th);
        t2.setName(" third");
        t2.start();
    }   

}

No comments:

Post a Comment