I have a small task that continually gets triggered. How would I use Threads effectively to make sure the previous one always finishes before the next one gets started? If this was a normal piece of code I'd probably use some kind of flag. But this involves calling another piece of code (which I've no control over) to speak to the user. I pass the method a string to be spoken. The invisible processing takes care of everything else. Immediately afterwards, some other string gets passed to the method for speaking and the first one still hasn't finished. I have no way of telling when the first method finishes. I'm thinking about using Threads in some way to improve my calls to the speaking method. I know this might sound a little obscure but has anyone got any hints or ideas about a way to do this? Thanks!
Hi Paul, Since you want to perform operations sequencially you are not going to be using Threads effectively anyway. The idea with threads is that more than one thing can be happening at almost the same time (i.e., painting components, user interactions, etc.). By making the Threads work in sequence you are just adding more overhead and making execution time longer for no good reason. If you want sequencial then just call a method. If you don't like that then you can generate events (at least that will happen in some order and can be behind where you are at now). Regards, Manfred.
Paul, You could make the method synchronized and then use the wait() and notify() methods to ensure your thread doesn't execute until the previous one is finished. You can set a flag to represent a currently executing thread. When another thread calls this method, if the flag is set it goes into a waiting pool. When the current thread finishes, your method calls notify() and this selects a thread from the wait pool. I think the only problem may be if you have multiple threads in the wait pool... you won't know which one will be activated. Chad