Results 1 to 6 of 6

Thread: ComputerJy, need your expert help on threads please

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    ComputerJy, need your expert help on threads please

    Code:
    public class Test {
        public static void main(String[] args) {
    	GamePlayer game = new GamePlayer();
    	
    	System.out.println(game.doSomeMove());
        }
    }
    
    class GamePlayer {
        int doSomeMove() {
    	// Cause some timeout on purpose.. 7 seconds!
    	Long stoptime = 7000L; 
    	
    	try {
    	    Thread.sleep(stoptime);
    	} catch (InterruptedException e) {
    	    e.printStackTrace();
    	}
    
    	return 5;
        }
    }
    
    class ThreadTesting implements Runnable {
        public void start() {
        }
        
        public void stop() {
        }
        
        public void run() {
    	try {
    	}
    	
    	catch (Exception e) {
    	}
        }
    }
    I'm not too sure on how to do this.

    as you see i deliberately make the doSomeMove() return the int after 7 seconds. i want to somehow run the doSomeMove() method in a thread and check it for timeout issues. If the method takes more than 5 seconds to respond, i want to stop the thread and print "player move took too long to repsond" otherwise i return the move (the int) as normal. can anyone help me with this
    Last edited by Pouncer; Jul 18th, 2008 at 05:47 PM.

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

    Re: ComputerJy, need your expert help on threads please

    Code:
    public class Test
    {
    	static Integer returnedValue;
    
    	public static void main(String[] args)
    	{
    		final Thread workingThread = new Thread(new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				try
    				{
    					Thread.sleep(7000);
    					returnedValue = 5;
    				}
    				catch (InterruptedException e)
    				{
    				}
    			}
    		});
    		final Thread checkingThread = new Thread(new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				try
    				{
    					Thread.sleep(5000);
    					if (workingThread.isAlive() || returnedValue == null)
    					{
    						System.err.println("Timed out");
    					}
    				}
    				catch (InterruptedException e)
    				{
    				}
    			}
    		});
    		workingThread.start();
    		checkingThread.start();
    	}
    }
    "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
    Join Date
    Sep 2005
    Posts
    1,364

    Re: ComputerJy, need your expert help on threads please

    hey ComputerJy that is perfect and works good! but is there a way to have the working thread as a seperate class which returns 5?

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

    Re: ComputerJy, need your expert help on threads please

    No you can't, because a thread does not return any values
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: ComputerJy, need your expert help on threads please

    I played around with this a bit, and got this done - its working fine

    Code:
    public class Test {
        
        public static void main(String[] args) throws InterruptedException {
    
    	ThreadTesting tt = new ThreadTesting();
    	Thread t = new Thread(tt);
       
            long patience = 2000;
    	long startTime = System.currentTimeMillis();
    
    	t.start();
    
    	
    	while (t.isAlive()) {
    
    	    // Keep checking if threads time goes over 'patience'  seconds..
    	    if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive()) {
    		System.out.println("timeout......");
    		t.stop();
    		break;
    	    }
    
    	    
    	    if (tt.getValue() != null) {
    		System.out.println("Got a return value: " + tt.getValue());
    		t.stop();
    		break;
    	    }
    	}
    	
        }
    }
    
    class GamePlayer {
        int doSomeMove() {
    	// Cause some timeout on purpose.. 7 seconds!
    	try {
    	    Thread.sleep(7000);
    	} 
    	catch (InterruptedException e) { }
    	
    	
    	return 5;
        }
    }
    
    class ThreadTesting implements Runnable {
        Integer returnedValue;
       
    
        public void start() {
        }
        
        public void stop() {
        }
        
        public void run() {
    	try {
    	    GamePlayer gp = new GamePlayer();
    	    
    	    returnedValue = gp.doSomeMove();
    	}
           
    	catch (Exception e) {
    	}
        }
    	
        public Integer getValue() {
    	return returnedValue;
        }
    }
    but what i need to do now is also print the time it took to return a value from doSomeMove() (if there wasnt a timeout..) any ideas mate?

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

    Re: ComputerJy, need your expert help on threads please

    Code:
    import java.util.Date;
    
    class GamePlayer
    {
    	int doSomeMove()
    	{
    		// Cause some timeout on purpose.. 7 seconds!
    		try
    		{
    			Thread.sleep(7000);
    		}
    		catch (final InterruptedException e)
    		{
    		}
    
    		return 5;
    	}
    }
    
    public class Test
    {
    
    	@SuppressWarnings("deprecation")
    	public static void main(final String[] args) throws InterruptedException
    	{
    
    		final ThreadTesting tt = new ThreadTesting();
    		final Thread t = new Thread(tt);
    
    		final long patience = 2000;
    		final long startTime = System.currentTimeMillis();
    
    		t.start();
    
    		while (t.isAlive())
    		{
    
    			// Keep checking if threads time goes over 'patience' seconds..
    			if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive())
    			{
    				System.out.println("timeout......");
    				t.stop();
    				break;
    			}
    
    			if (tt.getValue() != null)
    			{
    				System.out.println("Got a return value: " + tt.getValue());
    				t.stop();
    				break;
    			}
    		}
    
    	}
    }
    
    class ThreadTesting implements Runnable
    {
    	Integer returnedValue;
    
    	public Integer getValue()
    	{
    		return returnedValue;
    	}
    
    	public void run()
    	{
    		try
    		{
    			final GamePlayer gp = new GamePlayer();
    
    			Date d1 = new Date();
    			returnedValue = gp.doSomeMove();
    			Date d2 = new Date();
    			long val = d2.getTime() - d1.getTime();
    			System.out.println("Operation took " + val + " Milliseconds to complete");
    		}
    
    		catch (final Exception e)
    		{
    		}
    	}
    
    	public void start()
    	{
    	}
    
    	public void stop()
    	{
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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