【发布时间】:2014-04-22 18:10:00
【问题描述】:
我研究使用wait 和notify 方法进行同步。
我写了一个小例子,不明白为什么我看到这个输出:
take 1
release 1
release 2
我的代码:
主要:
public class CustomSemaphoreTest {
public static void main(String[] args) throws InterruptedException {
Semaphore semaphore = new Semaphore();
SendingThread sender = new SendingThread(semaphore);
RecevingThread receiver = new RecevingThread(semaphore);
sender.start();
Thread.sleep(300);
receiver.start();
}
}
发件人:
class SendingThread extends Thread {
volatile Semaphore semaphore = null;
int count = 1;
public SendingThread(Semaphore semaphore) {
this.semaphore = semaphore;
}
public void run() {
this.semaphore.take();
}
}
接收者:
class RecevingThread extends Thread {
volatile Semaphore semaphore = null;
int count = 1;
public RecevingThread(Semaphore semaphore) {
this.semaphore = semaphore;
}
public void run() {
while (true) {
try {
this.semaphore.release();
Thread.sleep(20);
} catch (InterruptedException e) {
}
// System.out.println("get signal " + count++);
}
}
}
信号量:
class Semaphore {
private boolean signal = false;
int rCount = 1;
int sCount = 1;
public synchronized void take() {
System.out.println("take " + sCount++);
this.signal = true;
this.notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void release() throws InterruptedException {
System.out.println("release " + rCount++);
notify();
while (!this.signal) {
wait();
}
this.signal = false;
}
}
我如何想象这个程序的执行?:
时间 0:线程 1:sender.start() ->...->
public synchronized void take() {
System.out.println("take " + sCount++);
this.signal = true;
this.notify(); //try wake up waiting threads but noone sleep thus have not affect
try {
wait(); // release monitor and await notify
} catch (InterruptedException e) {
e.printStackTrace();
}
}
因此我在屏幕上看到take 1
时间 300:线程 2 receiver.start(); ->
public synchronized void release() throws InterruptedException {
System.out.println("release " + rCount++); //section is rid thus this lines outputes
notify(); // notify thread1
while (!this.signal) {
wait(); // pass the control to thread1 and await notification .....
}
this.signal = false;
}
所以没想到会看到release 2
请澄清我的误解。
【问题讨论】:
-
完全有可能首先调用
release(),并成功完成(因为signal被初始化为false),然后take()运行,然后release()再次运行。
标签: java multithreading concurrency wait notify