私はクラスProducer
とクラスPrinter
を持っています。 プロデューサはファイルからデータを読み取り、PrinttJobs
オブジェクトを作成します。 Producer
は、PrintJob
を生成し、Queue
に追加し、Printer
を通知します。新しいPrintJobs
を作成するのに1〜5秒待機するよりもProducer
です。 Printer
が通知されていない場合は、オンになりジョブをキューから取得して印刷します。この期間にProducer
は動作しません。 Printer
はすべてを印刷し、もう一度待ってProducer
を再び動作させます。 アプリは2つのプロデューサと1つのプリンタで動作します。 私の問題は時にはそれがうまくいくことです、時にはそれはすべてのものを印刷しないことです。また、私のwait
の時間制限は1〜5秒でうまくいきません/問題かもしれないと思います。コードは以下の通りです:Java - スレッドに関する問題
EDITED
生産者が実際に何かを作るとき、彼らはほとんど常に同時に送信します。そして時には、ファイル内のデータを生成することも止めます。
class Printer implements Runnable {
protected long MILLIS_PER_PAGE = 500;
private String name;
Queue queue;
boolean lock = false;
public Printer(String name, Queue queue) {
this.name = name;
this.queue = queue;
}
public String getPrinterName() {
return name;
}
@Override
public void run() {
System.out.println("["+getPrinterName()+"] Ligando...");
while(true) {
synchronized(this){
if(queue.isEmpty()) {
try {
System.out.println("["+getPrinterName()+"] Esperando por tabalho de impressão...");
lock = false;
halt();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
lock = true;
PrintJob pj = queue.removeFront();
System.out.println("Imprimindo "+ pj.getJobName());
try {
wait(pj.getNumberOfPages() * MILLIS_PER_PAGE);
System.out.println(pj.getJobName() + " ok.");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void halt() throws InterruptedException {
wait();
}
}
`
`
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
class Producer implements Runnable {
private String name;
Queue queue;
String job;
int pags;
String arquivo;
public Producer(String name, Queue queue, String arquivo) {
this.name = name;
this.queue = queue;
this.arquivo = arquivo;
}
public String getProducerName() {
return name;
}
@Override
public void run(){
synchronized (PrinterApp.printer) {
FileReader fin;
try {
fin = new FileReader(arquivo);
Scanner src = new Scanner(fin);
while (src.hasNext()) {
if (PrinterApp.printer.lock == true){
PrinterApp.printer.wait();
}
job = src.next();
pags = src.nextInt();
PrintJob p = new PrintJob(job, pags);
queue.addBack(p);
System.out.println("["+getProducerName()+"] produzindo arquivo " + job +", número de páginas: " + pags);
PrinterApp.printer.notify();
PrinterApp.printer.wait(1000 + (int)Math.round((Math.random() * (5000 - 1000))));
}
fin.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (QueueException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
は、オペレーティング・システムのクラスからの割り当てのような非常に多く、私はキュー答えを – Woot4Moo