LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2007, 12:27 PM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
How do I pause a thread indefinitely and then resume it again (in Java)?


I have a program in which I want to do an animation and I have two buttons: one to start the animation and one to stop it. What I would like to do is to be able to press the Stop button to pause the animation and then press my Start button to resume it. I am using a separate thread to do the animation stuff. So far, the relevant code is as follows:

Code:
Thread t = new Thread(new Animator());

...

startButton.addActionListener(new
	    ActionListener()
	    {
		public void actionPerformed(ActionEvent e)
		{
	            t.start();    
		}
	    });

stopButton.addActionListener(new
	    ActionListener()
	    {
		public void actionPerformed(ActionEvent e)
		{
		    t.interrupt();
		}
	    });
Animator is a class that implements the Runnable interface, obviously. Initially, I can press the Start button and then press Stop to pause. Pressing Start again doesn't re-start the animation because you can't call start() more than once on a thread, so an IllegalThreadStateException is thrown. How, then, do I do what I want to do?

Thanks .
 
Old 02-18-2007, 01:36 AM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
I don't know Java but I have been using threads in Python, and I assume the functions provided are similar.

The best way of communicating between threads is queues. So you'd have thread 1 which does the GUI part, and thread 2 which does the animation.

In thread 1, whenever a key is pressed, you just put the strings "stop" or "start" into that queue.

Thread 2 would go something like this, assuming the thread only starts when you hit the button:
Code:
loop
  read(queue,no_timeout) until message=='start'
  loop
    { do animation here }
    if read(queue,timeout=0)=='stop': break
  endloop
endloop
 
Old 02-18-2007, 06:01 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Have you read about the Java Concurrency Utilities, such as handling Interrupts?

Edit: More relevant seems to be The article explaining why Thread.stop, Thread.suspend and Thread.resume are deprecated. If you don't understand the synchronisation problems, you should read up. :-)

Last edited by Proud; 02-18-2007 at 06:09 AM.
 
Old 02-18-2007, 09:19 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
as was mentioned the suspend and resume are deprecated because they are not safe..

consider something like this as an alternative..

Code:
public void run(){
    while(running){
        while(paused);
        
        //real work here
    }
}
control the paused var to give the appearance of starting and stopping the thread
 
Old 02-18-2007, 11:02 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by xhi
Code:
        while(paused);
ouch !
You CPU isn't going to like that one ...
 
Old 02-18-2007, 11:04 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Yeah, this is true. I tried to do it using wait() and notify(), but I'm having problems getting that to work (I won't post code now though).
 
Old 02-18-2007, 11:16 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by jlliagre
ouch !
You CPU isn't going to like that one ...
yeh, i guess ouch is right. im not sure where i was going with that one.

maybe something on the order of wrapping the code to be executed in an if
Code:
public void run(){
    while(running){
        if(!paused)
        {
          // do work
        }
    }
}
 
Old 02-18-2007, 12:13 PM   #8
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
xhi, surely the crux of your code's problem is the inherent concurrency issue of reading paused==true while another thread's just changed it to false in its allocated registers, but not yet saved that back to memory. This may then mean that two threads attempt to modify variables which should have mutex access, or worse.

Wikipedia's article on Monitors isn't Java specific, but a start if you're having problems with wait() and notify(). Their Java Concurrency is a little sparse. The Sun Java Concurrency Utilites page may be more help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suspend/Resume thread in Linux Ramkrishan Ravi Programming 1 02-15-2007 09:22 AM
Pause and resume Gnome Mixer Applet for Suspend-to-disk cdhgee Linux - Software 0 11-21-2005 10:44 PM
pause/resume an ssh make or other process linuxhippy Slackware 4 09-01-2005 05:03 AM
XMMS won't resume after pause Rounan Linux - Software 4 02-25-2004 02:16 AM
resume supported download app in java. juby Programming 2 03-24-2003 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration