Multi-threaded File replication

Source: Internet
Author: User

To implement file replication with Multithreading:

Main function class:

1  Public classThreadcopyfile {2 3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5 copyFile ();6     }7 8     Static voidCopyFile () {9         //source FileTenFile File =NewFile ("E:\\test\\ai.mp4"); One         //target file AFile file2 =NewFile ("E:\\test\\copyai.mp4"); -         //total file size, in bytes -         LongFileSize =file.length (); the         //Number of blocks, number of processes -         intBlockcount = 3; -         //the size of each block -         LongPerblocksize = fileSize/Blockcount; +         //the size of the last piece -         LongLastblocksize = perblocksize + fileSize%Blockcount; +         //previous two: 0,1 A          for(inti = 0; i < blockCount-1; i++) { at             //incoming parameters are: source file, destination file, file block start location, file block size, thread name -             NewCopythread (file, file2, perblocksize * I, Perblocksize, "Tom" -+i). Start (); -         } -         //last time -         NewCopythread (file, File2, Perblocksize * (blockCount-1), inLastblocksize, "CC"). Start (); -     } to}

Thread class:

1  Public classCopythreadextendsThread {2     Privatefile file;3     Private Longbegin;4     PrivateFile file2;5     Private Longpersize;6 7      PublicCopythread () {8         //TODO auto-generated Constructor stub9 Ten     } One  A      PublicCopythread (file file, file File2,LongBeginLongPersize, - String name) { -         Super(name);//Process Name the          This. begin = Begin;//Start Position -          This. File = file;//source File -          This. file2 = File2;//target file -          This. persize = persize;//File Block Size +  -     } +  A @Override at      Public voidrun () { -         //open a Randomaccessfile object for each thread, -         //have each thread be responsible for reading different parts of the file separately -Randomaccessfile RAF =NULL; -Randomaccessfile raf2 =NULL; -         //granularity, if large, will make the copy file larger than the source file, and if it is small, the time to copy the file will be extended in         //replication Results 10k, should not be at the end of 1k1k copy it? or let the last remainder of the file 1 bytes 1 bytes to read, the last word surplus number of files is the number of threads!!  -         //hahaha problem has been solved!!!  to         byte[] B =New byte[10 * 1024]; +         inti = 0; -         //record the cursor copied to each file block the         intLen = 0; *         Try { $RAF =NewRandomaccessfile (file, "RW");Panax NotoginsengRAF2 =NewRandomaccessfile (File2, "RW"); -             //set the start point of a read file the Raf.seek (begin); +             //The default file is the cc->tom1->tom0 that is read from the back forward??? A             //when the end of the file is not read and the length of the read is less than the size of the file block allocated by each thread, the execution the             //If you don't add (len<persize) just read from the end of the file, and overwrite the read +              while((((i = Raf.read (b))! =-1) && (Len <persize)) { - Raf2.seek (begin); $                 //accurate to 1 bytes!!  $Raf2.write (b, 0, i); -Len + =i; -Begin + =i; the             } -             //System.out.println (Thread.CurrentThread (). GetName ());Wuyi}Catch(IOException e) { the             //TODO auto-generated Catch block - e.printstacktrace (); Wu}finally { -             Try { About raf.close (); $ raf2.close (); -}Catch(IOException e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); A             } +         } the     } -}

Multi-threaded File replication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.