需求描述:一个线程从控制台读入字符流,把字符流存在Stack中,然后用另外一个线程将缓存区Stack中的字符写入文件。
代码如下------------------------------------------------------------------
import java.io.BufferedReader;
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();
}
}
------解决方案--------------------------------------------------------
- Java code
import java.io.BufferedReader;import java.io.File;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.RandomAccessFile;import java.io.UnsupportedEncodingException;import java.util.Random;import java.util.Stack;public class ThreadHome { public static void main(String[] args) { Stack<String> buf = new Stack<String>(); // 读写线程 ReaderThread rt = new ReaderThread(buf); WriterThread wt = new WriterThread(buf); Thread t1 = new Thread(rt); Thread t2 = new Thread(wt); t1.start(); t2.start(); }}class ReaderThread implements Runnable { private Stack<String> buf; public ReaderThread(Stack<String> buf) { this.buf = buf; } public void run() { while (true) { // 等待随机时间 CharUtils.waitTime(); // 流关闭,退出线程 if (CharUtils.isObjectNull(CharUtils.getBufferedReader())) { break; } String str = CharUtils.readLine(); if ("exit".equals(str)) { CharUtils.closeInputStream(); } buf.push(str); System.out.println("read :" + str); } }}class WriterThread implements Runnable { private Stack<String> buf; public WriterThread(Stack<String> buf) { this.buf = buf; } public void run() { while (true) { // 等待随机时间 CharUtils.waitTime(); // 流关闭,退出线程 if (CharUtils.isObjectNull(CharUtils.getPrintWriter())) { break; } if (!buf.empty()) { String str = buf.pop(); if ("exit".equals(str)) { CharUtils.closeOutputStream(); } else { CharUtils.writeLine(str); } } else { System.out.println("Stack empty wait!"); } } }}class CharUtils { private static BufferedReader br = null; private static PrintWriter pw = null; private static final String PATH = "F:\\360Downloads\\xxx.txt"; private static final int TIME = 10000; static { System.out.println("hello"); try { br = new BufferedReader(new InputStreamReader(System.in, "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream( getFile(), true))); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static String readLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public static void writeLine(String str) { pw.println(str); pw.flush(); System.out.println("have write:" + str); } // close inputstream public static void closeInputStream() { try { if (br != null) { br.close(); } br = null; } catch (IOException e) { e.printStackTrace(); } } // close outputstream public static void closeOutputStream() { try { if (pw != null) { pw.close(); } pw = null; } catch (Exception e) { e.printStackTrace(); } } /** * 随机等待 * @return */ public static void waitTime() { try { Thread.sleep(new Random().nextInt(TIME)); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 文件是否存在,不存在创建 * @return */ private static File getFile() { File file = new File(PATH); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return file; } /** * 判断是否为空 * @return */ public static boolean isObjectNull(Object obj) { if (obj == null) { return true; } return false; } public static BufferedReader getBufferedReader() { return br; } public static PrintWriter getPrintWriter() { return pw; }}