A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 韩伟 中级黑马   /  2012-8-2 16:06  /  2136 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. class FileDemo2
  3. {
  4.         public static void main(String []args)
  5.         {
  6.                 Object obj = new Object();
  7.                 File []file = File.listRoots();
  8.                 BufferedWriter bfw;
  9.                 try
  10.                 {
  11.                         bfw = new BufferedWriter(new FileWriter("f:\\exe.txt"));
  12.                 }
  13.                 catch(Exception e)
  14.                 {
  15.                         throw new RuntimeException("CreateFile Error!");
  16.                 }       
  17.                 ListDemo ld1 = new ListDemo(file[0],bfw,obj);
  18.                 ListDemo ld2 = new ListDemo(file[1],bfw,obj);
  19.                 ListDemo ld3 = new ListDemo(file[2],bfw,obj);
  20.                 ListDemo ld4 = new ListDemo(file[3],bfw,obj);
  21.                 Thread t1 = new Thread(ld1);
  22.                 Thread t2 = new Thread(ld2);
  23.                 Thread t4 = new Thread(ld3);
  24.                 Thread t3 = new Thread(ld4);
  25.                
  26.                 t1.start();
  27.                 t2.start();
  28.                 t3.start();
  29.                 t4.start();
  30.                
  31.                 try
  32.                 {
  33.                         bfw.close();                       
  34.                 }
  35.                 catch(Exception e)
  36.                 {
  37.                         throw new RuntimeException("CloseFile Error!");
  38.                 }               
  39.         }       
  40. }
  41. class ListDemo implements Runnable
  42. {
  43.         private File file;
  44.         private BufferedWriter bfw;
  45.         private Object obj;
  46.         ListDemo(File file, BufferedWriter bfw,Object obj)
  47.         {
  48.                 this.file = file;
  49.                 this.bfw = bfw;
  50.                 this.obj = obj;
  51.         }               
  52.         public  void listDemo(File file,BufferedWriter bfw) throws Exception
  53.         {               
  54.                 File [] name = file.listFiles();
  55.                 if(name != null)
  56.                 for(File str: name)
  57.                 {
  58.                         if(str.isDirectory())
  59.                             listDemo(str,bfw);                       
  60.                         else
  61.                         {
  62.                             if(str.getName().endsWith(".exe"))
  63.                             {
  64.                                     synchronized(obj)
  65.                                          {
  66.                                                  bfw.write(str.getAbsolutePath()+"...."+str.isHidden());
  67.                                                  bfw.newLine();
  68.                                                  bfw.flush();
  69.                                          }
  70.                                  }
  71.                          }                                                       
  72.                 }
  73.         }       
  74.         public void run()
  75.         {
  76.                 try
  77.                 {
  78.                         listDemo(file, bfw);
  79.                 }
  80.                 catch(Exception e)
  81.                 {
  82.                         throw new RuntimeException("写入文件异常!");
  83.                 }
  84.         }       
  85. }
复制代码
我的计划是这样的,每个线程读取一个盘符的文件,都写到一个txt文件中,按理说只有写入时才会有冲突,所以我把写入的那些代码加了锁,可是那些锁好像有问题,
请大家给指点一下应该怎么改!{:soso_e183:}

3 个回复

倒序浏览
问题找到了。题目有误导的意思,不是锁的问题
package what;



import java.io.*;
class FileDemo2
{
        public static void main(String []args)
        {
                Object obj = new Object();
                File []file = File.listRoots();
                for(File fil :file){
                        System.out.println(fil);
                }
                BufferedWriter bfw;

                try
                {
                        bfw = new BufferedWriter(new FileWriter("f:\\exe.txt"));

                }
                catch(Exception e)
                {
                        throw new RuntimeException("CreateFile Error!");
                }        
                ListDemo ld1 = new ListDemo(file[0],bfw,obj);
                ListDemo ld2 = new ListDemo(file[1],bfw,obj);
                ListDemo ld3 = new ListDemo(file[2],bfw,obj);
                ListDemo ld4 = new ListDemo(file[3],bfw,obj);

                Thread t1 = new Thread(ld1);
                Thread t2 = new Thread(ld2);
                Thread t3 = new Thread(ld3);
                Thread t4 = new Thread(ld4);

               
                t1.start();
                t2.start();
                t3.start();
                t4.start();

               
         /*    try                                                        //原因就是这了。线程还没有结束,bfw就关闭了,当然不能输出内容,有时候运气好会输出一个。。。                {
                        bfw.close();                        
                     
                }
                catch(Exception e)
                {
                        throw new RuntimeException("CloseFile Error!");
                }   */            
        }        
}
class ListDemo implements Runnable
{
        private File file;
        private BufferedWriter bfw;
        private Object obj;
        ListDemo(File file, BufferedWriter bfw,Object obj)
        {
                this.file = file;
                this.bfw = bfw;
                this.obj = obj;
        }               
        public  void listDemo(File file,BufferedWriter bfw)
        {               
                File [] name = file.listFiles();
                if(name != null)
                for(File str: name)
                {
                        if(str.isDirectory())
                            listDemo(str,bfw);                        
                        else
                        {
                            if(str.getName().endsWith(".exe"))
                            {
                                    //synchronized(obj)
                                         
                                                try {
                                                        System.out
                                                                                                                        .println(str.getAbsolutePath()+"...."+str.isHidden());
                                                                                                         bfw.write(str.getAbsolutePath()+"...."+str.isHidden());
                                                 bfw.newLine();
                                                 bfw.flush();
                                                                                                } catch (Exception e) {
                                                                                                        // TODO: handle exception
                                                                                                        new RuntimeException("bb");
                                                                                                }
                                         
                                 }
                         }                                                        
                }
        }        
        public void run()
        {
                try
                {
                        listDemo(file, bfw);
                }
                catch(Exception e)
                {
                        throw new RuntimeException("写入文件异常!");
                }
        }        
}
结果:
回复 使用道具 举报
问题解决了,谢谢。不过还有一点儿不明白,为什么这个程序比之前单线程时运行慢,而且还慢了很多!多线程不是应该加快速度的吗?
回复 使用道具 举报
递归加锁,我怀疑是这方面的问题。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马