FileDocCategorySizeDatePackage
LiveTraceCallback.javaAPI DocphoneME MR2 API (J2ME)5041Wed May 02 18:00:00 BST 2007com.sun.midp.util

LiveTraceCallback

public class LiveTraceCallback extends Object implements LiveTraceListener
Provides a rendezvous point and synchronous callback within a LiveTraceListener. An instance of LiveTraceCallback is installed into a LiveTracer tracepoint like any other LiveTraceListener. Once installed, the LiveTrace code calls a listener here, which synchronously calls a callback established by a test making a call to await(). If a callback isn't available, the trace listener blocks until one becomes available. After the callback returns, it is removed. Therefore, while the listener is active, every trace point must be matched by a call to await(). Otherwise, the thread being traced will block and all the tests will timeout. Sample usage from an i3test: tcb = new LiveTraceCallback(); targetObject.liveTracer.add(TRACE_TAG, tcb); // initiate some operation that will eventually hit TRACE_TAG assertTrue( "trace point was hit", tcb.await( new Runnable() { public void run() { // stuff executed by the traced thread } }) );

Fields Summary
public static final long
DEFAULT_TIMEOUT
The default timeout period, in milliseconds.
boolean
active
Runnable
callback
Runnable
doNothing
Constructors Summary
Methods Summary
public synchronized booleanawait(java.lang.Runnable r, long timeout)
Blocks until a tracepoint is reached, causes r.run() to be called synchronously by the traced thraed, and then lets both the traced thread and the caller continue. Times out after the indicated timeout period. If a timeout or interrupt occurs, the callback is cleared without being called.

param
r the Runnable whose run() method is to be called
param
timeout timeout period in milliseconds
return
true if the tracepoint was handled normally, false if a timeout or interrupt occurred

        long deadline = System.currentTimeMillis() + timeout;
        long remain = timeout;
        boolean retval = true;

        callback = r;
        notifyAll();

        try {
            while (callback != null && remain > 0) {
                wait(remain);
                remain = deadline - System.currentTimeMillis();
            }

            if (callback != null) {
                System.out.println("LiveTraceCallback.await: timed out");
                retval = false;
            }
        } catch (InterruptedException ie) {
            System.out.println("LiveTraceCallback.await: interrupted");
            retval = false;
        } finally {
            callback = null;
        }

        return retval;
    
public booleanawait(java.lang.Runnable r)
Same as await(r, DEFAULT_TIMEOUT).

        return await(r, DEFAULT_TIMEOUT);
    
public booleanawait()
Same as await(do nothing, DEFAULT_TIMEOUT).

        return await(doNothing, DEFAULT_TIMEOUT);
    
public synchronized voidcall(java.lang.String tag)
Called by Display's LiveTracer object after a screen change occurs.



                   
         
        if (!active) {
            return;
        }

        while (active && callback == null) {
            try {
                wait();
            } catch (InterruptedException ignore) { }
        }

        if (active) {
            callback.run();
        }

        callback = null;
        notifyAll();
    
public synchronized voidshutdown()
Shuts down this trace listener by arranging for any listener not to block and instead to be ignored. Any blocked listener is unblocked and returns without calling the callback, even if one was currently installed.

        active = false;
        notifyAll();