使用信号量实现监视器



我想使用信号量来实现监视器。我创建 2 个类。缓冲区和线程演示。在类缓冲区中,我创建了方法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类中的代码不正确,请告诉我。谢谢!

我认为...假设您是否在 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.");
   }
}

最新更新