--- old/test/java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java 2014-02-23 17:06:56.000000000 +0800 +++ new/test/java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java 2014-02-23 17:06:55.000000000 +0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -76,6 +76,8 @@ final AtomicLong approximateCount = new AtomicLong(0L); abstract class CheckedThread extends Thread { + protected volatile boolean stopRequest = false; + CheckedThread(String name) { super(name); setDaemon(true); @@ -87,15 +89,23 @@ } /** Polls occasionally for quitting time. */ protected boolean quittingTime(long i) { - return (i % 1024) == 0 && quittingTime(); + return stopRequest || quittingTime() && (i % 1024 == 0 || i < 1024); + } + protected void giveupCPU(){ + try { + Thread.sleep(0L); + } catch (InterruptedException ignore) {} } abstract protected void realRun(); public void run() { try { realRun(); } catch (Throwable t) { unexpected(t); } } + public void stopThread() { + stopRequest = true; + } } - Thread offerer = new CheckedThread("offerer") { + CheckedThread offerer = new CheckedThread("offerer") { protected void realRun() { final long chunkSize = getRandom().nextInt(maxChunkSize) + 2; long c = 0; @@ -104,13 +114,13 @@ if ((++c % chunkSize) == 0) { approximateCount.getAndAdd(chunkSize); while (approximateCount.get() > maxQueueSize) - Thread.yield(); + giveupCPU(); } } else { - Thread.yield(); + giveupCPU(); }}}}; - Thread remover = new CheckedThread("remover") { + CheckedThread remover = new CheckedThread("remover") { protected void realRun() { final long chunkSize = getRandom().nextInt(maxChunkSize) + 2; long c = 0; @@ -120,14 +130,14 @@ approximateCount.getAndAdd(-chunkSize); } } else { - Thread.yield(); + giveupCPU(); } } q.clear(); approximateCount.set(0); // Releases waiting offerer thread }}; - Thread scanner = new CheckedThread("scanner") { + CheckedThread scanner = new CheckedThread("scanner") { protected void realRun() { final Random rnd = getRandom(); while (! quittingTime()) { @@ -138,10 +148,10 @@ (Arrays.asList(q.toArray(new Long[0]))); break; } - Thread.yield(); + giveupCPU(); }}}; - for (Thread thread : new Thread[] { offerer, remover, scanner }) { + for (CheckedThread thread : new CheckedThread[] { offerer, remover, scanner }) { thread.join(timeoutMillis + testDurationMillis); if (thread.isAlive()) { System.err.printf("Hung thread: %s%n", thread.getName()); @@ -149,7 +159,7 @@ for (StackTraceElement e : thread.getStackTrace()) System.err.println(e); // Kludge alert - thread.stop(); + thread.stopThread(); thread.join(timeoutMillis); } }