日期:2014-05-20 浏览次数:20779 次
import java.io.*; public class ThreadFight0 implements Runnable { public void run() { long a = System.currentTimeMillis(); int i = 0; BufferedWriter buff = null; try { buff = new BufferedWriter(new FileWriter("d:\\logm2.txt")); } catch (IOException e1) { e1.printStackTrace(); } while (System.currentTimeMillis() - a <= 50) { try { String str = Thread.currentThread().toString() + i++; buff.write(str); System.out.println("str is "+str); buff.write("\r\n"); buff.flush(); } catch (IOException e) { e.printStackTrace(); } } try { buff.close(); System.err.println("OVER, and i is " + i); } catch (IOException e) { e.printStackTrace(); } } public static void main1(String[] args) { ThreadFight0 fight = new ThreadFight0(); Thread thread = new Thread(fight); thread.start(); } public static void main2(String[] args) { ThreadFight0 fight = new ThreadFight0(); ThreadFight0 fight2 = new ThreadFight0(); Thread thread2 = new Thread(fight2); Thread thread = new Thread(fight); thread.start(); thread2.start(); } public static void main(String[] args) { //main1(args); main2(args); } }