zoukankan      html  css  js  c++  java
  • 多线程学习——终结任务

    package concurrency;
    
    //: concurrency/OrnamentalGarden.java
    import static net.mindview.util.Print.print;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    class Count {
    	private int count = 0;
    	private Random rand = new Random(47);
    
    	// Remove the synchronized keyword to see counting fail:
    	public synchronized int increment() {
    		int temp = count;
    		if (rand.nextBoolean()) // Yield half the time
    			Thread.yield();
    		return (count = ++temp);
    	}
    
    	public synchronized int value() {
    		return count;
    	}
    }
    
    class Entrance implements Runnable {
    	private static Count count = new Count();
    	private static List<Entrance> entrances = new ArrayList<Entrance>();
    	private int number = 0;
    	// Doesn't need synchronization to read:
    	private final int id;
    	private static volatile boolean canceled = false;
    
    	// Atomic operation on a volatile field:
    	public static void cancel() {
    		canceled = true;
    	}
    
    	public Entrance(int id) {
    		this.id = id;
    		// Keep this task in a list. Also prevents
    		// garbage collection of dead tasks:
    		entrances.add(this);
    	}
    
    	public void run() {
    		while (!canceled) {
    			synchronized (this) {
    				++number;
    			}
    			print(this + " Total: " + count.increment());
    			try {
    				TimeUnit.MILLISECONDS.sleep(100);
    			} catch (InterruptedException e) {
    				print("sleep interrupted");
    			}
    		}
    		print("Stopping " + this);
    	}
    
    	public synchronized int getValue() {
    		return number;
    	}
    
    	public String toString() {
    		return "Entrance " + id + ": " + getValue();
    	}
    
    	public static int getTotalCount() {
    		return count.value();
    	}
    
    	public static int sumEntrances() {
    		int sum = 0;
    		for (Entrance entrance : entrances)
    			sum += entrance.getValue();
    		return sum;
    	}
    }
    
    public class OrnamentalGarden {
    	public static void main(String[] args) throws Exception {
    		ExecutorService exec = Executors.newCachedThreadPool();
    		for (int i = 0; i < 5; i++)
    			exec.execute(new Entrance(i));
    		// Run for a while, then stop and collect the data:
    		TimeUnit.SECONDS.sleep(3);
    		Entrance.cancel();
    		exec.shutdown();
    		if (!exec.awaitTermination(250, TimeUnit.MILLISECONDS))
    			print("Some tasks were not terminated!");
    		print("Total: " + Entrance.getTotalCount());
    		print("Sum of Entrances: " + Entrance.sumEntrances());
    	}
    } /*
     * Output: (Sample) Entrance 0: 1 Total: 1 Entrance 2: 1 Total: 3 Entrance 1: 1
     * Total: 2 Entrance 4: 1 Total: 5 Entrance 3: 1 Total: 4 Entrance 2: 2 Total: 6
     * Entrance 4: 2 Total: 7 Entrance 0: 2 Total: 8 ... Entrance 3: 29 Total: 143
     * Entrance 0: 29 Total: 144 Entrance 4: 29 Total: 145 Entrance 2: 30 Total: 147
     * Entrance 1: 30 Total: 146 Entrance 0: 30 Total: 149 Entrance 3: 30 Total: 148
     * Entrance 4: 30 Total: 150 Stopping Entrance 2: 30 Stopping Entrance 1: 30
     * Stopping Entrance 0: 30 Stopping Entrance 3: 30 Stopping Entrance 4: 30
     * Total: 150 Sum of Entrances: 150
     */// :~
    

  • 相关阅读:
    Unity3D在各平台上的路径
    Unity简单的单例模式
    C#遍历枚举(Enum)
    C#常用的流类型(FileStream,SteamWriter/StreamReader,MemoryStream等)
    编写一个C程序,运行时输入a,b,c三个值,输出其中最大者
    精确一维搜索算法(直接法)
    Java一维数组求和
    java 导出EXCEL
    Java判断字符串的数字类型(小数、整数)
    网址存储
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959189.html
Copyright © 2011-2022 走看看