黑马程序员技术交流社区

标题: 谁能帮我看看哪里出错了 [打印本页]

作者: 郭阳    时间: 2012-9-26 13:32
标题: 谁能帮我看看哪里出错了
本帖最后由 郭阳 于 2012-9-26 19:52 编辑
  1. import java.io.*;
  2. class IODemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 BugThread bt=new BugThread();
  7.                 Thread t1=new Thread (bt);
  8.                 Thread t2=new Thread (bt);
  9.                 Thread t3=new Thread (bt);
  10.                 Thread t4=new Thread (bt);
  11.                 Thread t5=new Thread (bt);
  12.                 t1.start();
  13.                 t2.start();
  14.                 t3.start();
  15.                 t4.start();
  16.                 t5.start();        
  17.         }
  18. }

  19. class BugThread  implements Runnable
  20. {        
  21.         FileWriter fw=null;
  22.         try
  23.         {
  24.                 fw=new FileWriter("Demo.txt");
  25.         }
  26.         catch (Exception e){}        

  27.         public void run()
  28.         {               
  29.                 for (;; )
  30.                 {
  31.                         try
  32.                         {
  33.                                 fw.write("abcdefg");
  34.                                 fw.flush();
  35.                         }
  36.                         catch(Exception e){}
  37.                 }        
  38.         }
  39. }
复制代码


作者: 郭阳    时间: 2012-9-26 16:40
自己顶一下。。。
作者: 孙岳    时间: 2012-9-26 17:55
本帖最后由 孙岳 于 2012-9-26 18:23 编辑

import java.io.*;
class IODemo
{
        public static void main(String[] args)
        {
                 BugThread bt=new BugThread();
                 bt.demo();     //这里调用一下
                Thread t1=new Thread (bt);
                Thread t2=new Thread (bt);
                Thread t3=new Thread (bt);
                Thread t4=new Thread (bt);
                Thread t5=new Thread (bt);
                t1.start();
                t2.start();
                t3.start();
                t4.start();
                t5.start();        
        }
}

class BugThread  implements Runnable
{      
        
        FileWriter fw=null;
        // 定义一个方法,这一段必须写在方法里面,类里面不能直接写命令语句。        
        public void demo()
        {
                try
                {
                       fw=new FileWriter("Demo.txt");
                }
              catch (Exception e){}  
        }      
        public void run()
        {               
                for (int i = 0; i < 5; i++ )//这里要加限定条件,否则Demo.txt文件将会变得特别大。你让它无限打印是想做病毒吗?
                {
                        try
                        {
                                fw.write("abcdefg\r\n");   //这里加个换行\r\n,一共打印25行。
                                fw.flush();
                        }
                        catch(Exception e){}
                }        
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2