Last active
December 20, 2015 16:38
-
-
Save ytoshima/6162497 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.net.*; | |
// import java.util.concurrent.Phaser; | |
// Try mutex/condvar style. | |
// If it didn't work, try semaphore | |
// test for JDK-8006395 | |
// Racey test, will not always fail, but if it does then we have a problem. | |
public class Race15 { | |
static final Object sync = new Object(); | |
static final int NTHREADS = 100; | |
static volatile int nStarted = 0; | |
static int getNStarted() { | |
synchronized(sync) { | |
return nStarted; | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
ServerSocket ss = new ServerSocket(0); | |
try { | |
final int port = ss.getLocalPort(); | |
//final Phaser phaser = new Phaser(101); | |
for (int i=0; i<NTHREADS; i++) { | |
final Socket s = new Socket("localhost", port); | |
s.setSoLinger(false, 0); | |
Socket sa = ss.accept(); | |
try { | |
sa.setSoLinger(false, 0); | |
final InputStream is = s.getInputStream(); | |
Thread[] threads = new Thread[NTHREADS]; | |
synchronized (Race15.sync) { | |
Race15.nStarted = 0; | |
} | |
for (int j=0; j<NTHREADS; j++) { | |
threads[j] = new Thread() { | |
public void run() { | |
try { | |
//phaser.arriveAndAwaitAdvance(); | |
synchronized (Race15.sync) { | |
Race15.nStarted += 1; | |
try { | |
Race15.sync.wait(); | |
} catch (InterruptedException ie) { | |
Thread t = Thread.currentThread(); | |
System.err.println(t.getName() + " in wait: " + ie); | |
} | |
} | |
while (is.read() != -1) | |
Thread.sleep(50); | |
} catch (Exception x) { | |
if (!(x instanceof SocketException | |
&& x.getMessage().equals("Socket closed"))) | |
x.printStackTrace(); | |
} | |
}}; | |
} | |
for (int j=0; j<NTHREADS; j++) | |
threads[j].start(); | |
(new Thread("notifier") { | |
public void run() { | |
//phaser.arriveAndAwaitAdvance(); | |
while (getNStarted() < (NTHREADS+1)) { | |
try { Thread.sleep(20); } catch (InterruptedException ie) {} | |
} | |
//System.out.println("notifyAll"); | |
synchronized(sync) { | |
sync.notifyAll(); | |
} | |
} | |
}).start(); | |
synchronized(sync) { | |
Race15.nStarted += 1; | |
try { | |
sync.wait(); | |
} catch (InterruptedException ie) {} | |
} | |
s.close(); | |
for (int j=0; j<NTHREADS; j++) | |
threads[j].join(); | |
} finally { | |
if (sa != null) { | |
sa.close(); | |
} | |
} | |
} | |
} finally { | |
if (ss != null) { | |
ss.close(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.net.*; | |
import java.util.concurrent.CountDownLatch; | |
// test for JDK-8006395 | |
// Racey test, will not always fail, but if it does then we have a problem. | |
public class RaceCd { | |
static final int NTHREADS = 100; | |
public static void main(String[] args) throws Exception { | |
ServerSocket ss = new ServerSocket(0); | |
try { | |
final int port = ss.getLocalPort(); | |
final CountDownLatch readySignal = new CountDownLatch(NTHREADS+1); | |
for (int i=0; i<NTHREADS; i++) { | |
final Socket s = new Socket("localhost", port); | |
s.setSoLinger(false, 0); | |
Socket sa = ss.accept(); | |
try { | |
sa.setSoLinger(false, 0); | |
final InputStream is = s.getInputStream(); | |
Thread[] threads = new Thread[NTHREADS]; | |
for (int j=0; j<NTHREADS; j++) { | |
threads[j] = new Thread() { | |
public void run() { | |
try { | |
readySignal.countDown(); | |
readySignal.await(); | |
while (is.read() != -1) | |
Thread.sleep(50); | |
} catch (Exception x) { | |
if (!(x instanceof SocketException | |
&& x.getMessage().equals("Socket closed"))) | |
x.printStackTrace(); | |
} | |
}}; | |
} | |
for (int j=0; j<NTHREADS; j++) | |
threads[j].start(); | |
readySignal.countDown(); | |
readySignal.await(); | |
s.close(); | |
for (int j=0; j<NTHREADS; j++) | |
threads[j].join(); | |
} finally { | |
if (sa != null) { | |
sa.close(); | |
} | |
} | |
} | |
} finally { | |
if (ss != null) { | |
ss.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment