Results 1 to 4 of 4
  1. #1
    Creativelymad is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default [SOLVED]Thread Doesn't Sleep

    Simply put, my problem is that I attempt to run a thread two different ways, both with different results, yet neither of them the one I want. And I cannot figure out what is going wrong to cause both attempts to come out flawed.

    I'll post relevant pieces of code.

    With this first bit, all the code operates functionally every time. But, the program refuses to sleep in each repeat of the while loop and speeds right through the loop to finish.


    Java Code:
    [COLOR="Red"][B]class[/B][/COLOR] Controller [COLOR="Red"][B]extends[/B][/COLOR] JComponent
    [COLOR="Red"][B]implements[/B][/COLOR] MouseMotionListener, MouseListener, Runnable
    {
    	Thread [COLOR="DeepSkyBlue"]t[/COLOR] = [COLOR="DarkRed"]new[/COLOR] Thread(this);
    
            [COLOR="Red"][B]public void[/B][/COLOR] mouseClicked(MouseEvent e) 
    	{
    		[COLOR="YellowGreen"]//Puts the player character in motion when the mouse is clicked[/COLOR]
                   [COLOR="DarkOliveGreen"][Unimportant variables][/COLOR]
    		[COLOR="DarkRed"]new[/COLOR] Thread(t).start();
    	}
    	
    	[COLOR="Red"][B]public void[/B][/COLOR] run()
    	{
    		[COLOR="YellowGreen"]//Pretty much moves a character step by step by altering the x and y, 
                    //than sleeping, and than repeating until it reaches it's goal.[/COLOR]
    		[COLOR="Red"][B]try[/B][/COLOR] {
    			[COLOR="Red"][B]while[/B][/COLOR] ([COLOR="DarkOliveGreen"][X or Y are not equal to area I want character to move to][/COLOR])
    			{
    				[COLOR="DarkOliveGreen"][Script that alters the x or y by +1 or -1 depending on where I need it to go][/COLOR]
    			        repaint();
    				Thread.sleep(300);
    			}
    
    		    } [COLOR="Red"][B]catch[/B][/COLOR] (InterruptedException e) {}
    	}
    }

    With this second bit of the code, I only change one thing from the original. The start() for the thread. With this version it sleeps perfectly, and the character makes the movement in the desired steps, but expectantly the action cannot be repeated since the thread dies after it's done. And this version also sends me slight errors. Not errors that end the program, but I do receive errors which I'll show after this next code.


    Java Code:
    [COLOR="Red"][B]class[/B][/COLOR] Controller [COLOR="Red"][B]extends[/B][/COLOR] JComponent
    [COLOR="Red"][B]implements[/B][/COLOR] MouseMotionListener, MouseListener, Runnable
    {
    	Thread [COLOR="DeepSkyBlue"]t[/COLOR] = [COLOR="DarkRed"]new[/COLOR] Thread(this);
    
            [COLOR="Red"][B]public void[/B][/COLOR] mouseClicked(MouseEvent e) 
    	{
    		[COLOR="YellowGreen"]//Puts the player character in motion when the mouse is clicked[/COLOR]
                   [COLOR="DarkOliveGreen"][Unimportant variables][/COLOR]
    		t.start(); [COLOR="YellowGreen"]//Here is the only difference between the first and second[/COLOR]
    	}
    	
    	[COLOR="Red"][B]public void[/B][/COLOR] run()
    	{
    		[COLOR="YellowGreen"]//Pretty much moves a character step by step by altering the x and y, 
                    //than sleeping, and than repeating until it reaches it's goal.[/COLOR]
    		[COLOR="Red"][B]try[/B][/COLOR] {
    			[COLOR="Red"][B]while[/B][/COLOR] ([COLOR="DarkOliveGreen"][X or Y are not equal to area I want character to move to][/COLOR])
    			{
    				[COLOR="DarkOliveGreen"][Script that alters the x or y by +1 or -1 depending on where I need it to go][/COLOR]
    			        repaint();
    				Thread.sleep(300);
    			}
    
    		    } [COLOR="Red"][B]catch[/B][/COLOR] (InterruptedException e) {}
    	}
    }


    The errors I receive are this:


    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    So, basically, could someone help me to get the repeatable and bug free nature of the first code, with the desired sleep effect of the second. And also, could someone help me to understand these errors I'm having?
    If this doesn't answer anything, I wouldn't mind providing the rest of the code. I simply paraphrased to the parts that I felt were the only things that could be affecting the
    sleep() method. Namely, where I created the thread, where I started it, and where I told it to sleep, since the only time it changed was when I altered the mouseClicked method.
    Last edited by Creativelymad; 03-07-2011 at 04:22 AM. Reason: Solved post at bottom

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It is simpler to use Swing Timer for your animation rather than Thread.sleep as the chances of having threading issues (the source for your errors above) are greatly reduced. You also might want to read about Concurrency in Swing.

  3. #3
    Creativelymad is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the advise. I used yesterday and today to get an idea of what swing is and does. But it seems to me to simply be a GUI toolkit. The movement isn't an animation, it's simply moving the icon's x and y on the screen step by step, moving the x and y step by step in intervals and than ending.

    There is no animation in this program right now. I've just been studying java for a few weeks now and am looking for ways to use what I've learned, and threads was that day (and still is). I really want to get this under wraps.

  4. #4
    Creativelymad is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default [solved]

    I went with the second code example in my original post and altered the run() method to create a new thread for variable t at the end of the threads life so that it becomes available to move once again afterward. It makes it repeatable. While I still receive the errors, I've learned their graphics related to calling the repaint() method when it feels it's not necessary (because I have another object calling it in the script).

    Java Code:
    [COLOR="Red"][B]public void[/B][/COLOR] run()
    	{
    		[COLOR="YellowGreen"]//Pretty much moves a character step by step by altering the x and y, 
                    //than sleeping, and than repeating until it reaches it's goal.[/COLOR]
    		[COLOR="Red"][B]try[/B][/COLOR] {
    			[COLOR="Red"][B]while[/B][/COLOR] ([COLOR="DarkOliveGreen"][X or Y are not equal to area I want character to move to][/COLOR])
    			{
    				[COLOR="DarkOliveGreen"][Script that alters the x or y by +1 or -1 depending on where I need it to go][/COLOR]
    			        repaint();
    				Thread.sleep(300);
    			}
    
    		    } [COLOR="Red"][B]catch[/B][/COLOR] (InterruptedException e) {}
                        [COLOR="Blue"]t[/COLOR] = new Thread([COLOR="DarkRed"]this[/COLOR]); [COLOR="YellowGreen"]//Here is the change[/COLOR]
    	}
    Thanks for introducing swing to me Fubarable.
    Last edited by Creativelymad; 03-07-2011 at 10:07 AM.

Similar Threads

  1. Thread.sleep
    By Gog in forum New To Java
    Replies: 3
    Last Post: 01-13-2011, 08:14 AM
  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. Do I have to put Thread to sleep?
    By atch in forum New To Java
    Replies: 0
    Last Post: 03-08-2010, 10:21 AM
  5. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM

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
  •