我的目的是同步上传主要位置的文件,同时异步上载多个辅助位置上的同一文件。那么请你帮我实现辅助位置写入的多线程(例如同一个文件在读取时同时写多个路径)
以下是从输入流中读取的代码
List<FileUploadMultiLocator> fileUploadList = new ArrayList<>();
FileUploadMultiLocator fum1 = new FileUploadMultiLocator("fileserver0", new BufferedOutputStream(new FileOutputStream(new File(reposMap.get("D:\\Harisingh\\FileUpload\\Destination1.txt")))));
FileUploadMultiLocator fum2 = new FileUploadMultiLocator("fileserver0", new BufferedOutputStream(new FileOutputStream(new File(reposMap.get("D:\\Harisingh\\FileUpload\\Destination2.txt")))));
fileUploadList.add(fum1);
fileUploadList.add(fum2);
while((len=channel.read(byteBuffer))>=0)
{
itrCount++;
totalBytes+=len;
baOS.write(byteBuffer.array(),0,len);
if(itrCount>=maxIterateCnt)
{
//primary location writing
bFout.write(baOS.toByteArray(),0,totalBytes);
// this is for secondary location writing
for (FileUploadMultiLocator fileUploadMultiLocator : fileUploadList) {
fileUploadMultiLocator.baOS = baOS;
fileUploadMultiLocator.totalBytes = totalBytes;
new Thread(fileUploadMultiLocator).start();
}
totalBytes=0;
baOS.reset();
itrCount=0;
}
byteBuffer.clear();
}
这是我的runnable类,可以同时写多个BufferedOutputStream
public class FileUploadMultiLocator实现Runnable {
public FileUploadMultiLocator(String fileserver,BufferedOutputStream bFout) {
// TODO Auto-generated constructor stub
this.fileserver = fileserver;
this.bFout = bFout;
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
bFout.write(baOS.toByteArray(),0,totalBytes);
bFout.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}