Java Lock 实例 -- 实现简单的tee

Develop a parallel multithreaded program (in C using Pthreads, or in Java) that implements the Unix tee command, which is invoked as follows:
tee filename
The command reads the standard input and writes it to both the standard output and to the file filename. Your parallel tee program should have three threads: one for reading standard input, one for writing to standard output, and one for writing to file filename.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;

public class Tee {
private static List<String> queue = new ArrayList<String>();
private static boolean EOF = false;
public static Lock lock = new ReentrantLock();
public static Condition condOutput = lock.newCondition();

public static Condition condReader = lock.newCondition();

public static int counterFile = 0;
public static int counterConsole = 0;

public static void main(String[] args) {
args = new String[2];
args[0] = "C:\\LCT\\3.txt";
args[1] = "C:\\LCT\\tmp.txt";
if(args == null || args.length != 2){
System.out.println("java Tee <input file> <output file>");
System.exit(1);
}
FileWriterThread fThread = new FileWriterThread(args[1]);
ConsoleWriterThread cThread = new ConsoleWriterThread();

fThread.start();
cThread.start();

ReaderThread rThread = new ReaderThread(args[0]);
rThread.start();

}

static class ReaderThread extends Thread{
private String fileName;

public ReaderThread(String fileName){
this.fileName = fileName;
}

public void run(){
BufferedReader in;
try {
in =
new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line;
while ((line = in.readLine()) != null) {
lock.lock();

queue.add(line);
condOutput.signalAll();
condReader.await();
queue.clear();
lock.unlock();

}
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
EOF = true;
lock.lock();
condOutput.signalAll();
lock.unlock();
}

}
}

static class FileWriterThread extends Thread{
private FileOutputStream out;
private PrintWriter pw;

public FileWriterThread(String fileName){

try {
out = new FileOutputStream(new File(fileName));
pw = new PrintWriter(out);
}catch(Exception e){
e.printStackTrace();
}
}

public void run(){
do{
lock.lock();
if(queue.isEmpty() || counterFile == 1){
try {
condOutput.await();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
for (Iterator<String> iter = queue.iterator(); iter.hasNext();)
{
String line = iter.next();

pw.println(line);
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
counterFile++;

if(counterFile == 1 && counterConsole == 1){

queue.clear();
counterFile = 0;
counterConsole = 0;
condReader.signal();
}
lock.unlock();
} while(!EOF);

try {
pw.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

static class ConsoleWriterThread extends Thread{

public ConsoleWriterThread(){

}

public void run(){
do{
lock.lock();
if(queue.isEmpty() || counterConsole == 1){
try {
condOutput.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Iterator<String> iter = queue.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
counterConsole++;

if(counterFile == 1 && counterConsole == 1){
queue.clear();
counterFile = 0;
counterConsole = 0;
condReader.signal();
}
lock.unlock();
} while(!EOF);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值