本帖最后由 cat73 于 2016-7-9 13:22 编辑
随便写写的 0.0
- public final class Main {
- public static void main(final String args[]) throws Exception {
- new Main()._main(args);
- }
- private void _main(final String args[]) throws Exception {
- final Int num = new Int();
- new Thread(new IincThread(num, 1, 20, System.out), "+1 Thread 1").start();
- new Thread(new IincThread(num, 1, 20, System.out), "+1 Thread 2").start();
- new Thread(new IincThread(num, -1, 20, System.out), "-1 Thread 1").start();
- new Thread(new IincThread(num, -1, 20, System.out), "-1 Thread 2").start();
- }
- /**
- * 存储一个可变整数的类
- *
- * @author cat73
- */
- class Int {
- public int num = 0;
- }
- /**
- * 对数字执行加减操作的线程
- *
- * @author cat73
- */
- class IincThread implements Runnable {
- private final Int num;
- private final int n;
- private int count;
- private final PrintStream out;
- /**
- * 实例化一个工作线程
- * @param num 要被加减的数字的包装类
- * @param n 每次加减的量
- * @param count 加减的次数
- * @param out 输出详细信息的输出流,如不需要输出可设置为 null
- */
- public IincThread(final Int num, final int n, final int count, final PrintStream out) {
- this.num = num;
- this.n = n;
- this.count = count;
- this.out = out;
- }
- @Override
- public void run() {
- int numBefor, numAfter;
- while (this.count-- > 0) {
- synchronized (this.num) {
- numBefor = this.num.num;
-
- this.num.num += this.n;
-
- numAfter = this.num.num;
-
- if(this.out != null) {
- this.out.println(Thread.currentThread().getName() + ": " + numBefor + " -> " + numAfter);
- }
- }
-
- // 执行完一次后等一会再执行下一次
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- }
- }
- }
- }
- }
复制代码
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 1: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- -1 Thread 2: -1 -> -2
- +1 Thread 1: -2 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 2: 0 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- -1 Thread 2: -1 -> -2
- +1 Thread 2: -2 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 2: 0 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- -1 Thread 2: -1 -> -2
- +1 Thread 2: -2 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- -1 Thread 2: -1 -> -2
- +1 Thread 2: -2 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 2: 0 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- -1 Thread 2: -1 -> -2
- +1 Thread 1: -2 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- -1 Thread 2: 0 -> -1
- +1 Thread 2: -1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
复制代码
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 2: -1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 1: 0 -> 1
- +1 Thread 2: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 1: 0 -> 1
- +1 Thread 2: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 1: 0 -> 1
- +1 Thread 2: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 1: 2 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- +1 Thread 1: 1 -> 2
- -1 Thread 2: 2 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- +1 Thread 1: 0 -> 1
- -1 Thread 1: 1 -> 0
- +1 Thread 2: 0 -> 1
- -1 Thread 2: 1 -> 0
- -1 Thread 1: 0 -> -1
- +1 Thread 1: -1 -> 0
复制代码 |