Java 使用信号量实现监控

Java 使用信号量实现监控,java,concurrency,semaphore,monitor,Java,Concurrency,Semaphore,Monitor,我想用信号量来实现监视器。我创建了2个类。缓冲区和线程演示。在类缓冲区中,我创建方法put()和get()(我从这个页面获取代码) 在TestThread类中,我创建了Thread-T1和Thread-T2。但我不能在类缓冲区中调用put和Get public class TestThread extends Thread { private Thread t; private String threadName; public TestThread(String name) {

我想用信号量来实现监视器。我创建了2个类。缓冲区和线程演示。在类缓冲区中,我创建方法put()和get()(我从这个页面获取代码)

在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类中的代码不正确,请告诉我。谢谢

我想。。。假设在缓冲区类中定义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.");
   }
}

如何创建对象TestThread?TestThread T1=新的TestThread(“Thread-1”,?);“?”应该是缓冲区对象。这意味着您需要在初始化线程之前创建一个缓冲区对象。此后,不同的线程可以将同一个缓冲区对象作为构造函数输入,然后并发操作同一个缓冲区对象。没问题。祝你的学习之旅好运!此外,如果你觉得我的回答有帮助,你可以接受:)
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.");
   }
}