在while循环中调用completableFuture,使byteArrayOutputStream无法正常工作

时间:2018-04-19 09:10:45

标签: java io stream completable-future

CompletableFuture可以在简单的异步任务中正常工作,但我在读取文件和写入多个文件时在while循环中尝试过相同的代码: -

BufferedInputStream bin = null;
ReadableByteChannel channel = null;
int bufferSize = 1048576;
int readBufferSize = 1024*4;
java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.allocate(readBufferSize);
InputStream is = new FileInputStream(new File("D:\\Harisingh\\myfile.txt"));

bin = new BufferedInputStream(is,bufferSize);
channel = Channels.newChannel(bin);
int retryCnt = 0;
ByteArrayOutputStream baOS = new ByteArrayOutputStream(bufferSize);
int totalBytes=0;
int itrCount=0;
int maxIterateCnt = 1;
int len;
//primary location file path
BufferedOutputStream bFout = new BufferedOutputStream(new FileOutputStream(new File("D:\\Harisingh\\FileUpload\\primary.txt")));
//secondary location file path
FileUploadMultiLocator fileUploadMultiLocator = new FileUploadMultiLocator("fileserver0",new BufferedOutputStream(new FileOutputStream(new File("D:\\Harisingh\\SecondaryPath\\Secondary1.txt"))));

 if(bufferSize > readBufferSize){
      maxIterateCnt = bufferSize/readBufferSize;
 }
while((len=channel.read(byteBuffer))>=0) 
            {
                itrCount++;
                totalBytes+=len;
                baOS.write(byteBuffer.array(),0,len);
                if(itrCount>=maxIterateCnt)
                {
                    //Writing to one file on primary location only
                    try{
                        bFout.write(baOS.toByteArray(),0,totalBytes);
                    }catch(Exception se)
                    {
                    }
                  //sending baOS for writing to other files on secondary locations with fileUploadMultiLocator runnable class
                    createCompletableFutureTask(baOS, totalBytes, fileUploadMultiLocator);
                    totalBytes=0;
                    baOS.reset();
                    itrCount=0;
                }
                byteBuffer.clear();
            }

createCompletableFutureTask(baOS,totalBytes,fileUploadMultiLocator)方法代码如下: -

public static void createCompletableFutureTask(ByteArrayOutputStream baOS, int totalBytes, FileUploadMultiLocator fileUploadMultiLocator){
    // Submit Task 1
     CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
         try {
             fileUploadMultiLocator.baOS.write(baOS.toByteArray(), 0, totalBytes);
             fileUploadMultiLocator.setTotalBytes(totalBytes);
             new Thread(fileUploadMultiLocator).start();                    
         } catch (Exception e) {
         }
         System.out.println("Task 1 completed");
         return 5;
     });
}

我认为正如我们所知,completableFuture是异步执行的,在这种情况下,我在读取文件时在while循环中发送baOS所以由于在循环中进行线程化,任务无法控制baOS,因此它只能写入某些字节不是所有辅助位置

仅供参考我想通知你,如果我添加

  

f1.get()

在createCompletableFutureTask方法中的

代码然后它工作正常,但它同步工作,这意味着主要&amp;由于同步行为,二次写入将同时完成。

但我希望主要位置写入应该由主线程同步完成,辅助位置写入应该由completableFuture任务在后台异步完成。能帮助我实现这个目标吗?

0 个答案:

没有答案