Phaser表示“阶段器”,用来解决控制多个线程分阶段共同完成任务的情景问题
启动三个线程,分别对三个文件夹搜索,文件要以txt结尾,修改时间要在一天之内,并将文件路径打印在控制台
/** * 开启三个线程对三个文件夹进行搜索 * */ public class FileSearch implements Runnable { private String path;// 查找路径 private String suffix;// 文件后缀 private List<String> result;// 结果集 private Phaser phaser; public FileSearch(String path, String end, Phaser phaser) { this.path = path; this.suffix = end; this.phaser = phaser; this.result = new ArrayList<String>(); } private void directoryProcess(File file) { File list[] = file.listFiles(); if (list != null) { for (File file2 : list) { if (file2.isDirectory()) { directoryProcess(file2); } else { fileProcess(file2); } } } } private void fileProcess(File file) { if (file.getName().endsWith(suffix)) { result.add(file.getAbsolutePath()); } } //只保留最近一天有过更改的文件 private void filterResultLatest() { List<String> newResult = new ArrayList<String>(); long actualDate = new Date().getTime(); for (int i = 0; i < result.size(); i++) { File file = new File(result.get(i)); long lastModifyTime = file.lastModified(); if (actualDate - lastModifyTime < TimeUnit.MICROSECONDS.convert(1, TimeUnit.DAYS)) { newResult.add(result.get(i)); } } result = newResult; } private boolean firstCheckResults() { if (result.isEmpty()) { System.out.println(Thread.currentThread().getName() + " 搜索完成 Phase = " + phaser.getPhase() + " , result = 0 , 取消注册"); phaser.arriveAndDeregister(); return false; } else { System.out.println(Thread.currentThread().getName() + " 搜索完成 Phase = " + phaser.getPhase() + " , result = " + result.size()); phaser.arriveAndAwaitAdvance(); return true; } } private boolean secondCheckResults() { if (result.isEmpty()) { System.out.println(Thread.currentThread().getName() + " 经过对修改时间过滤后 Phase = " + phaser.getPhase() + " , result = 0 , 取消注册"); phaser.arriveAndDeregister(); return false; } else { System.out.println(Thread.currentThread().getName() + " 经过对修改时间过滤后 Phase = " + phaser.getPhase() + " , result = " + result.size()); phaser.arriveAndAwaitAdvance(); return true; } } private void showInfo() { for (int i = 0; i < result.size(); i++) { System.out.println(Thread.currentThread().getName() + ":" + result.get(i)); } phaser.arriveAndAwaitAdvance(); } @Override public void run() { phaser.arriveAndAwaitAdvance(); System.out.println(Thread.currentThread().getName()+": starts"); File file=new File(path); //将满足后缀结尾的文件的绝对路径放到results中 if(file.isDirectory()){ directoryProcess(file); } //第一次检查,假如result为空,当前线程从phaser中注销 if(!firstCheckResults()){ return; } //对搜索的结果过滤,只保留在最近一天修改过的文件 filterResultLatest(); //有可能在上一步中,本来有10个文件,但是更改时间都是超过一天的,经过上一步后,result为空了 if(!secondCheckResults()){ return; } //打印文件列表 showInfo(); //取消注册 phaser.arriveAndDeregister(); } public static void main(String[] args) { Phaser phaser = new Phaser(3); Thread t1 = new Thread(new FileSearch("C:\Users\PHP\Desktop\abc\a", "txt", phaser), "【线程一】"); t1.start(); Thread t2 = new Thread(new FileSearch("C:\Users\PHP\Desktop\abc\b", "txt", phaser), "【线程二】"); t2.start(); Thread t3 = new Thread(new FileSearch("C:\Users\PHP\Desktop\abc\c", "txt", phaser), "【线程三】"); t3.start(); try { t1.join(); t2.join(); t3.join(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Terminated:" + phaser.isTerminated()); } }