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?
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:
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:
In this example I used a boolean, but obviously you can make it as complicated as you want (or not).