testsync.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 2,769 行 · 第 1/5 页

JAVA
2,769
字号
/* * @(#)TestSync.java	1.10 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * *//* * @(#)TestSync.java	1.4 03/11/04 * The TestSync class tests miscellaneous uneven locking scenarios. */class TestSyncT2Locker extends Thread{    TestSyncInflater inflater;    boolean done;    boolean ready;    boolean waiting;    Object objToLock;    Object request;    Object requestDone;    int requestType;    static final int LOCK = 1;    static final int UNLOCK = 2;    static final int SYNC = 3;    static final int UNSYNC = 4;    static final int INFLATE = 5;    TestSyncT2Locker(TestSyncInflater _inflater) {        inflater = _inflater;        ready = false;        request = new Object();        requestDone = new Object();        objToLock = null;    }    void init() {        done = false;        this.start();    }    void terminate() {        done = true;        synchronized(request) {            request.notifyAll(); // Wake up locker thread.        }    }    public void run() {        while (!done) {            doRun();	}    }    private void doRun() {            // Step 2: Lock the request lock and declare the locker ready.            synchronized(request) {                ready = true;                try {                    // Step 3: Wait for a request.                    request.wait();                    ready = false;                } catch (InterruptedException e) {                }                if (!done) {                    while (!waiting) {                        try {                            Thread.sleep(10);                        } catch (InterruptedException e) {                        }                    }                    // Step 7: Lock of unlock the object.                    if (requestType == LOCK) {                        TestSyncLocker.monitorEnter(objToLock);		    } else if (requestType == UNLOCK) {			TestSyncLocker.monitorExit(objToLock);		    } else if (requestType == SYNC) {                        synchronized(objToLock) {                            // Now wait for the next request which should                            // be for unsync:                            synchronized(requestDone) {                                requestDone.notifyAll();		            }                            doRun();			}                    } else if (requestType == UNSYNC) {		    } else if (requestType == INFLATE) {			inflater.inflate(objToLock);		    }                    // Step 8: Tell the requester that we're done.                    synchronized(requestDone) {                        requestDone.notifyAll();		    }                }            }    }    void monitorEnter(Object o) {	request(o, LOCK);    }    void monitorExit(Object o) {	request(o, UNLOCK);    }    void sync(Object o) {	request(o, SYNC);    }    void unsync(Object o) {	request(o, UNSYNC);    }    void inflate(Object o) {	request(o, INFLATE);    }    private void request(Object o, int requestVal) {        // Step 1: Wait for the locker thread to get ready:        while (!ready) {            try {                Thread.sleep(10);            } catch (InterruptedException e) {            }        }        // Step 4: Lock the request lock and make a request.        synchronized(request) {            waiting = false;            objToLock = o;            requestType = requestVal;            // Step 5 : Tell the locker thread to wake up and handle the            // request.            request.notifyAll();        }        // Step 6: Lock the object to be locked and wait for        // the requested to contend and notify it.        synchronized(requestDone) {            try {                waiting = true;                requestDone.wait();            } catch (InterruptedException e) {            }        }        // The only way we can get here is if the locked boolean has been        // set to true.  The only way that can happen is if the locker        // thread managed to wait on the object and the requester thread gets        // to notify the locker thread to wake up from that wait.  Hence,        // contention (and therefore inflation) must have happened.    }}class TestSyncInflater extends Thread{    boolean done;    boolean ready;    boolean waiting;    Object objToInflate;    Object request;    TestSyncInflater() {        ready = false;        request = new Object();        objToInflate = null;    }    void init() {        done = false;        this.start();    }    void terminate() {        done = true;        synchronized(request) {            request.notifyAll(); // Wake up inflater thread.        }    }    public void run() {        while (!done) {            // Step 2: Lock the request lock and declare the inflater ready.            synchronized(request) {                ready = true;                try {                    // Step 3: Wait for a request.                    request.wait();                    ready = false;                } catch (InterruptedException e) {                }                if (!done) {                    while (!waiting) {                        try {                            Thread.sleep(10);                        } catch (InterruptedException e) {                        }                    }                    // Step 7: Contend on the object to be inflated and notify                    // the inflater once we have contended on it.                    synchronized(objToInflate) {                        objToInflate.notifyAll();                    }                    try {                        Thread.sleep(10);                    } catch (InterruptedException e) {                    }                }            }        }    }    void inflate(Object o) {        // Step 1: Wait for the inflater thread to get ready:        while (!ready) {            try {                Thread.sleep(10);            } catch (InterruptedException e) {            }        }        // Step 4: Lock the request lock and make a request.        synchronized(request) {            waiting = false;            objToInflate = o;            // Step 5 : Tell the inflater thread to wake up and handle the            // request.            request.notifyAll();        }        // Step 6: Lock the object to be inflated and wait for        // the requested to contend and notify it.        synchronized(objToInflate) {            try {                waiting = true;                objToInflate.wait();            } catch (InterruptedException e) {            }        }        // The only way we can get here is if the inflated boolean has been        // set to true.  The only way that can happen is if the inflater        // thread managed to wait on the object and the requester thread gets        // to notify the inflater thread to wake up from that wait.  Hence,        // contention (and therefore inflation) must have happened.    }}public class TestSync{    static int level = 0;    static int total = 0;    static int failed = 0;    static int passed = 0;    static TestSyncInflater inflater =  new TestSyncInflater();    static TestSyncT2Locker locker =  new TestSyncT2Locker(inflater);    static TestSyncT2Locker t2 = locker;    public static void main(String[] args) throws Exception {        long maxReentryCount = 0;        boolean runLargeReentryLockingTest = false;	boolean needToTestUnstructuredLocks = false;	int usedArg = -1;        try {	    // Check for the "testUnstructuredLocks" arg:	    for (int i = 0; i < args.length; i++) {		// NOTE: JDK 1.4's javac generate a catch block that catches		// the same IllegalMonitorStateException that it throws. See		// bug reports at		// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6271353 and		// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4414101.		// However, the javac team has determined this to not be a bug		// due to various reasons. But in CVM's case, this unfortunately		// results in an infinite loop due to CVM supporting unstructured		// locks.  For now, we disable these tests by default for		// convenience.		if (args[i].equals("testUnstructuredLocks")) {		    needToTestUnstructuredLocks = true;		    usedArg = i;		    break;		}	    }	    // Check for the "maxReentryCount" arg:	    for (int i = 0; i < args.length; i++) {		if (i != usedArg) {		    maxReentryCount = Long.parseLong(args[0]);		    runLargeReentryLockingTest = true;		}	    }        } catch (Exception e) {            maxReentryCount = 0;            runLargeReentryLockingTest = false;        }        inflater.init();        locker.init();        testHoldsLock();	if (needToTestUnstructuredLocks) {	    testExcessMonitorExit();	    testExcessMonitorEnter();	}        if (runLargeReentryLockingTest) {            testLargeReentryLocking(maxReentryCount);        }        locker.terminate();        inflater.terminate();        System.gc();        System.out.println();        System.out.println("Passed: " + passed);        System.out.println("Failed: " + failed);        System.out.println("Total:  " + total);    }    static void monitorEnter(Object o) {        TestSyncLocker.monitorEnter(o);    }    static void monitorExit(Object o) {        TestSyncLocker.monitorExit(o);    }    static void inflate(Object o) {        inflater.inflate(o);    }    static void    passIf(boolean success, String message) {        String status;        if (success) {            passed++;            status = "PASSED";        } else {            failed++;            status = "FAILED";        }        total++;        System.out.println(status + " " + total + ": " + message);    }    static void    passIf(boolean success, String message, int level, int expected) {        passIf(success, message);        if (!success || (level != expected)) {            System.out.println("    Expected level = " + expected);            System.out.println("    Actual level = " + level);        }    }    static void decLevels(Object o) {        for (int i = 0; i < 10; i++) {            monitorExit(o);            level--;        }    }    //======================================================================    synchronized void syncF() {        level++;        level--;    }    synchronized void syncFX() {        level++;        monitorExit(this);        level--;    }    synchronized void syncFE() {        level++;        monitorEnter(this);        level--;    }    synchronized void syncFIX() {        level++;        inflate(this);        monitorExit(this);        level--;    }    synchronized void syncFIE() {        level++;        inflate(this);        monitorEnter(this);        level--;    }    synchronized void syncFXI() {        level++;        monitorExit(this);        inflate(this);        level--;    }    synchronized void syncFEI() {        level++;        monitorEnter(this);        inflate(this);        level--;    }    synchronized void syncFFX() {        level++;        syncFX();        level--;    }    synchronized void syncFFE() {        level++;        syncFE();        level--;    }    synchronized void syncFFIX() {        level++;        syncFIX();        level--;    }    synchronized void syncFFIE() {        level++;        syncFIE();        level--;    }    synchronized void syncFFXI() {        level++;        syncFXI();        level--;    }    synchronized void syncFFEI() {        level++;        syncFEI();        level--;    }    synchronized void syncFIFX() {        level++;        inflate(this);        syncFX();        level--;    }    synchronized void syncFIFE() {        level++;        inflate(this);        syncFE();        level--;    }    synchronized void syncFXX() {        level++;        monitorExit(this);        monitorExit(this);        level--;    }    synchronized void syncFEE() {        level++;        monitorEnter(this);        monitorEnter(this);        level--;    }    synchronized void syncFIXX() {        level++;        inflate(this);        monitorExit(this);        monitorExit(this);        level--;    }    synchronized void syncFIEE() {        level++;        inflate(this);        monitorEnter(this);        monitorEnter(this);        level--;    }    synchronized void syncFXIX() {        level++;        monitorExit(this);        inflate(this);        monitorExit(this);        level--;    }    synchronized void syncFEIE() {        level++;        monitorEnter(this);        inflate(this);        monitorEnter(this);        level--;    }    synchronized void syncFFXX() {        level++;        syncFXX();        level--;    }    synchronized void syncFFEE() {        level++;        syncFEE();        level--;    }    synchronized void syncFFIXX() {        level++;        syncFIXX();        level--;    }    synchronized void syncFFIEE() {        level++;        syncFIEE();        level--;    }    synchronized void syncFFXIX() {        level++;        syncFXIX();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?