zoukankan      html  css  js  c++  java
  • Java Phaser

    //Listing 6-5. Using a Phaser to Control a One-Shot Action Serving a Variable Number
    //of Parties
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Phaser;
    
    public class A {
        public static void main(String[] args)
    {
        List<Runnable> tasks = new ArrayList<>();
        
        tasks.add(() -> System.out.printf("%s running at %d%n",
        Thread.currentThread().getName(),
        System.currentTimeMillis()));
        
        tasks.add(() -> System.out.printf("%s running at %d%n",
        Thread.currentThread().getName(),
        System.currentTimeMillis()));
        runTasks(tasks);
    }
    
        static void runTasks(List<Runnable> tasks)
        {
            final Phaser phaser = new Phaser(1); // "1" (register self)
            // create and start threads
            for (final Runnable task: tasks)
            {
            phaser.register();
            Runnable r = () ->
            {
                try
                {
                    Thread.sleep(50 + (int) (Math.random() * 300));
                }
                catch (InterruptedException ie)
                {
                    System.out.println("interrupted thread");
                }
                phaser.arriveAndAwaitAdvance(); // await the ...
                // creation of ...
                // all tasks
                task.run();
            };
            
            Executors.newSingleThreadExecutor().execute(r);
            }
            // allow threads to start and deregister self
            phaser.arriveAndDeregister();
    }
    }
  • 相关阅读:
    ASP.net AJAX Toolkit —— DynamicPopulateExtender
    查找存储过程修改时间
    Application_Start和Application_End事件执行时间
    制作导航菜单
    multiple backgrounds
    background-size
    background-clip
    background-origin
    文本阴影text-shadow
    嵌入字体@font-face
  • 原文地址:https://www.cnblogs.com/rojas/p/5377261.html
Copyright © 2011-2022 走看看