Results 1 to 7 of 7
  1. #1
    mtz1406 is offline Member
    Join Date
    Aug 2008
    Location
    Riyadh - Saudi Arabia
    Posts
    15
    Rep Power
    0

    Default 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What is the method doing? Read about SwingWorker.

  3. #3
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    Try making that "continue" variable static ;

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by suhassr View Post
    Try making that "continue" variable static ;
    You don't want to be defenestrated now, do you?

  5. #5
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    Sorry....
    My bad.
    a little misunderstanding......

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Yeah, variables called "continue" don't go down too well with the compiler. Making them static doesn't fool it.

  7. #7
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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.

    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) {
              
          
          }
          
          
        }
      }
    }
    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.

    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

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  3. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •