【发布时间】:2017-04-03 16:01:19
【问题描述】:
我想用 Semaphore 来实现 Monitor。我创建了 2 个班级。缓冲区和线程演示。在 Buffer 类中,我创建了 put() 和 get() 方法(我从这个页面获取代码)
public void put(int input) throws InterruptedException {
monitorSemaphore.acquire();
boolean acquired = false;
while (numberInBuffer == size) {
// Equivalent of wait()
if (acquired) {
dec();
notifyCalled.release();
}
inc();
monitorSemaphore.release();
notifyCalled.acquire();
monitorSemaphore.acquire();
acquired = true;
}
// Critical section
buffer[last] = input;
last = (last + 1) % size;
numberInBuffer++;
// Equivalent of notifyAll()
for (int i = val(); i > 0; i--) {
dec();
notifyCalled.release();
}
monitorSemaphore.release();
}
public int get() throws InterruptedException {
monitorSemaphore.acquire();
boolean acquired = false;
while (numberInBuffer == 0) {
// Equivalent of wait()
if (acquired) {
dec();
notifyCalled.release();
}
inc();
monitorSemaphore.release();
notifyCalled.acquire();
monitorSemaphore.acquire();
acquired = true;
}
// Critical section
int temp = buffer[start];
start = (start + 1) % size;
numberInBuffer--;
// Equivalent of notifyA(ll)
for (int i = val(); i > 0; i--) {
dec();
notifyCalled.release();
}
monitorSemaphore.release();
return temp;
}
在TestThread 类中,我创建了Thread-T1、Thread-T2。但我不能在 Buffer 类中调用 put 和 Get。
public class TestThread extends Thread {
private Thread t;
private String threadName;
public TestThread(String name) {
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
put(2);//I can't call this method
Thread.sleep(5000);
get(); //
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
public static void main(String[] args) {
TestThread T1 = new TestThread( "Thread-1");
T1.start();
TestThread T2 = new TestThread( "Thread-2");
T2.start();
}}
如果我在 TestThread 类中的代码不正确,请告诉我。谢谢!
【问题讨论】:
标签: java concurrency semaphore monitor