使用信号量实现监控

时间:2017-04-03 16:01:20

标签: java concurrency semaphore monitor

我想使用Semaphore来实现Monitor。我创造了2个班级。 Buffer和ThreadDemo。在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。但是我无法在类缓冲区中调用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类中的代码不正确,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:1)

我想......如果你在Buffer类中定义get()和put()方法,我们假设。然后,在调用类内方法之前,应首先初始化类实例。像下面的代码:

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.");
   }
}