Java Concurrency Learning 19--The Thread synchronization tool Phaser

Source: Internet
Author: User

This article is a summary of learning about articles on the web. Thank you for your selfless sharing.

JDK 1.7 has added a new tool, Phaser. The function of the Phaser is partially coincident with the Countdownlatch.

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 directories and their subdirectories.

This task is divided into 3 steps:

1. Obtain a list of files with a. log file name extension in the specified directory and subdirectories.
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.

assumed to be empty. Then the thread ends the execution directly 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 ());}}

Execution results




Java Concurrency Learning 19--The Thread synchronization tool Phaser

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.