javaマルチスレッドは同じファイルに書き込みます。
2320 ワード
回転:
http://www.update8.com/Program/Java/19760.html
http://www.update8.com/Program/Java/19760.html
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
*
* @author owen.huang
*
*/
/**
*
*/
class Creater implements Runnable{
private ConcurrentLinkedQueue<String> queue;
private String contents;
public Creater(){}
public Creater(ConcurrentLinkedQueue<String> queue, String contents){
this.queue = queue;
this.contents = contents;
}
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.add(contents);
}
}
/**
*
*/
class DealFile implements Runnable{
private FileOutputStream out;
private ConcurrentLinkedQueue<String> queue;
public DealFile(){}
public DealFile(FileOutputStream out,ConcurrentLinkedQueue<String> queue){
this.out = out;
this.queue = queue;
}
public void run() {
while(true){//
if(!queue.isEmpty()){
try {
out.write(queue.poll().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
*
*/
public class TestMultipleWriteFile {
public static void main(String[] args) throws FileNotFoundException{
FileOutputStream out = new FileOutputStream(new File("F:"+ File.separator +"test.txt"),true);
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
for(int j=0;j<10;j++){
new Thread(new Creater(queue,j+"--")).start();//
}
new Thread(new DealFile(out,queue)).start();// , queue
}
}