Results 1 to 3 of 3

Thread: Thread.Sleep

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Planet Earth
    Rep Power

    Default Thread.Sleep

    Java Code:
    class Clicker implements Runnable { 
    	int clicker = 0;
    	private volatile boolean running = true;
    	Thread thread;
    	public Clicker(int priority) {
    		thread = new Thread(this, "Clicker");
    	public void run() {
    		while (running) {
    	void stop(){
    		running = false;
    public static void main(String[] args) throws InterruptedException {
    		Clicker hi = new Clicker(Thread.NORM_PRIORITY+2);
    		Clicker lo = new Clicker(Thread.NORM_PRIORITY-2);
    		try {
    		} catch (InterruptedException e) {
    			System.out.println("Main interrupted");
    	System.out.println("Low priority thread : " + lo.clicker);
    	System.out.println("High priority thread : " + hi.clicker);
    When I remove thread.sleep it doesnot incriment the value of clicker. Whats the purpose of sleep here ? To stop execution of main thread and allowing hi and lo thread to run for 1 second ??? and when will interupted exception occur, mean how thread are interrupted by what means ?
    Last edited by allaudin; 07-16-2013 at 07:10 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Thread.Sleep

    The purpose of the timer is to give the threads a chance to run so you can see the difference in the priority behavior. The exception will not be caught unless the InterruptedException is thrown. Normally, when an exception is not thrown that is a good thing. This is because Exceptions are indicative of exceptional (read - out of the ordinary or not normal) behavior.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: Thread.Sleep

    InterruptedException is thrown when interrupt() is called on a thread while it's blocked in a wait() or sleep(). Maybe other times, too, but those are the ones I've most commonly encountered. (There's also InterruptedIOException, which is thrown when a thread is interrupted during blocking I/O.)
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2012, 07:01 PM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  4. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts