多线程写一个资料,回字的3个写法

参考地址:http://www.verydemo.com/demo_c134_i14238.html

多线程写 一个文件,回字的3个 写法
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();
		}


}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值