2014-12-03 4 views
3

안녕하세요,세마포어로 동기화, 대기, 알림 변경 방법? (생산자 - 소비자)

나는 어떻게 동기화 할 수 있고, 다음 코드에서 세마포어로 알림을 대체 할 수 있는지 궁금합니다. 그리고 세마포어 변수는 어디에 만들어야합니까?

import java.util.*; 

    class Producer 
    extends Thread 
    { 
    private Vector v; 

    public Producer(Vector v) 
    { 
     this.v = v; 
    } 

    public void run() 
    { 
     String s; 

     while (true) { 
     synchronized (v) { 
      s = "Value"+Math.random(); 
      v.addElement(s); 
      System.out.println("Producer created "+s); 
      v.notify(); 
     } 
     try { 
      Thread.sleep((int)(100*Math.random())); 
     } catch (InterruptedException e) { 
      //nothing 
     } 
     } 
    } 
    } 

    class Consumer 
    extends Thread 
    { 
     private Vector v; 

     public Consumer(Vector v) 
     { 
     this.v = v; 
     } 

     public void run() 
     { 
     while (true) { 
      synchronized (v) { 
       if (v.size() < 1) { 
        try { 
        v.wait(); 
        } catch (InterruptedException e) { 
        //nothing 
        } 
       } 
       System.out.print(
       " Consumer found "+(String)v.elementAt(0) 
       ); 
       v.removeElementAt(0); 
       System.out.println(" (remaning: "+v.size()+")"); 
      } 
      try { 
       Thread.sleep((int)(100*Math.random())); 
      } catch (InterruptedException e) { 
       //nothing 
      } 
     } 
     } 
    } 

누군가 나를 도울 수 있다면 기쁠 것입니다.

은 .. 사전에 여러 스레드가 공유 자원에 액세스 할 수 있도록 할 수있는 잠금 장치로 세마포어의

답변

3

생각해 주셔서 감사합니다. 세마포어를 초기화하는 번호는 많은 스레드가 동시에 리소스에 액세스 할 수있게합니다. Producer-Consumer에서 리소스는 두 스레드 간의 공유 버퍼입니다. 꽉 차거나 버퍼가 비어 있지 않으면 버퍼에 액세스 할 수 없으면 소비자가 버퍼에 액세스 할 수 없도록해야합니다. 소비자의 세마포어에서는 0, 생산자의 세마포에서는 1로 시작해야합니다. 이렇게하면 제작자가 첫 번째 이동을해야합니다. 생산자가 버퍼에 쓰기 시작하면 생산자 세마포어 인 down이 필요합니다. 생산자가 완료되면 소비자가 자원에 액세스 할 수있게하는 소비자의 세마포어 up을 원하게됩니다. 소비자가 리소스에 액세스하면 생산자의 세마포어가 버퍼가 비어 있음을 생산자에게 알리는 up입니다.

사용이 출발점으로 : http://cs.gmu.edu/cne/modules/ipc/aqua/producer.html

+0

nLee 안녕하세요 .. 오늘은 당신이 그랬던 것처럼 자세히 모든 것이 그가 나를 설명하는 저를 설명하는 교사에게 물었다 ... 난 당신을 감사드립니다 당신의 대답에 대해 대단히. 너와 stackdev는 나를 많이 도와 줬어. – David

+0

전혀 문제가되지 않습니다. 도와 줘서 기뻐요! – nLee

0
import java.util.Vector; 
import java.util.concurrent.Semaphore; 

public class Main { 
    public static void main(String[] args) { 
     Semaphore mutex = new Semaphore(1); 
     Vector<String> vector = new Vector<>(); 
     new Producer(vector, mutex).start(); 
     new Consumer(vector, mutex).start(); 
    } 
} 

class Producer extends Thread { 
    private Vector v; 
    private Semaphore mutex; 

    public Producer(Vector v, Semaphore mutex) { 
     this.v = v; 
     this.mutex = mutex; 
    } 

    public void run() { 
     String s; 

     while (true) { 
      try { 
       mutex.acquire(); 
       s = "Value" + Math.random(); 
       v.addElement(s); 
       System.out.println("Producer created " + s); 
       mutex.release(); 
       Thread.sleep((int) (100 * Math.random())); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 

     } 
    } 
} 


class Consumer extends Thread { 
    private Vector v; 
    private Semaphore mutex; 

    public Consumer(Vector v, Semaphore mutex) { 
     this.v = v; 
     this.mutex = mutex; 
    } 

    public void run() { 
     while (true) { 
      try { 
       mutex.acquire(); 
       if (v.size() > 0) { 
        System.out.print(" Consumer found " + (String) v.elementAt(0)); 
        v.removeElementAt(0); 
        System.out.println(" (remaning: " + v.size() + ")"); 
       } 
       mutex.release(); 
       Thread.sleep((int) (100 * Math.random())); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 
} 
+0

대단히 고맙습니다. stackdev! – David