FileDocCategorySizeDatePackage
MethodTimes.javaAPI DocJ2ME MIDP 2.02490Thu Nov 07 12:02:16 GMT 2002example

MethodTimes

public class MethodTimes extends MIDlet implements Runnable
An example MIDlet runs a simple timing test When it is started by the application management software it will create a separate thread to do the test. When it finishes it will notify the application management software it is done. Refer to the startApp, pauseApp, and destroyApp methods so see how it handles each requested transition.

Fields Summary
Thread
thread
Constructors Summary
Methods Summary
public voiddestroyApp(boolean unconditional)
Destroy must cleanup everything. The thread is signaled to stop and no result is produced.

	thread = null;
	System.out.println("destroyApp");
    
voidempty()
An Empty method.

    
public voidpauseApp()
Pause signals the thread to stop by clearing the thread field. If stopped before done with the iterations it will be restarted from scratch later.

	thread = null;
	System.out.println("pauseApp");
    
public voidrun()
Run the timing test, measure how long it takes to call a empty method 1000 times. Terminate early if the current thread is no longer the thread from the

        Thread curr = Thread.currentThread();  // Remember current thread
    	long start = System.currentTimeMillis();
    	for (int i = 0; i < 1000000 && thread == curr; i++) {
    	    empty();
    	}
    	long end = System.currentTimeMillis();

    	// Check if timing was aborted, if so just exit
        // The rest of the application has already become quiescent.
    	if (thread != curr) {
    	    return;
    	}
    	long millis = end - start;
        // Reporting the elapsed time is outside the scope of this example.
	System.out.println("Elapsed " + millis + " ms.");
	// All done cleanup and quit
        destroyApp(true);
    	notifyDestroyed();
    
public voidstartApp()
Start creates the thread to do the timing. It should return immediately to keep the dispatcher from hanging.

    	thread = new Thread(this);
    	thread.start();
    	System.out.println("startApp");