Java 同時セマフォのデッドロック
温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
同期にセマフォを使用しているプロデューサー/コンシューマー シナリオで問題が発生しました。アイテム (プリンター) の生産を担当するプロデューサー (Produttore) と、アイテムの消費を担当する消費者 (Cliente) がいます。コンシューマー セマフォを 1 (一度に 1 つのコンシューマーを許可) に初期設定、プロデューサー セマフォを 0 (最初はプロデューサーをブロックする) に初期化しました。
プロダクション後に両方のセマフォの値を出力しようとしましたが、予想通り、コンシューマのセマフォには 1、プロデューサーのセマフォには 0 が得られました。
私が期待する動作は、プロデューサーはプリンターが店内で使い果たされた場合にのみプリンターを生産し、コンシューマーはプリンターが利用可能な場合にそれを消費することです。サイクルに入らないと、プリンターが生成された後にプログラムが停止するようです。
主:
public class Main {
public static void main(String[] args) {
Negozio n1=new Negozio(10, 50);
Semaphore semaforoCliente=new Semaphore(1);
Semaphore semaforoProduttore=new Semaphore(0);
Cliente c1=new Cliente(semaforoCliente, semaforoProduttore, n1);
Produttore p1=new Produttore(semaforoCliente, semaforoProduttore, n1);
Tempo t1=new Tempo(n1);
Thread cliente=new Thread(c1, "Acquisto");
Thread produttore=new Thread(p1);
Thread tempo=new Thread(t1);
System.out.println(n1.toString());
tempo.start();
produttore.start();
cliente.start();
}
}
コンシューマクラス:
public class Cliente implements Runnable{
private Negozio negozio;
private int stampantiAcquistate;
private Semaphore semaforoCliente;
private Semaphore semaforoProduttore;
private Thread t;
public Cliente(Semaphore semaforoCliente, Semaphore semaforoProduttore, Negozio negozio) {
this.negozio=negozio;
this.stampantiAcquistate=0;
this.semaforoCliente=semaforoCliente;
this.semaforoProduttore=semaforoProduttore;
t=new Thread(this, "Acquisto");
}
@Override
public void run() {
do {
this.stampantiAcquistate=10;
try {
semaforoCliente.acquire();
} catch (InterruptedException e) {
System.out.println("Problemi con i semafori");
}
try {
Thread.sleep(1000);
System.out.println(this.negozio.toString()+"\n"+t.getName()+": "+this.stampantiAcquistate);
this.negozio.acquisto(this.stampantiAcquistate);
} catch (Exception e) {
System.out.println(e.getMessage());
}
if(this.negozio.getNumStampanti()>0) {
this.semaforoCliente.release();
}
}while(this.negozio.getNumStampanti()>0 && this.negozio.getTempo()>0);
this.semaforoProduttore.release();
}
}
プロデューサークラス:
package stampanti;
import java.util.Random;
import java.util.concurrent.Semaphore;
public class Produttore implements Runnable{
private Negozio negozio;
private Semaphore semaforoCliente;
private Semaphore semaforoProduttore;
private Thread t;
private int stampantiProdotte;
public Produttore(Semaphore semaforoCliente, Semaphore semaforoProduttore, Negozio negozio) {
this.negozio=negozio;
this.semaforoCliente=semaforoCliente;
this.semaforoProduttore=semaforoProduttore;
this.t=new Thread(this, "Rifornimento");
}
@Override
public void run() {
this.stampantiProdotte=new Random().nextInt(10)+1;
try {
this.semaforoProduttore.acquire();
} catch (InterruptedException e) {
System.out.println("\nProblemi con i semafori");
}
try {
Thread.sleep(1000);
System.out.println("Stampanti prodotte: "+this.stampantiProdotte);
this.negozio.setNumStampanti(stampantiProdotte);
} catch (InterruptedException e) {
System.out.println("Problemi con i threads");
}
this.semaforoCliente.release();
System.out.println(this.semaforoCliente.toString());
System.out.println(this.semaforoProduttore.toString());
}
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2598
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(193) 评论(0)