I want to make a thread to pause in a GUI by pressing a button and the thread can be used or free until the same button is clicked is that possible in java. and any ideas how its done ? I am using Swing GUI
-
The answer is yes.. Can you add more specifics like which UI library (Swing) :) – Adam Gent Mar 21 '12 at 00:21
-
yes i am using Swing what more information would you like more so i can edit the question and add them – dori naji Mar 21 '12 at 00:22
-
@dorinaji What do you mean you want the `Thread` to be used? You want to have it run a completely different task? – Jeffrey Mar 21 '12 at 00:31
-
You don't need to free up a thread to do other things as you don't have threads in your computer, you have CPUs. As long a thread stops/pauses another thread can use the same CPU. – Peter Lawrey Mar 21 '12 at 09:04
5 Answers
There is no easy way to pause a Thread and have it immediately free for other tasks, and then be able to resume that same thread on demand.
You can pause the thread using Thread.sleep(), and then have other threads do work, but the paused thread will be blocked and unable to do additional work until it is unpaused or interrupted.
Since a thread has to keep track of its stack and all of the variables pursuant to its execution, it would be quite difficult to pause it in one flow of execution and have it do other work before unpausing. Notably, there is no reason to even attempt to do this, since your attempt would simply be duplicating what thread already does (tracking the stack etc). So what you need to do is make another thread to do what you want when you pause the first thread.
Long story short, pause with Thread.sleep() and create additional threads to do your other work (you cannot use the paused thread for other work while it is paused).

- 7,112
- 2
- 21
- 40
You probably do not want to do the accepted answer of Thread.sleep and should probably learn how the Swing Event Queue works.
You want to look at:
http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html
Particularly:
Also see these stackoverflow posts:
You can simulate a pause by not doing anything in it.
To simulate, all you need is a boolean that is toggled by the button that the thread has reference to. If it's true, then carry out what you would normally, otherwise just sleep for a bit.
That's the only way I know how.

- 883
- 8
- 22
-
Yes i know that way but i want the thread to be free and can be used – dori naji Mar 21 '12 at 00:27
Thread.sleep(4000);
http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
Or the newer
TimeUnit.SECONDS.sleep(4);
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/TimeUnit.html
-
-
How is *this* a valid answer? I don't know why the OP accepted it. This answer doesn't say anything about the way you would signal the thread to sleep, which is the most important part, imho. – Kiril Mar 21 '12 at 01:37
-
Question was somewhat ambiguous. Pausing the thread sounded like what he was after. After rereading the question it is still not clear what the OP is after. – dbrin Mar 21 '12 at 02:11
In multithreading you can't really safely 'force' a thread to stop/pause without cooperation from the thread itself. (Java has ill-conceived methods to attempt this such Thread.suspend(). You can read about why they're deprecated.).
A common way pause a thread is to set a boolean flag and have that thread poll that state. That way it can safely pause or shutdown without causing deadlocks etc.
Also, as @Perry states a worker thread may go to sleep simply by having nothing to do. Ex. a thread to process enqueued requests. If the queue is empty then it just quietly waits for new requests.

- 5,168
- 1
- 24
- 37
-
Yes, the methods were ill-conceived. But non-discretionary thread control from within the JVM turns out to be a really hard problem ... and much harder than they realized in the Java 1.0 days. – Stephen C Mar 21 '12 at 01:13
-
@Stephen C I think it's an issue with threading in the real world. Unlike separate processes, threads share more state (ex. memory). Pthreads doesn't have methods like this. I seems as if the orig Java implementors just weren't that knowledgable about threading. – seand Mar 21 '12 at 01:21
-
-
Like like a mentioned, I think the orig Java guys were thinking "we should have a way to kill and suspend..." without studying existing APIs and carefully considering implications. Win32 also has some brain-dead thread related functions. – seand Mar 21 '12 at 01:47
-
@seand - actually pthreads do have a pthread_kill method. But what the specs don't mention (explicitly) is that killing a thread can leave the rest of the application in an indeterminate state that is (in general) impossible to recover from. This is essentially the issue that caused the Java developers to deprecate Java's `Thread` kill / stop / pause / resume methods. – Stephen C Mar 21 '12 at 05:35