2017-04-03 13 views
1

セマフォを使用してMonitorを実装したい。私は2つのクラスを作成します。バッファとThreadDemo。クラスバッファに、私は、メソッドのPUTを()を作成し、私はスレッドT1、スレッドT2を作成し、クラスTestThreadでセマフォを使用してモニタを実装する

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; 
} 

(私は、このページからコードを取得))(取得します。しかし、私はputと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(); 

}} 

TestThreadクラスのコードが間違っている場合は、私に表示してください。ありがとう!

答えて

0

は、私はあなたのバッファクラスでの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."); 
    } 
} 
+0

オブジェクトTestThreadを作成するにはどうすればよいですか? TestThread T1 =新しいTestThread( "Thread-1"、???); –

+0

"???"あなたのBufferオブジェクトにする必要があります。つまり、スレッドを初期化する前にバッファオブジェクトを作成する必要があります。その後、異なるスレッドはコンストラクタ入力と同じバッファオブジェクトを取得し、同じBufferオブジェクトに対して同時に操作できます。 – Ray

+0

:)ありがとうございました。わかりました ! –

関連する問題