2017-04-03 4 views
1

세마포어를 사용하여 모니터를 구현하고 싶습니다. 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; 
} 

(I이 페이지에서 코드를 얻을)) (얻는다. 하지만 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 객체 여야합니다. 즉, 스레드를 초기화하기 전에 버퍼 객체를 만들어야합니다. 이후에 다른 스레드는 생성자 입력과 동일한 버퍼 객체를 취할 수 있고 동일한 버퍼 객체를 동시에 조작 할 수 있습니다. – Ray

+0

:) 고마워요. 이해 했어 ! –