import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.io.IOUtils;
public class MuFile implements Runnable{
static File file = new File("D:\\log6666666.txt");
private int chunk;
private static final ReentrantLock lock = new ReentrantLock();
public MuFile(int chunk){
this.chunk=chunk;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(!file.exists()){try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}else{
file.delete();
}
List<Thread> tl = new ArrayList<Thread>();
for(int i=0;i<5;i++){
Thread m = new Thread(new MuFile(i));
m.setName("QJW"+i);
tl.add(m);
m.start();
}
for(int i=0;i<tl.size();i++){
Thread m =tl.get(i);
try {
m.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void run() {
List<String> lines = new ArrayList<String> ();
String tname = Thread.currentThread().getName();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int j=0;j<3;j++){
lines.add(tname+" line "+j);
}
write(lines,chunk);
//write2(lines,chunk);
//write3(lines,chunk);
}
void write(List<String> lines,int chunk) {
synchronized (file) {
//File file = new File("D:\\sample4final.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
String tname = Thread.currentThread().getName();
if (!lines.isEmpty()) {
IOUtils.write(chunk+" -"+tname+" start: \n", bw);
IOUtils.writeLines(lines, null, bw);
IOUtils.write(chunk+" -"+tname+" end: \n", bw);
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(bw);
IOUtils.closeQuietly(fw);
}
}
}
void write2(List<String> lines,int chunk) {
//File file = new File("D:\\sample4final.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
String tname = Thread.currentThread().getName();
if (!lines.isEmpty()) {
IOUtils.write(chunk+" -"+tname+" start: \n", bw);
IOUtils.writeLines(lines, null, bw);
IOUtils.write(chunk+" -"+tname+" end: \n", bw);
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(bw);
IOUtils.closeQuietly(fw);
}
}
void write3(List<String> lines,int chunk) {
lock.lock(); // block until condition holds
//File file = new File("D:\\sample4final.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
String tname = Thread.currentThread().getName();
if (!lines.isEmpty()) {
IOUtils.write(chunk+" -"+tname+" start: \n", bw);
IOUtils.writeLines(lines, null, bw);
IOUtils.write(chunk+" -"+tname+" end: \n", bw);
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(bw);
IOUtils.closeQuietly(fw);
lock.unlock();
}
}
}
多线程写一个文件,回字的3个写法
最新推荐文章于 2024-09-25 10:59:43 发布