日期:2014-05-20 浏览次数:20996 次
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; } }