나는, 모든 라인이 한 시간을 계산 얻을 있는지 확인하려면
나는 당신이 ExecutorService
을 사용하는 것이 좋습니다 스레드의 풀에 이미지 작업으로 각 행을 제출하는 것입니다. 코드 샘플은 하단을 참조하십시오. 이 작업을 올바르게 수행하면 출력 라인 수를 걱정할 필요가 없습니다.
나는 System.out.println(CalculatedLineNumber)
내가 아주 이것에 대한 필요성을 이해하지 못하는 만들 수 있습니다. 이것은 모든 이미지가 처리되었는지 확인하는 데 도움이되는 일종의 회계 파일입니까?
누군가가 내가 이미 아래에 동기화되어 있기 때문에 당신은 flush
PrintWriter
필요없는의 PrintWriter와 플러시()
를 사용한다고 내게 말했다. 각 작업이 끝날 때마다 결과를 출력하고 X 행 작업을 threadPool
에 제출하면 출력 X 행을 갖게됩니다.
당신이 PrintWriter
를 사용하기 위해 필요한 모든
은 다음과 같습니다
PrintWriter printWriter = new PrintWriter(new File("/tmp/outputFile.txt"));
// each thread can do:
writer.println("Some sort of output: " + myRow);
여기 ExecutorService
스레드 풀을 사용하는 방법을 보여주는 몇 가지 예제 코드입니다.
PrintWriter outputWriter = ...;
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// i'm not sure exactly how to build the parameter for each of your rows
for (int myRow : rows) {
// something like this, not sure what input you need to your jobs
threadPool.submit(new ImageJob(outputWriter, myRow, getHeight(), getWidth()));
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
...
public class ImageJob implements Runnable {
private PrintWriter outputWriter;
private int myRow;
private int height;
private int width;
public MyJobProcessor(PrintWriter outputWriter, int myRow, int height,
int width, ...) {
this.outputWriter = outputWriter;
this.myRow = myRow;
this.height = height;
this.width = width;
}
public void run() {
image.setRGB(0, myRow, width, 1, renderLine(myRow), 0, 0);
outputWriter.print(...);
}
}
난 당신이 ['RandomAccessFile'] (http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html)를 필요가 있다고 생각합니다. –