求指点(一朋友写的)

package com.jwzx.vo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;

/**
* 题目如下:
* 现有 4个文件 4个线程
* thread1 向文件中写入1
* thread2 向文件中写入2
* thread3 向文件中写入3
* thread4 向文件中写入4
* 启动4个线程,最终得到4个文件输出如下:
* file1 ----> 123412341234...
* file2 ----> 213421342134...
* file3 ----> 312431243124...
* file4 ----> 412341234123...
* 请用程序模拟这一过程
*
* synchronize 是一种锁机制(不属于公平锁/非公平锁)
* ReentrantLock 此类的构造方法接受一个可选的公平参数(属于公平锁/非公平锁)
* FileWriter 内部提供锁机制(属于公平锁/非公平锁)
* 可以看日志
*
*/

public class TestReentrantLock1 {
private static final Logger log = Logger.getLogger(TestReentrantLock1.class);
private static final String[] files = {
"D:\\work\\text1.txt",
"D:\\work\\text2.txt",
"D:\\work\\text3.txt",
"D:\\work\\text4.txt"
};
private static final ReentrantLock[] locks = {new ReentrantLock()
,new ReentrantLock()
,new ReentrantLock()
,new ReentrantLock()
};
private static final Object[] objs ={new Object()
,new Object()
,new Object()
,new Object()
};
static {
for (int i = 0; i < files.length; i++) {
File file = new File(files[i]);
FileWriter out;
try {
out = new FileWriter(file);
out.write("");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* thread1 往文件写 1 的时间,
* thread2 往文件写 2 的时间,
* thread3 往文件写 3 的时间,
* thread4 往文件写 4 的时间
* 时间等差
*/
private static final int quan = 20;
// 写文件的单位时间
private static final int unitTime = 200;
private static void writeFile(int index){

//synchronized (objs[index]) {
locks[index].lock();
String context = Thread.currentThread().getName();
log.debug("Thread"+context+" begin write "+context + " to text"+(index+1));
FileWriter out = null;
try{
File file = new File(files[index]);
out = new FileWriter(file,true);
out.write(context);

out.flush();
out.close();
//
Thread.sleep(unitTime+quan*Integer.parseInt(context));
Thread.sleep(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
log.debug("Thread"+context+" end write "+context + " to text"+(index+1));
locks[index].unlock();
}
//}
}

private static class MyThread extends Thread{

public MyThread(String name){
super(name);
}
@Override
public void run() {
String name = getName();
for (int i = 0; i < 3; i++) {
if("1".equals(name)){
writeFile(0);
writeFile(1);
writeFile(2);
writeFile(3);
}else if("2".equals(name)){
writeFile(1);
writeFile(0);
writeFile(2);
writeFile(3);
}else if("3".equals(name)){
writeFile(2);
writeFile(0);
writeFile(1);
writeFile(3);
}else if("4".equals(name)){
writeFile(3);
writeFile(0);
writeFile(1);
writeFile(2);
}
}
}
}
public static void main(String[] args) {
new MyThread("1").start();
new MyThread("2").start();
new MyThread("3").start();
new MyThread("4").start();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值