java上传文件功能_Java MemoryMapped文件的功能
java上傳文件功能
Java MemoryMapped文件的功能
在JDK 1.4中,內存映射文件的一個有趣功能被添加到Java中,該功能允許將任何文件映射到OS內存以進行有效讀取。 內存映射文件可用于開發IPC類型的解決方案。 本文是使用內存映射文件創建IPC的實驗。
有關內存映射文件的一些詳細信息,來自WIKI的定義
內存映射文件是虛擬內存的一部分,已為其分配了與文件或類似文件的資源的某些部分的逐字節直接相關性。 此資源通常是物理上存在于磁盤上的文件,但也可以是設備,共享內存對象或操作系統可以通過文件描述符引用的其他資源。 一旦存在,文件和內存空間之間的這種關聯關系允許應用程序將映射部分視為主內存。
樣例程序
下面我們有兩個Java程序,一個是作者,另一個是讀者。 寫入者是生產者,嘗試寫入“內存映射”文件,讀取者是使用者,它從內存映射文件中讀取消息。 這只是一個示例程序,向您展示了這個想法,它不能處理許多極端情況,但足以在內存映射文件的頂部構建內容。
MemoryMapWriter
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel;public class MemoryMapWriter {public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {File f = new File("c:/tmp/mapped.txt");f.delete();FileChannel fc = new RandomAccessFile(f, "rw").getChannel();long bufferSize=8*1000;MappedByteBuffer mem =fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);int start = 0;long counter=1;long HUNDREDK=100000;long startT = System.currentTimeMillis();long noOfMessage = HUNDREDK * 10 * 10; for(;;){ if(!mem.hasRemaining()){start+=mem.position();mem =fc.map(FileChannel.MapMode.READ_WRITE, start, bufferSize);}mem.putLong(counter); counter++;if(counter > noOfMessage )break; }long endT = System.currentTimeMillis();long tot = endT - startT;System.out.println(String.format("No Of Message %s , Time(ms) %s ",noOfMessage, tot)) ; }}MemoryMapReader
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel;public class MemoryMapReader {/*** @param args* @throws IOException * @throws FileNotFoundException * @throws InterruptedException */public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {FileChannel fc = new RandomAccessFile(new File("c:/tmp/mapped.txt"), "rw").getChannel();long bufferSize=8*1000;MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, bufferSize);long oldSize=fc.size();long currentPos = 0;long xx=currentPos;long startTime = System.currentTimeMillis();long lastValue=-1;for(;;){while(mem.hasRemaining()){lastValue=mem.getLong();currentPos +=8;}if(currentPos < oldSize){xx = xx + mem.position();mem = fc.map(FileChannel.MapMode.READ_ONLY,xx, bufferSize);continue; }else{long end = System.currentTimeMillis();long tot = end-startTime;System.out.println(String.format("Last Value Read %s , Time(ms) %s ",lastValue, tot));System.out.println("Waiting for message");while(true){long newSize=fc.size();if(newSize>oldSize){oldSize = newSize;xx = xx + mem.position();mem = fc.map(FileChannel.MapMode.READ_ONLY,xx , oldSize-xx);System.out.println("Got some data");break;}} }}}}觀察
使用內存映射文件對于開發進程間通信可能是一個很好的選擇,對于生產者和消費者而言,吞吐量也相當不錯。 按生產者和消費者運行的性能統計數據:
每條消息是一個長號
產生– 1000萬條消息– 16個
消費者– 1000萬條消息0.6(s)
一條非常簡單的消息用于說明您的想法,但是它可以是任何類型的復雜消息,但是當存在復雜的數據結構時,序列化會增加開銷。 有許多技術可以克服這些開銷。 下一個博客中的更多內容。
參考:來自JCG合作伙伴 Ashkrit Sharma 的Java MemoryMapped File的強大功能,在Are you ready博客上。翻譯自: https://www.javacodegeeks.com/2013/05/power-of-java-memorymapped-file.html
java上傳文件功能
總結
以上是生活随笔為你收集整理的java上传文件功能_Java MemoryMapped文件的功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Pulsar:分布式发布订
- 下一篇: 怎么用蓝牙控制电脑(如何用蓝牙控制电脑)