请哪位仁兄帮忙解决一道多线程与输入输出的综合问题:编写一个java程序,有两个线程,一个负责输入读取,另一个负责输出。因为比较急用(2天内),我先谢谢大家。
解决方案 »
- java在sleep时,会进行垃圾回收吗
- 如何写JAVA程序把SQLServer的数据导入Oracle?
- 关于HTML
- 小I机器人的开发
- 请问JSDK中的Update是什么意思?
- 请问如何把一个CLASS文件制作成一个双击就可以执行的文件,还有就是在其他人机子上没有JRE也能运行~~谢谢
- 请问一道面试题的执行过程,分问没了
- 请问Java中移位的操作怎么处理?
- 一个校园系统项目请求JAVA高手合作开发(限深圳市内)兼职也可项目金额为:2.8万
- 所谓jre是指什么涵义,具体有那些部分组成,如果说是指java运行环境。就是要运行java程序所必需的话,那么有jvm还不够吗?如何不够?
- JNDI问题:Jndi访问FSSP时,lookup报“java.lang.ClassCastException: javax.naming.Reference”
- 急!!tomcat启动报错:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while(true) { queue.put(produce()); }
} catch (InterruptedException ex) { ... handle ...}
}
Object produce() { ... }
} class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while(true) { consume(queue.take()); }
} catch (InterruptedException ex) { ... handle ...}
}
void consume(Object x) { ... }
} class Setup {
void main() {
BlockingQueue q = new SomeQueueImplementation();
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Stack;public class ThreadHome {
public static Stack<String> buf;
public static void main(String[] args) {
//读写线程
ReaderThread rt=new ReaderThread();
WriterThread wt=new WriterThread();
Thread t1=new Thread(rt);
Thread t2=new Thread(wt);
//start
//start read
t1.start();
//start write
t2.start();
}}
class ReaderThread implements Runnable{
@Override
public void run() {
String str=CharUtils.readLine();
while(true){
if(!str.equals("exit")){
ThreadHome.buf.push(str);
System.out.println("reading....");
str=CharUtils.readLine();
}
else{//否则关闭输入流
ThreadHome.buf.push(str);
CharUtils.closeInputStream();
}
}
}
}
class WriterThread implements Runnable{
@Override
public void run() {
String str=ThreadHome.buf.pop();
while(true){
if(!str.equals("exit")){
CharUtils.writeLine(str);
}
else{
CharUtils.closeOutputStream();
}
}
}
}
class CharUtils{
private static BufferedReader br=null;
private static PrintWriter pw=null;
{
System.out.println("hello");
try {
br=new BufferedReader(new InputStreamReader(System.in,"utf-8"));
} catch (UnsupportedEncodingException e) {
System.out.println("BufferedReader Exception");
}
try {
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("src/homework.log",true)));
} catch (FileNotFoundException e) {
System.out.println("PrintWriter Exception");
}
}
public static String readLine(){
String str="";
try {
str=br.readLine();
} catch (IOException e) {
System.out.println("BufferedReader ReadLine Exception");
}
return str;
}
public static void writeLine(String str){
pw.println(str);
}
//close inputstream
public static void closeInputStream(){
try {
br.close();
} catch (IOException e) {
System.out.println("BufferedReader close Exception");
}
}
//close outputstream
public static void closeOutputStream(){
pw.close();
}
}