This article is a summary of the study of the article on the network, thank you for your selfless sharing.
JDK 1.7 Adds a new tool to Phaser,phaser's functionality with The Countdownlatch is partially coincident.
The following uses the Phaser class to synchronize 3 concurrent tasks. These 3 tasks will search for files with a. log extension in 3 different folders and their subfolders. This task is divided into 3 steps:
1. Get a list of files with a. log file name extension in the specified folder and subfolders.
2. Print the results on the console.
at the end of Step 1 and step 2 we want to check if the list is empty. If it is empty, then the thread ends up running and is retired from the Phaser class.
Package Chapter3;import Java.io.file;import Java.util.arraylist;import java.util.date;import java.util.List;import Java.util.concurrent.phaser;import Java.util.concurrent.timeunit;public class FileSearch implements Runnable{ private string Initpath;private string end;private list<string> results;private Phaser phaser;public FileSearch ( String initpath,string end,phaser Phaser) {This.initpath = Initpath;this.end = End;this.phaser = Phaser;this.results = new Arraylist<string> ();} private void directoryprocess (file file) {file list[] = File.listfiles (), if (list! = null) {for (int i = 0;i< list.length;i + +) {if (List[i].isdirectory ()) {directoryprocess (list[i]);} Else{fileprocess (List[i]);}}} private void fileprocess (file file) {if (File.getname (). EndsWith (end)) {Results.add (File.getabsolutepath ());}} private void Filterresults () {list<string> newresults = new arraylist<string> (); Long actualdate = new Date (). GetTime (); for (int i = 0; i < results.size (); i++) {File File = new FIle (Results.get (i)); Long filedate = File.lastmodified (); if (Actualdate-filedate<timeunit.milliseconds.convert (1 , timeunit.days)) {Newresults.add (Results.get (i))}} results = Newresults;} Private Boolean checkresults () {if (Results.isempty ()) {System.out.printf ("%s:phase%d:0 results.\n", Thread.CurrentThread (). GetName (), phaser.getphase ()); System.out.printf ("%s:phase%d:end.\n", Thread.CurrentThread (). GetName (), phaser.getphase ()); Phaser.arriveandderegister (); return false;} else{system.out.printf ("%s:phase%d:%d results.\n", Thread.CurrentThread (). GetName (), Phaser.getphase (), Results.size ());p haser.arriveandawaitadvance (); return true;}} private void Showinfo () {for (int i = 0; i < results.size (); i++) {File File = new file (Results.get (i)); System.out.printf ("%s:%s\n", Thread.CurrentThread (). GetName (), File.getabsolutepath ());} Phaser.arriveandawaitadvance ();} @Overridepublic void Run () {phaser.arriveandawaitadvance (); System.out.printf ("%s:starting.\n", Thread.CurrentThread (). GetName ()); File File = new file (Initpath), if (File.isdirectory ()) {directoryprocess (file);} if (!checkresults ()) {return;} Filterresults (); if (!checkresults ()) {return;} Showinfo ();p haser.arriveandderegister (); System.out.printf ("%s:work completed.\n", Thread.CurrentThread (). GetName ());}}
Package Chapter3;import Java.util.concurrent.phaser;public class Main5 {/** * <p> * </p> * @author Zhangjunshu Ai * @date 2014-9-29 pm 4:31:46 * @param args */public static void main (string[] args) {Phaser Phaser = new Phaser (3); FileSearch system = new FileSearch ("C:\\Windows", "Log", phaser); FileSearch apps = new FileSearch ("C:\\Program Files", "Log", phaser); FileSearch documents = new FileSearch ("C:\\Documents and Settings", "Log", phaser); Thread systemthread = new Thread (System, "system"); Systemthread.start (); Thread appsthread = new Thread (Apps, "apps"); Appsthread.start (); Thread documentsthread = new Thread (documents, "Documents");d Ocumentsthread.start (); try {systemthread.join (); Appsthread.join ();d ocumentsthread.join ();} catch (Exception e) {e.printstacktrace ();} System.out.println ("Terminated:" + phaser.isterminated ());}}
Run results
Java Concurrency Learning 19--The Thread synchronization tool Phaser