code_money_guji's Blog

Happy coding

给 code_money_guji 留言

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Avatar_small
[url=http://www.i-on 说:
2018年9月03日 21:57

Looking for <a href=http://ii-online-casino.com>casino</a>?Check out top free <a href=http://www.i-online-casino.org/>casino online</a> bonus offers at <a href=http://www.i-online-casino.org/Slotocash-Casino.html>online casino</a>

Avatar_small
pay day loans cash a 说:
2017年10月28日 23:59

pay day loans
cash advance loan
online loans
payday loan direct lender

Avatar_small
buy cialis new zeala 说:
2017年8月10日 21:39

buy cialis new zealand
tadalafil
cialis generique discount

Avatar_small
часы 说:
2017年2月12日 14:59


<a href=http://xn----8sbwhzpef4b3bg.xn--j1amh/content/15-naruchnye-chasy-izyuminka-dlya-vashego-imidzha>Наручные часы – изюминка для вашего имиджа</a> купить в Украине в интернет-магазине с доставкой наложкой

<a href=http://xn----8sbwhzpef4b3bg.xn--j1amh/><img>http://xn----8sbwhzpef4b3bg.xn--j1amh/upload/stowlcarousel/7dc1ada023d8d1a01c939bfce5047be9.jpg</img></a>

Avatar_small
I rellay couldn't as 说:
2013年6月14日 14:59

I rellay couldn't ask for more from this article.

Avatar_small
coffee 说:
2013年5月07日 17:31

 

你好! 今天需要写网络文件传输,先做了一个本地的文件文件多线程拷贝的测试,使用的MappedByteBuffer,但是在10个线程拷贝文件块的时候出现了访问文件锁的问题 java.lang.Error: Cleaner terminated abnormally at sun.misc.Cleaner$1.run(Cleaner.java:130) at java.security.AccessController.doPrivileged(Native Method) at sun.misc.Cleaner.clean(Cleaner.java:127) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:124) Caused by: java.io.IOException: 另一个程序已锁定文件的一部分,进程无法访问。 at sun.nio.ch.FileChannelImpl.unmap0(Native Method) at sun.nio.ch.FileChannelImpl.access$100(FileChannelImpl.java:32) at sun.nio.ch.FileChannelImpl$Unmapper.run(FileChannelImpl.java:667) at sun.misc.Cleaner.clean(Cleaner.java:125) ... 1 more 这个异常.我使用的AtomicLong作为文件拷贝起始位置pos的记录,所以应该不会出现写入块复合的问题.在5个线程拷贝的时候并不会出现这个问题的.,我测试的文件是3.3G,很奇怪,看到你有一篇文章也是写使用内存映射实现文件拷贝,所以想和你探讨一下,附上我的测试代码,请帮我看看为什么线程到达10个的时候会报错,如果有答案可以联系我,邮件coffee_hc@163.com.QQ:58848232

public class FileTest {
public static void main(final String[] args) {
File file = new File("d:/src.dmp");
File newFile = new File("d:/test.dmp");
final long fileSize = file.length();
newFile.delete();
try {
RandomAccessFile oldRandomAccessFile = new RandomAccessFile(file, "r");
final FileChannel oldChannel = oldRandomAccessFile.getChannel();
RandomAccessFile randomAccessFile = new RandomAccessFile(newFile, "rw");
randomAccessFile.setLength(fileSize);
final FileChannel newChannel = randomAccessFile.getChannel();
long startTime = System.currentTimeMillis();
System.out.println("fileSize:" + fileSize);
final int copySize = 1024 * 1024;
final AtomicLong i = new AtomicLong(0);
int theardNum = 10;
final CountDownLatch countDownLatch = new CountDownLatch(theardNum);
for (long num = 0; num < theardNum; num++) {
Thread t = new Thread(new Runnable() {
 
@Override
public void run() {
while (true) {
long pos = i.getAndAdd(copySize);
int cSize = copySize;
if (pos < fileSize) {
cSize = (int) ((fileSize - pos) > cSize ? cSize : fileSize - pos);
try {
System.out.println(pos + "----" + cSize);
MappedByteBuffer inBuffer = oldChannel.map(MapMode.READ_ONLY, pos, cSize);
MappedByteBuffer outBuffer = newChannel.map(MapMode.READ_WRITE, pos, cSize);
outBuffer.put(inBuffer);
outBuffer.load();
} catch (Exception e) {
e.printStackTrace();
break;
} finally {
System.gc();
}
} else {
break;
}
 
}
countDownLatch.countDown();
}
});
t.start();
}
countDownLatch.await();
randomAccessFile.close();
oldRandomAccessFile.close();
long endTime = System.currentTimeMillis();
double speed = 1.0D * fileSize / (endTime - startTime) / 1024 / 1024 * 1000;
System.out.println("拷贝速度:" + speed + "M/S");
} catch (Exception e) {
e.printStackTrace();
}
}
}