- 浏览: 254934 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
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 951ObjectServer.java import java ... -
Eclipse下运行System.getenv()返回null
2012-09-22 18:01 2106在Eclipse下运行System.getenv(" ... -
emma doesn't coverage code with tests with @RunWith(PowerMockRunner.class);
2012-09-14 17:03 2238被测试的class是VINESNodeAddressColle ... -
使用PowerMock导致的ClassCastException
2012-09-07 16:02 2127使用PowerMock时遇到一些莫名其妙的问题,比如: jav ... -
PowerMock incompatible with Mockito issue
2012-09-07 09:55 3772Mock版本如下: Mockito version: 1.8. ... -
一个JTable相关的问题
2012-08-06 18:33 903Exception in thread "AWT-E ... -
Java SPI学习
2012-07-22 16:16 1028Java1.6开始提供了ServiceLoader类来支持SP ... -
用jvmti调试java application: hello world 实现
2012-07-03 11:57 33981. 用C写一个动态链接库, 代码如下: ubuntu@ub ... -
8000 nodes continously disconnect in large network environment
2012-06-21 18:16 1360根本原因是socket资源不足,导致连接失败, 下面是对exc ... -
a Java-level deadlock example
2012-05-25 12:08 1889Found one Java-level deadlock: ... -
java Semaphore 实例 -- Hungry Birds
2012-05-09 12:55 2105The Hungry Birds Problem (one ... -
Java synchronized实例 -- Hungry Bird
2012-05-08 21:25 1138Given are n baby birds and one ... -
replaceAll()中正则表达式的应用
2011-10-18 11:13 1284(?i): 忽略大小写; (?m): 在这种模式下,'^'和' ... -
Java集合容器总结(ZZ)
2011-10-11 16:30 1037按数据结构主要有以下几类: 1,内置容器:数组 2,lis ...
相关推荐
- 线程同步:synchronized关键字、wait()、notify()和notifyAll()方法,以及Lock接口和相关的实现类。 7. **网络编程** - Socket编程:创建客户端和服务端的连接,进行数据传输。 - URL和URLConnection:用于从...
《Java代码实例——论坛管理系统设计》是一份集源代码与论文为一体的毕业设计作品,主要针对的是使用Java语言进行Web应用开发的实践。这份资源详细展示了如何利用Java技术栈构建一个功能完善的论坛管理系统,涵盖了...
Lock锁是对象锁,仅在同一对象中,锁才会生效。(不做论证) (以下场景皆为单例模式下运行) lock.lock()的加锁方式,会使后续请求的线程堵塞等待。(方案A) lock.tryLock()的加锁方式,不会堵塞,会立即返回加锁...
Java提供了多种机制来保证这一点,如`synchronized`关键字、`volatile`关键字、`Atomic`类以及`Lock`接口。在断点续传中,可能使用这些机制来管理共享的文件下载状态和进度。 3. **HTTP协议**:HTTP(超文本传输...
以上只是Java基础知识的一部分,实际的Java基础教程会更深入地讲解每个概念,并配有实例来帮助理解。这份"java基础教程----精华版"应该涵盖了这些主题,并且可能还有更多的实践指导和示例代码,对于学习和巩固Java...
java面试题_java-interview-questions-master.zip2、在 Java 程序中怎么保证多线程的运行安全? 出现线程安全问题的原因一般都是三个原因: 1、 线程切换带来的原子性问题 解决办法:使用多线程之间同步...
2. **线程同步**:在多线程环境下,数据共享可能导致数据不一致问题,Java提供了多种同步机制,如`synchronized`关键字、`Lock`接口(包括`ReentrantLock`)以及`volatile`关键字,以确保线程安全。 3. **并发集合*...
在"lockfree-shm-ipc"中,开发者可能采用了CAS(Compare and Swap)或FAS(Fetch and Swap)等无锁算法来实现数据的读写操作。 在压缩包中,"lsi.make"和"Makefile"是构建脚本,用于编译和链接项目。"lockfree-shm-...
Java面试是每位Java开发者职业生涯中的重要环节,而GitHub上的高分项目往往汇聚了社区的智慧结晶,提供了丰富的面试准备资源。"Java-Interview-超全集合github上评分最高的jiva面试题"就是一个这样的宝藏,它涵盖了...
在本资源包“Java 实例 - 状态监测源代码+详细指导教程.zip”中,包含了一个全面的学习资源,旨在帮助用户深入理解如何在Java中实现状态监测功能。这个教程不仅提供了源代码,还搭配了详尽的指导,使得学习者能够...
为避免多线程环境下的数据不一致性,Java提供了多种同步机制,如`synchronized`关键字、`wait()`, `notify()`和`notifyAll()`方法,以及`Lock`和`Condition`接口等。 4. **死锁**: 当两个或更多线程互相等待对方...
标题中的"mysql-connector-java-5.1.0 jar包"指的是这个特定版本的驱动程序,它是一个.jar文件,用于在Java Web工程中进行MySQL数据操作。 首先,了解JDBC是理解MySQL Connector/J的关键。JDBC是Java中用于访问各种...
《Java经典实例.第三版》是一本深受Java开发者欢迎的实战指南,旨在通过丰富的实例来深入浅出地讲解Java编程的各个方面。这本书的高清PDF版本提供了清晰易读的阅读体验,而配套的代码则为读者提供了实践操作的机会,...
C语言实例精华--PWlock 密码锁源码
标题中的“聊天室------java编写----多人聊天”表明这是一个基于Java编程语言开发的多用户聊天应用程序。这个项目可能是一个简单的实现,旨在帮助学习者理解如何构建一个基础的网络通信系统,特别是涉及到多人实时...
下面是一个简单的Java示例,展示如何获取和打印当前线程状态: ```java public class ThreadStateExample { public static void main(String[] args) { Thread thread = new Thread(() -> { while (true) { ...
计算机后端-Java-Java核心基础-第20章 多线程 13. Lock锁方式解决线程安全问题.avi
1. **基础概念**:Java由James Gosling在1995年推出,它的设计目标是“简单、面向对象、健壮、安全、性能好、可移植、动态”。Java程序由类组成,通过JVM(Java虚拟机)运行。 2. **语法**:Java语法与C++类似,但更...
下面是一个简单的Java线程优先级设置的源代码示例: ```java public class ThreadPriorityExample { public static void main(String[] args) { // 创建一个低优先级线程 Thread lowPriorityThread = new Thread...
`yarn.lock` 和 `package-lock.json` 文件都是npm和yarn包管理器为了确保项目依赖一致性而生成的文件。本篇文章将详细探讨这两个文件的作用、差异以及如何实现它们之间的相互转换。 ### `yarn.lock` 文件 `yarn....