Results 1 to 7 of 7
Thread: suspend a method
- 10-11-2009, 04:11 PM #1
Member
- Join Date
- Aug 2008
- Location
- Riyadh - Saudi Arabia
- Posts
- 15
- Rep Power
- 0
suspend a method
How can I suspend a method when click on swing button?
I try this but it not wok
class WORK
{
public boolean continue;
public void PROCESSING()
{
while(this.continue)
{
//------do any thing
}
}
}
private void STOPActionPerformed(java.awt.event.ActionEvent evt)
{
work.continue = false;
}
any suggestion will be great
- 10-11-2009, 04:27 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What is the method doing? Read about SwingWorker.
- 10-12-2009, 11:26 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
Try making that "continue" variable static ;
- 10-12-2009, 11:32 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-12-2009, 11:56 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
Sorry....
My bad.
a little misunderstanding......
- 10-12-2009, 12:00 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Yeah, variables called "continue" don't go down too well with the compiler. Making them static doesn't fool it.
- 10-21-2009, 08:36 PM #7
I don't know of any low level or internal way to pause, with the intention to resume general piece of code.
This example kind of demonstrates how pausing could be done on some worker thread. but requires some building in of the logic into the worker thread to check for the paused flag status., and this is then very specific to each implementation of the thing that you are trying to do.
This of course also only makes sense if the thing we want to pause is a loop of many iterations executing relatively small amount of run time operations, where the total time would be a longer time to run, such as copying bytes between streams, writing to a file, or inserting to a database.Java Code:/** * Controls a worker thread, starts it up, invokes the 'pause' operation after 4 seconds. 4 seconds later unpauses it. The worker thread just writes out a message every 1 second. */ public class ThreadSleep { @Test public void run() throws Exception { worker w = new worker(); Thread t1 = new Thread(w); t1.start(); try { Thread.sleep(4000); } catch (InterruptedException ex) { } System.out.println("setting paused state on worker thread."); w.paused = true; try { Thread.sleep(4000); } catch (InterruptedException ex) { } System.out.println("unpausing worker thread."); w.paused = false; t1.join(); } } class worker implements Runnable { public boolean paused = false; public void run() { for (int i = 0; i < 10; i++) { while (paused == true) { try { Thread.sleep(20); } catch (InterruptedException ex) { } } // the usual stuff System.out.println("<<doing some action>> (i=" + i + ")"); try { Thread.sleep(1000); } catch (InterruptedException ex) { } } } }
Another interesting idea might to design an interface that might be used in conjunction with threads and the serializable interface, where a generic implementation could be a wrapper for general tasks of a given pattern. Though its likely each different problem would require its own implementation, at least the API for having something suspended would be consistent.
Java Code:/** * methods that a general worker class would implement to allow us to have it suspended, with the intention of later resuming it, or possibly having a suspended worker serialized out to offline storage for eventual reloading to resume at a future date, or possibly to have it reloaded on a different system. */ public interface SuspendableWorker extends Serializable { /** * States we define the worker to be in. The transitions between these are what we need to do to handle suspending. FINISHED is the natural worker is finished state, where it would not make sense to suspend a finished worker. */ enum State { RUNNING, SUSPENDED, OFFLINE, FINISHED }; /** * gets the current state of this worker instance */ State getState(); /** * Has this [running] worker suspended. Once suspended it can be thawed, or stored. */ void freeze(); /** * unsuspends a suspended worker. next state is running. */ void thaw(); /** * does what ever is needed to have this suspended worker state serialized to offline storage. */ void store(ObjectOutputStream out); /** * does what ever is needed to restore an offline worker into the suspended state. */ void load(ObjectInputStream in); /** * The method you would implement to do the actual work */ void service(); }
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks