• Java并发之CyclicBarria的使用(二)


    Java并发之CyclicBarria的使用(二)

    一.简介

      之前借助于其他大神写过一篇关于CyclicBarria用法的博文,但是内心总是感觉丝丝的愧疚,因为笔者喜欢原创,而不喜欢去转载一些其他的文章,为此笔者自己原创了一个CyclicBarria的用法的示例Demo, 在此声明,该Demo没有实际的价值,仅仅只是演示CyclicBarria的用法,希望加深读者对"循环栅栏"的用法加深理解。

    二.使用

      需求假设:在D盘下有一个test文件夹,我们要使用两个线程将文件夹A, B, 拷贝到test目录下,必须要等到两个文件夹都拷贝完毕,然后再将其删除。

    public class CyclicBarriaTest {
        
        static class ProcessDir extends Thread{
            
            private CyclicBarrier cb;
            
            private CountDownLatch cdl;
            
            private String path;
            
            private int num;
            
            public ProcessDir(String path, CyclicBarrier cb, int num, CountDownLatch cdl){
                this.num = num;
                this.cb = cb;
                this.path = path;
                this.cdl = cdl;
            }
            
            @Override
            public void run() {
                try {
                    cb.await();
                    copyDir();
                    cb.await();
                    deleteDir();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    cdl.countDown();
                } 
            }
            
            /**
             * 拷贝文件夹
             * @throws IOException
             */
            public void copyDir() throws IOException{
                if(num == 1){
                    FileUtils.copyDirectory(new File("D:/eclipse"), new File(path));
                }
                
                if(num == 2){
                    FileUtils.copyDirectory(new File("D:/maven"), new File(path));
                }
            }
            
            /**
             * 删除文件夹
             * @throws IOException
             */
            public void deleteDir() throws IOException{
                if(num == 1){
                    FileUtils.deleteDirectory(new File("d:/test/eclipse"));
                }
                
                if(num == 2){
                    FileUtils.deleteDirectory(new File("d:/test/maven"));
                }
            }
        }
        
        static class ShowInfo implements Runnable{
            
            private boolean flag;
            
            public ShowInfo(boolean flag){
                this.flag = flag;
            }
            
            @Override
            public void run() {
                if(flag){
                    System.out.println("所有的线程已经 准备完毕,开始执行拷贝");
                    flag = false;
                }else{
                    System.out.println("数据拷贝完毕,开始执行删除");
                }
            }
        }
        
        public static void main(String[] args) throws InterruptedException {
            CyclicBarrier cb = new CyclicBarrier(2, new ShowInfo(true));
            CountDownLatch cdl = new CountDownLatch(2);
            
            for(int i = 1; i <= 2; i++){
                new ProcessDir("d:/test", cb, i, cdl).start();
            }
            
            cdl.await();
            
            File file = new File("d:/test");
            file.delete();
        }
    }
  • 相关阅读:
    POJ2454 Jersey Politics
    Codeforces 798D
    BZOJ4556 HEOI2016 字符串
    BZOJ1009 [HNOI2008]GT考试
    POJ3693 Maximum repetition substring
    POJ1226 Substrings
    POJ3450 Corporate Identity
    POJ3415 Common Substrings
    CSS Sprites(CSS精灵) 的优缺点
    window.location.replace
  • 原文地址:https://www.cnblogs.com/miller-zou/p/6954265.html
Copyright © 2020-2023  润新知