简体   繁体   中英

Pausing and resuming a method

I'm having some troubles pausing and resuming a method. (Using Java in Eclipse).

Basically I'm writing a program that works a bit like a compiler. I give it a string and it interprets this string, converts it in a block of n commands,conditions and things like loops (depending on the strong) and executes those commands. Like so:

(while 
  (energy-at-least 1000)
  (seq
    (move) 
    (turn clockwise)
  )
)

Currently I have a method that stops at the Nth command, but I'm unsure of how to continue after this, since reusing this method and telling it to start at the Nth+1 command mmakes it forget that the program is in loop(s).

Sorry for the poor explanation, but basically I need to be able to stop this method at the Nth command and let it resume from the course it was following. Java had methods for these (resume() and stop() ) but they are deprecated I've seen. Anybody have a good idea?

An easy way to do this would be to use wait() and notify() methods. Of course, it will depend on how many parallel threads you'll have running since the notify() method cannot guarantee you which thread will actually be awaken.

From what I understand your saying is that you need more fine grained control over the loop in the thread then methods like 'notify', 'resume', etc. are offering. You can do such a thing like this:

The class of the thread could look like this:

public WhateverLoop implements Runnable {
    private volatile boolean run = true;

    public void run() {
        while(run) {
            doWhatever();
        }        
    }

    public setRun(boolean run) {
        this.run = run;
    }
}

The 'volatile' part is very important. It enables other threads to influence the 'run' variable (basically it prevents threads copying it into their own memory). Otherwise changes from other threads to the variable won't be visible in the thread.

The controlling class could do this:

WhateverLoop whateverLoop = new WhateverLoop();
Thread WhateverLoopThread = new Thread(whateverLoop);
whateverLoopThread.start();
// Do stuff which consumes time.
...
// Stop the loop
whateverLoop.setRun(false);

In this example I used a boolean, but obviously you can make it as complicated as you want (or not).

One way to do this is to represent the execution state in such a way that the subset that is required to resume can be stored in heap objects reachable from a "context" object.

To pause execution, you get the interpretter to record all relevant state in a context object, and return it. To resume, you call the interpretter passing in the previously used context.

This won't work if your interpretter does a recursive walk of the program tree to execute it ...


Another way to do this is to implement "pause" by having the interpretter execute a callback to your CLI (or whatever it is that handles stuff while the program is paused). The CLI "resumes" execution by returning from the callback method.

Have you considered controlling it from a BlockingQueue ?

Start it with a queue of N instructions for it to execute. It will pull each instruction from the queue and process it. Once it reaches the Nth instruction it will stop/block. To start it again from where it left off, push more instructions into the queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM