Sunday 29 January 2012

Uses of Thread.sleep() in Java with Examples


Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Thread.sleep can also be used to implement some graphical functionalities i.e.to make some delay in displaying some texts,frames,windows etc.

Consider one real-time scenario, you and your friend has got a box of apples,both of you are started to eat apples.Suppose you can eat more fast than your friend,what will happen ?.Your friend won't get enough apples.

This situation is called 'Starvation' in Computer Science,where some threads won't get enough CPU time compared to some other concurrent threads.See the code given below



class MyThread extends Thread {

    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName()
                    + " finished one apple");
        }
    }
}

public class Main {

    public static void main(String args[]) {
        MyThread you = new MyThread();
        you.setName("YOU");
        MyThread yourFriend = new MyThread();
        yourFriend.setName("YOUR FRIEND");
        you.start();
        yourFriend.start();

    }
}

The output will be like this (may vary based on processor and other processes)

YOU finished one apple
YOU finished one apple
YOU finished one apple
YOU finished one apple
YOU finished one apple
YOU finished one apple
YOUR FRIEND finished one apple
YOUR FRIEND finished one apple
YOU finished one apple
YOU finished one apple

Here you can see that you are overrunning your friend,out of 10 apples you had eight apples and your friend got only two apples.

How can you show a little justice to your friend ? .You can sit some time as ideal and allow your friend to finish his apple(and vice versa).

See the implementation of it using Thread.sleep method given below.
class MyThread extends Thread {

    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName()
                    + " finished one apple");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {

    public static void main(String args[]) {
        MyThread you = new MyThread();
        you.setName("YOU");
        MyThread yourFriend = new MyThread();
        yourFriend.setName("YOUR FRIEND");
        you.start();
        yourFriend.start();

    }
}
The output will be like this(may have slight change based on processor)

YOU finished one apple
YOUR FRIEND finished one apple
YOU finished one apple
YOUR FRIEND finished one apple
YOU finished one apple
YOUR FRIEND finished one apple
YOU finished one apple
YOUR FRIEND finished one apple
YOU finished one apple
YOUR FRIEND finished one apple

Now you can see that both of you are sharing apples equally( NOTE: almost equally !! ,may change based on processor and other running processes).

No comments:

Post a Comment