java 多线读文件是同步的吗

iuoon 2016-05-20 01:56:51
按一行行读:
查看jdk中BufferedReader源码:这里面synchronized 的作用

String readLine(boolean ignoreLF) throws IOException {
StringBuffer s = null;
int startChar;

synchronized (lock) {
ensureOpen();
boolean omitLF = ignoreLF || skipLF;

bufferLoop:
for (;;) {
//...省略
}
}


用nio文件通道方式读取,源码中也加上了synchronized :

public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, path, true, false, this);
}
return channel;
}
}


测试代码:

public String readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
FileInputStream f=null;
// System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString;
String mess="";
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
mess+=tempString;
}
reader.close();
return mess;
} catch (IOException e) {
return "read file error";
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}


当用loadrunner测试下,读本地文件的性能急剧下降
查看jvm日志如下:

Thread Execution Information:
-----------------------
Thread "http-thread-pool-8080(98)" thread-id: 837 thread-state: TIMED_WAITING Waiting on lock: java.lang.Object@583dadc8
at: java.lang.Object.wait(Native Method)
at: com.sun.grizzly.util.SyncThreadPool$SyncThreadWorker.getTask(SyncThreadPool.java:374)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:524)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at: java.lang.Thread.run(Thread.java:745)
Thread Synchronization Statistics:
-----------------------
Number of times this thread was blocked (to enter/reenter a Monitor): 4,582
Number of times this thread waited for a notification (i.e. it was in WAITING or TIMED_WAITING state): 4,522
Total CPU time for this thread: 1 seconds 189,494,744 nanoseconds.
User-level CPU time for this thread: 1 seconds 189,494,744 nanoseconds.
Object Monitors currently held or requested by this thread: []
Ownable Synchronizers (e.g. ReentrantLock and ReentrantReadWriteLock) held by this thread: []
--------------------------------------------------------------------------------
Thread Execution Information:
-----------------------
Thread "http-thread-pool-8080(97)" thread-id: 836 thread-state: TIMED_WAITING Waiting on lock: java.lang.Object@583dadc8
at: java.lang.Object.wait(Native Method)
at: com.sun.grizzly.util.SyncThreadPool$SyncThreadWorker.getTask(SyncThreadPool.java:374)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:524)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at: java.lang.Thread.run(Thread.java:745)
Thread Synchronization Statistics:
-----------------------
Number of times this thread was blocked (to enter/reenter a Monitor): 4,585
Number of times this thread waited for a notification (i.e. it was in WAITING or TIMED_WAITING state): 4,523
Total CPU time for this thread: 1 seconds 194,873,842 nanoseconds.
User-level CPU time for this thread: 1 seconds 194,873,842 nanoseconds.
Object Monitors currently held or requested by this thread: []
Ownable Synchronizers (e.g. ReentrantLock and ReentrantReadWriteLock) held by this thread: []
--------------------------------------------------------------------------------
Thread Execution Information:
-----------------------
Thread "http-thread-pool-8080(96)" thread-id: 835 thread-state: TIMED_WAITING Waiting on lock: java.lang.Object@583dadc8
at: java.lang.Object.wait(Native Method)
at: com.sun.grizzly.util.SyncThreadPool$SyncThreadWorker.getTask(SyncThreadPool.java:374)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:524)
at: com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at: java.lang.Thread.run(Thread.java:745)
Thread Synchronization Statistics:


所以问下,多线程读取文件是同步的吗,
如果是,java有其他高效多线读取文件的方法吗
...全文
157 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧