Java Source

Example of Java source used in Thread Performance Tests demo.
/*
 * @(#)ThreadPerformanceTest.java Patrick Chan
 *
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import awt.*;
import browser.*;

class ThreadPerformanceTestT extends Thread {
    public void run() {};
} 

/* Used for context switch timing. */
class ThreadPerformanceTestT1 extends Thread {
    ThreadPerformanceTestT1 other;
    boolean die = false;
    int count = 0;

    public void run() {
        synchronize (this) {
            while (!die) {
                other.wakeup();
                wait();
                count++;
            }
        }
        other.wakeup();
    }

    synchronized void wakeup() {
        notify();
    }

} 

class ThreadPerformanceTest extends PerformanceTest {
    /* Title used for printout. */
    String testTitle = "Thread Performance Test";

    /* Title for each test. */
    String testLabels[] = {
        "context switch",
        "(new Thread()).join()",
        "activeCount()",
        "countStackFrames()",
        "currentThread()",
        "enumerate(Thread[])",
        "getName()",
        "getPriority()",
        "isAlive()",
        "isDaemon()",
        "setName(\"Thread\")",
        "setPriority(NORM_PRIORITY)",
        "setThreadPriority(NORM_PRIORITY)",
        "sleep(0)",
        "yield()"
    };

    /**
     * Initialize the test. 
     */
    public void init() {
        init(testTitle, testLabels);
    }

    Thread threads[] = new Thread[1000];
    public int test(Graphics g, int test) {
        Thread cur = Thread.currentThread();
        int count = 0;

        switch (test) {
        case -1:
            while (!stopTest) {
                count++;
            }
            return count;
        case 0:
            ThreadPerformanceTestT1 one = new ThreadPerformanceTestT1();
            ThreadPerformanceTestT1 two = new ThreadPerformanceTestT1();

            two.other = one;
            one.other = two;
            one.start();
            two.start();
            while (!stopTest) {
                Thread.currentThread().sleep(1000);
            }
            one.die = true;
            two.die = true;
            return one.count + two.count;
        case 1:
            while (!stopTest) {
                (new ThreadPerformanceTestT()).join();
                count++;
            }
            return count;
        case 2:
            while (!stopTest) {
                Thread.activeCount();
                count++;
            }
            return count;
        case 3:
            while (!stopTest) {
                cur.countStackFrames();
                count++;
            }
            return count;
        case 4:
            while (!stopTest) {
                Thread.currentThread();
                count++;
            }
            return count;
        case 5:
            while (!stopTest) {
                Thread.enumerate(threads);
                count++;
            }
            return count;
        case 6:
            while (!stopTest) {
                cur.getName();
                count++; 
            }
            return count;
        case 7:
            while (!stopTest) {
                cur.getPriority();
                count++;
            }
            return count;
        case 8:
            while (!stopTest) {
                cur.isAlive();
                count++;
            }
           return count;
        case 9:
            while (!stopTest) {
                cur.isDaemon();
                count++;
            }
            return count;
        case 10:
            while (!stopTest) {
                cur.setName("Thread");
                count++;
            }
            return count;
        case 11:
            while (!stopTest) {
                cur.setPriority(Thread.NORM_PRIORITY);
                count++;
            }
            return count;
        case 12:
            while (!stopTest) {
                cur.setThreadPriority(Thread.NORM_PRIORITY);
                count++;
            }
            return count;
        case 13:
            while (!stopTest) {
                cur.sleep(0);
                count++;
            }
            return count;
        case 14:
            while (!stopTest) {
                cur.yield();
                count++;
            }
            return count;
        }
        return 0;
    }
}



Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.