Reputation: 11
I want to use Semaphore to implement Monitor. I create 2 class. Buffer and ThreadDemo. In class Buffer, I create method put() and get() ( I get code from this page)
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;
}
In class TestThread, I create Thread-T1, Thread-T2. But I can't call put and Get in class Buffer.
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();
}}
And if my code in class TestThread incorrect, please show me. Thanks!
Upvotes: 1
Views: 274
Reputation: 160
I think... Let's assume if you define the get() and put() method in Buffer class. Then you should initialize the class instance first before you call the in-class methods. Like the code below:
public class TestThread extends Thread {
private Thread t;
private String threadName;
private Buffer buffer;
public TestThread(String name, Buffer buffer) {
threadName = name;
this.buffer = buffer;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
buffer.put(2);//I can't call this method
Thread.sleep(5000);
buffer.get(); //
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
}
Upvotes: 1