Results 1 to 3 of 3

Thread: Threaded Timer Class

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Threaded Timer Class

    I need to create a threaded timer class.

    This class will start at each junit test and end when the test complete, but if the test does not complete in a certain time interval, it should fail the test.

    Still very green to Java, so any help would be appreciated.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threaded Timer Class

    Something like this might work
    Code:
    public class Test
    {
    	private volatile Thread th;
    
    	public Test(final int timeout)
    	{
    		th = new Thread(new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				try
    				{
    					Thread.sleep(timeout);
    					assert false;
    				}
    				catch (final InterruptedException e)
    				{
    					e.printStackTrace();
    					assert false;
    				}
    			}
    		});
    		th.start();
    	}
    
    	public void complete()
    	{
    		th = null;
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: Threaded Timer Class

    Thanks!
    I did not use it (found the @Test annotation in jUnit have a timeout property, so been using that).
    But do have to get to threading sooner or later (as I've switched to Java), so this will be helpful.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

Posting Permissions

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



Click Here to Expand Forum to Full Width