黑马程序员技术交流社区
标题:
一个关于多线程IO的问题
[打印本页]
作者:
韩伟
时间:
2012-8-2 16:06
标题:
一个关于多线程IO的问题
import java.io.*;
class FileDemo2
{
public static void main(String []args)
{
Object obj = new Object();
File []file = File.listRoots();
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 t4 = new Thread(ld3);
Thread t3 = new Thread(ld4);
t1.start();
t2.start();
t3.start();
t4.start();
try
{
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) throws Exception
{
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)
{
bfw.write(str.getAbsolutePath()+"...."+str.isHidden());
bfw.newLine();
bfw.flush();
}
}
}
}
}
public void run()
{
try
{
listDemo(file, bfw);
}
catch(Exception e)
{
throw new RuntimeException("写入文件异常!");
}
}
}
复制代码
我的计划是这样的,每个线程读取一个盘符的文件,都写到一个txt文件中,按理说只有写入时才会有冲突,所以我把写入的那些代码加了锁,可是那些锁好像有问题,
请大家给指点一下应该怎么改!{:soso_e183:}
作者:
胡文凡
时间:
2012-8-2 19:29
问题找到了。题目有误导的意思,不是锁的问题
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("写入文件异常!");
}
}
}
结果:
QQ截图20120802192846.png
(19.91 KB, 下载次数: 19)
下载附件
2012-8-2 19:28 上传
作者:
韩伟
时间:
2012-8-3 08:43
问题解决了,谢谢。不过还有一点儿不明白,为什么这个程序比之前单线程时运行慢,而且还慢了很多!多线程不是应该加快速度的吗?
作者:
胡文凡
时间:
2012-8-3 12:00
递归加锁,我怀疑是这方面的问题。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2