- 浏览: 252210 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
soho00147:
我的想法是在这个插件的基础上编写更加强大的插件,扫描目录时发现 ...
maven增量编译的思考 -
soho00147:
如果没有变动则 maven install
使用时,直接 ...
maven增量编译的思考 -
soho00147:
可以参考这个插件,这个插件是对maven项目以module级别 ...
maven增量编译的思考
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);
}
}
}
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);
}
}
}
发表评论
-
java socket example
2013-07-17 17:08 918ObjectServer.java import java ... -
Eclipse下运行System.getenv()返回null
2012-09-22 18:01 2067在Eclipse下运行System.getenv(" ... -
emma doesn't coverage code with tests with @RunWith(PowerMockRunner.class);
2012-09-14 17:03 2178被测试的class是VINESNodeAddressColle ... -
使用PowerMock导致的ClassCastException
2012-09-07 16:02 2096使用PowerMock时遇到一些莫名其妙的问题,比如: jav ... -
PowerMock incompatible with Mockito issue
2012-09-07 09:55 3745Mock版本如下: Mockito version: 1.8. ... -
一个JTable相关的问题
2012-08-06 18:33 877Exception in thread "AWT-E ... -
Java SPI学习
2012-07-22 16:16 1009Java1.6开始提供了ServiceLoader类来支持SP ... -
用jvmti调试java application: hello world 实现
2012-07-03 11:57 33651. 用C写一个动态链接库, 代码如下: ubuntu@ub ... -
8000 nodes continously disconnect in large network environment
2012-06-21 18:16 1336根本原因是socket资源不足,导致连接失败, 下面是对exc ... -
a Java-level deadlock example
2012-05-25 12:08 1873Found one Java-level deadlock: ... -
java Semaphore 实例 -- Hungry Birds
2012-05-09 12:55 2074The Hungry Birds Problem (one ... -
Java synchronized实例 -- Hungry Bird
2012-05-08 21:25 1116Given are n baby birds and one ... -
replaceAll()中正则表达式的应用
2011-10-18 11:13 1268(?i): 忽略大小写; (?m): 在这种模式下,'^'和' ... -
Java集合容器总结(ZZ)
2011-10-11 16:30 1015按数据结构主要有以下几类: 1,内置容器:数组 2,lis ...
相关推荐
Lock锁是对象锁,仅在同一对象中,锁才会生效。(不做论证) (以下场景皆为单例模式下运行) lock.lock()的加锁方式,会使后续请求的线程堵塞等待。(方案A) lock.tryLock()的加锁方式,不会堵塞,会立即返回加锁...
java面试题_java-interview-questions-master.zip2、在 Java 程序中怎么保证多线程的运行安全? 出现线程安全问题的原因一般都是三个原因: 1、 线程切换带来的原子性问题 解决办法:使用多线程之间同步...
C语言实例精华--PWlock 密码锁源码
这个时候就需要队列具有自适应的扩充队列节点数的功能,基于锁(Lock-Base)的算法实现扩充是相当容易的,但基于锁无关(Lock-Free)实现起来难度较大,下面将使用伪代码介绍基于Lock-Free实现可自适应扩充的环形...
package-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-lockpackage-locockpackage-
计算机后端-Java-Java核心基础-第20章 多线程 13. Lock锁方式解决线程安全问题.avi
mvn se.vandmo:dependency-lock-maven-plugin:lock将创建一个dependencies-lock.json文件。 然后,您应该将该文件提交给您选择的源控件。 如果某些依赖项是同一多模块项目的一部分,则您可能希望这些依赖项与锁定...
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
integrated with an analog lock-in amplifier in 130-nm bulk CMOS. The integrated lock-in amplifier can support a modulation frequency of up to 10MHz with a gain of 54dB, a dynamic range of 42dB, and an...
实现yarn.lock与package-lock.json相互转换
秒杀代码-distributed-lock-seckill.zip
lockfree-shm-ipc为无锁机制的共享内存通信C源代码
这是java web工程与mysql数据库连接的中间件,直接导入至工程中即可使用,调用相应的方法。
significance of a lock-in amplifier. As an introduction to the subject there follows a simple intuitive account biased towards light measurement applications. All lock-in amplifiers, whether ...
Java多线程--避免同步机制带来的死锁问题及用Lock锁解决线程安全问题
C# Lock 实例,
Lock-free Dynamically Resizable Arrays Damian Dechev, Peter Pirkelbauer, and Bjarne Stroustrup
Lock-in amplifiers were invented in the 1930’s [1, 2, 3] and commercialized [4] in the mid 20th century as electrical instruments capable of extracting signal amplitudes and phases in extremely ...
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
Lock-Free Data Structures