안녕하세요,세마포어로 동기화, 대기, 알림 변경 방법? (생산자 - 소비자)
나는 어떻게 동기화 할 수 있고, 다음 코드에서 세마포어로 알림을 대체 할 수 있는지 궁금합니다. 그리고 세마포어 변수는 어디에 만들어야합니까?
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
}
}
}
}
누군가 나를 도울 수 있다면 기쁠 것입니다.
은 .. 사전에 여러 스레드가 공유 자원에 액세스 할 수 있도록 할 수있는 잠금 장치로 세마포어의
nLee 안녕하세요 .. 오늘은 당신이 그랬던 것처럼 자세히 모든 것이 그가 나를 설명하는 저를 설명하는 교사에게 물었다 ... 난 당신을 감사드립니다 당신의 대답에 대해 대단히. 너와 stackdev는 나를 많이 도와 줬어. – David
전혀 문제가되지 않습니다. 도와 줘서 기뻐요! – nLee