import java.util.concurrent.Semaphore;
public class ThreadTest {
private Semaphore semaphore = new Semaphore(0);
private String winner;
private synchronized void finished(String threadName) {
if (winner == null) {
winner = threadName;
}
semaphore.release();
}
public void run() {
Runnable r1 = new Runnable() {
public void run() {
try {
Thread.sleep((long) (5000 * Math.random()));
}
catch (InterruptedException e) {
}
finally {
finished("thread 1");
}
}
};
Runnable r2 = new Runnable() {
public void run() {
try {
Thread.sleep((long) (5000 * Math.random()));
}
catch (InterruptedException e) {
}
finally {
finished("thread 2");
}
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
try {
semaphore.acquire();
System.out.println("The winner is " + winner);
}
catch (InterruptedException e) {
System.out.println("No winner");
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
new ThreadTest().run();
}
}
, , , , , .
EDIT:
Aa, jtahlborn, CountDownLatch . :
import java.util.concurrent.CountDownLatch;
public class ThreadTest {
private CountDownLatch latch = new CountDownLatch(1);
private String winner;
private synchronized void finished(String threadName) {
if (winner == null) {
winner = threadName;
}
latch.countDown();
}
public void run() {
Runnable r1 = new Runnable() {
public void run() {
try {
Thread.sleep((long) (5000 * Math.random()));
}
catch (InterruptedException e) {
}
finally {
finished("thread 1");
}
}
};
Runnable r2 = new Runnable() {
public void run() {
try {
Thread.sleep((long) (5000 * Math.random()));
}
catch (InterruptedException e) {
}
finally {
finished("thread 2");
}
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
try {
latch.await();
System.out.println("The winner is " + winner);
}
catch (InterruptedException e) {
System.out.println("No winner");
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
new ThreadTest().run();
}
}
, 2 1.