/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code 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 code 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 in the LICENSE file that * accompanied this code). * * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd.
*/
/** * a reserialized semaphore has same number of permits and * fairness, but no queued threads
*/ publicvoid testSerialization() { testSerialization(false); } publicvoid testSerialization_fair() { testSerialization(true); } publicvoid testSerialization(boolean fair) { try {
Semaphore s = new Semaphore(3, fair);
s.acquire();
s.acquire();
s.release();
/** * release in one thread enables acquire in another thread
*/ publicvoid testReleaseAcquireDifferentThreads_acquire() { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquire); } publicvoid testReleaseAcquireDifferentThreads_acquire_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquire); } publicvoid testReleaseAcquireDifferentThreads_acquireN() { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireN); } publicvoid testReleaseAcquireDifferentThreads_acquireN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireN); } publicvoid testReleaseAcquireDifferentThreads_acquireUninterruptibly() { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireUninterruptibly); } publicvoid testReleaseAcquireDifferentThreads_acquireUninterruptibly_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireUninterruptibly); } publicvoid testReleaseAcquireDifferentThreads_acquireUninterruptiblyN() { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireUninterruptibly); } publicvoid testReleaseAcquireDifferentThreads_acquireUninterruptiblyN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireUninterruptibly); } publicvoid testReleaseAcquireDifferentThreads_tryAcquireTimed() { testReleaseAcquireDifferentThreads(false, AcquireMethod.tryAcquireTimed); } publicvoid testReleaseAcquireDifferentThreads_tryAcquireTimed_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.tryAcquireTimed); } publicvoid testReleaseAcquireDifferentThreads_tryAcquireTimedN() { testReleaseAcquireDifferentThreads(false, AcquireMethod.tryAcquireTimedN); } publicvoid testReleaseAcquireDifferentThreads_tryAcquireTimedN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.tryAcquireTimedN); } publicvoid testReleaseAcquireDifferentThreads(boolean fair, final AcquireMethod acquirer) { final Semaphore s = new Semaphore(0, fair); finalint rounds = 4; long startTime = System.nanoTime(); Thread t = newStartedThread(new CheckedRunnable() { publicvoid realRun() throws InterruptedException { for (int i = 0; i < rounds; i++) {
assertFalse(s.hasQueuedThreads()); if (i % 2 == 0)
acquirer.acquire(s); else
acquirer.acquire(s, 3);
}}});
for (int i = 0; i < rounds; i++) { while (! (s.availablePermits() == 0 && s.hasQueuedThreads())) Thread.yield();
assertTrue(t.isAlive()); if (i % 2 == 0)
s.release(); else
s.release(3);
}
awaitTermination(t);
assertEquals(0, s.availablePermits());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
/** * fair locks are strictly FIFO
*/ publicvoid testFairLocksFifo() { final PublicSemaphore s = new PublicSemaphore(1, true); final CountDownLatch pleaseRelease = new CountDownLatch(1); Thread t1 = newStartedThread(new CheckedRunnable() { publicvoid realRun() throws InterruptedException { // Will block; permits are available, but not three
s.acquire(3);
}});
waitForQueuedThread(s, t1);
Thread t2 = newStartedThread(new CheckedRunnable() { publicvoid realRun() throws InterruptedException { // Will fail, even though 1 permit is available
assertFalse(
s.tryAcquire(randomExpiredTimeout(), randomTimeUnit()));
assertFalse(
s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit()));
// untimed tryAcquire will barge and succeed
assertTrue(s.tryAcquire());
s.release(2);
assertTrue(s.tryAcquire(2));
s.release();
pleaseRelease.countDown(); // Will queue up behind t1, even though 1 permit is available
s.acquire();
}});
/** * toString indicates current number of permits
*/ publicvoid testToString() { testToString(false); } publicvoid testToString_fair() { testToString(true); } publicvoid testToString(boolean fair) {
PublicSemaphore s = new PublicSemaphore(0, fair);
assertTrue(s.toString().contains("Permits = 0"));
s.release();
assertTrue(s.toString().contains("Permits = 1"));
s.release(2);
assertTrue(s.toString().contains("Permits = 3"));
s.reducePermits(5);
assertTrue(s.toString().contains("Permits = -2"));
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.18Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.