FileDocCategorySizeDatePackage
LoopThread.javaAPI DocJMF 2.1.1e3302Mon May 12 12:20:44 BST 2003com.sun.media.util

LoopThread

public abstract class LoopThread extends MediaThread
LoopThread A looping thread that implements a safe way of pausing and restarting. Instead of using suspend() and resume() from the thread class, a pause() and start() method is provided. To use it, you will: - subclass from it; - overwrite the process() callback; - call start() to initiate the thread.
version
1.9, 98/09/28

Fields Summary
protected boolean
paused
protected boolean
started
protected boolean
killed
private boolean
waitingAtPaused
Constructors Summary
public LoopThread()
The pause flag determines the initial state.


                
      
	setName("Loop thread");
    
Methods Summary
public synchronized voidblockingPause()
Same as pause except that it blocks until the loop has actually paused (at waitHereIfPaused).

	if (waitingAtPaused || killed) return;
	paused = true;
	waitForCompleteStop();
    
public booleanisPaused()

	return paused;
    
public synchronized voidkill()

	killed = true;
	notifyAll();
    
public synchronized voidpause()
Set the paused state to true. The thread will halt at the beginning of the loop at the next iteration. i.e., unlike suspend(), the thread will NOT pause immediately. It will execute until the next waitHereIfPaused() is encountered.

	paused = true;
    
protected abstract booleanprocess()
process callback function.

public voidrun()
The run method for the Thread class.

	for (;;) {

	    // Wait here if pause() was invoked.  Until start() is called,
	    // the thread will wait here indefinitely.
	    if (!waitHereIfPaused())
		break;

	    // Invoke the callback function.
	    if (!process())
		break;
	}
    
public synchronized voidstart()
Resume the loop at the beginning of the loop where waitHereIfPaused() is called.

	if (!started) {
	    super.start();
	    started = true;
	}
	paused = false;
	notifyAll();
    
public synchronized voidwaitForCompleteStop()
Wait until the loop has come to a complete stop.

	try {
	    while (!killed && !waitingAtPaused && paused)
		wait();
	} catch (InterruptedException e) { }
    
public synchronized voidwaitForCompleteStop(int millis)
Wait until the loop has come to a complete stop. Time out after the given milli seconds.

	try {
	    if (!killed && !waitingAtPaused && paused)
		wait(millis);
	} catch (InterruptedException e) { }
    
public synchronized booleanwaitHereIfPaused()
Put the thread in a wait() if the paused state is on. Otherwise, it will just proceed.

	if (killed)
	    return false;
	waitingAtPaused = true;
	if (paused)
	    notifyAll();	// notify the blocking pause.
	try {
	    while (!killed && paused)
		wait();
	} catch (InterruptedException e) {
	    System.err.println("Timer: timeLoop() wait interrupted " + e);
	}
	waitingAtPaused = false;
	if (killed)
	    return false;
	return true;