Home > Back-end >  CompletableFuture instance (a) _ for multithreaded copying specified directory
CompletableFuture instance (a) _ for multithreaded copying specified directory

Time:09-27

Recently in learning Java8 CompletableFuture class, want to learn the multithreading,
Also try to write some code, deficiencies still please people more directions!
 import Java. IO. IOException; 
Import the Java. Nio. File. FileSystems;
Import the Java. Nio. File. FileVisitResult;
Import the Java. Nio. File. The Files;
Import the Java. Nio. File. The Path;
Import the Java. Nio. File. Paths;
Import the Java. Nio. File. SimpleFileVisitor;
Import the Java. Nio. File. The attribute. The BasicFileAttributes;
import java.util.ArrayList;
Import the Java. Util. Collections;
import java.util.List;
Import the Java. Util. Objects;
The import java.util.concurrent.Com pletableFuture;
Import the Java. Util. Concurrent. The ExecutorService;
Import the Java. Util. Concurrent. Executors;
Import the Java. Util. Concurrent. TimeUnit;

Public class Test {
Private static final ExecutorService serivce=Executors. NewFixedThreadPool (Runtime. The getRuntime (). AvailableProcessors ());

The public Test () {
//TODO Auto - generated constructor stub
}

Private static List ExecFileCopy (String SRC, String tar) {
List LST=Collections. SynchronizedList (new ArrayList ());

Try {
The Path target=FileSystems. GetDefault (.) getPath (tar);
Files. The walkFileTree (Paths. The get (SRC), new SimpleFileVisitor () {
Public FileVisitResult preVisitDirectory (Path dir, BasicFileAttributes attrs) throws IOException {
Objects. RequireNonNull (dir);
Objects. RequireNonNull (attrs);
The Path targetPath=target. Resolve (dir. Subpath (0, dir. GetNameCount ()));
Files. CreateDirectories (targetPath);
Return FileVisitResult. CONTINUE;
}

Public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {
Objects. RequireNonNull (file);
The Path targetPath=target. Resolve (file. Subpath (0, the file. GetNameCount ()));
LST. Add (CompletableFuture. RunAsync (() - & gt; {
Try {
Files. The copy (file, targetPath);
} the catch (IOException e) {
//TODO Auto - generated the catch block,
E.p rintStackTrace ();
}
}, serivce));
Return FileVisitResult. CONTINUE;
}
});
} the catch (IOException e) {
E.p rintStackTrace ();
}
Return LST.
}

Public static void main (String [] args) throws the Exception {
Long s=System. CurrentTimeMillis ();
String tar="E: \ \ 3";
String SRC="F: \ \ angular2";//directory contains 386 m 734 m size file (1), 185 m files (1), the remaining 26918 files for grade K pieces file

List List=execFileCopy (SRC, tar);
CompletableFuture ControlFuture=CompletableFuture. AllOf (list. ToArray (new CompletableFuture [] {}));
while(! ControlFuture. IsDone () {}
System. The out. Println (" exception found: "+ controlFuture. IsCompletedExceptionally ());

System. The out. Println ((System. CurrentTimeMillis () - s)/1000);//takes 27 seconds system environment (i5-3337 - u 4 g 256 GSSD ramdisk Win10)
}
}
  • Related