Results 1 to 3 of 3
  1. #1
    TrolleY is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default How to sleep exactly 5 seconds ignoring InterruptedException

    Hi!

    Im curious how to sleep in Java excatly f.e. 5 seconds no matter if thread was interrupted or not. I know how to do it in C but I have no idea how to do it in Java. Is there a way to find out how long thread was sleeping like in C?

    Java Code:
    public static void main(String... args) throws InterruptedException
    	{
    		new Thread()
    		{
    			@Override
    			public void run()
    			{
    				for(int i=0; i<5; i++)
    				{
    					try
    					{
    						System.out.printf("T1: Sleeping %d\n",i);
    						Thread.sleep(1000);
    					} catch (InterruptedException e)
    					{
    						e.printStackTrace();
    						return;
    					}
    				}
    			}
    		}.start();
    		Thread t = new Thread()
    		{
    			@Override
    			public void run()
    			{
    				System.out.println("T2: I will sleep 5 seconds");
    				try
    				{
    					Thread.sleep(5000); // I want him to sleep exactly 5 seconds
    				} catch (InterruptedException e)
    				{
    					System.out.println("INTERRUPTED");
                                            return;
    				}
    				System.out.println("T2: Done");
    			}
    		};
    		t.start();
    		Thread.sleep(2500);
    		t.interrupt();
    	}
    Last edited by TrolleY; 09-08-2013 at 10:48 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: How to sleep exactly 5 seconds ignoring InterruptedException

    You need to take a snap shot of the current time. Then do your sleep. Then when you get interrupted, compare how long you actually slept to the current time and then sleep the remainder.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: How to sleep exactly 5 seconds ignoring InterruptedException

    P.S. It is only possible to sleep EXACTLY 5 seconds (or at least within a few nanoseconds of that) using Hard RealTime programming practices with Java RealTime, and for that jvm, at least when using Hard RealTime, there is a fairly hefty license cost.

Similar Threads

  1. Replies: 6
    Last Post: 02-28-2013, 08:03 AM
  2. Replies: 4
    Last Post: 09-09-2010, 11:15 AM
  3. Replies: 3
    Last Post: 04-05-2010, 01:08 PM
  4. InterruptedException and Locks
    By JavaJuJitZu in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-19-2010, 02:47 AM
  5. Replies: 2
    Last Post: 05-28-2009, 05:09 PM

Posting Permissions

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