zoukankan      html  css  js  c++  java
  • Java控制多线程执行顺序

    package net.jasonjiang.thread;
    
    import java.io.IOException;  
      
    public class ThreadTestNew {  
      
        public static void main(String[] args) throws IOException {  
            final Test obj = new Test();  
      
            new Thread() {  
                public void run() {  
                    obj.m1();  
                }  
            }.start();  
            new Thread() {  
                public void run() {  
                    obj.m2();  
                }  
            }.start();  
            new Thread() {  
                public void run() {  
                    obj.m3();  
                }  
            }.start();  
      
        }  
      
    }  
      
    class Test {  
        volatile int target = 1;  
      
        public synchronized void m1() {  
            for (int i = 0; i < 10; i++) {  
                while (target == 2 || target == 3) {  
                    try {  
                        wait();  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
                System.out.println("m1() =" + i);  
                target = 2;  
                notifyAll();  
            }  
        }  
      
        public synchronized void m2() {  
            for (int i = 0; i < 10; i++) {  
                while (target == 1 || target == 3) {  
                    try {  
                        wait();  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
                System.out.println("m2() =" + i);  
                target = 3;  
                notifyAll();  
            }  
        }  
      
        public synchronized void m3() {  
            for (int i = 0; i < 10; i++) {  
                while (target == 1 || target == 2) {  
                    try {  
                        wait();  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
                System.out.println("m3() =" + i);  
                target = 1;  
                notifyAll();  
            }  
        }  
    }

    运行结果:

    m1() =0
    m2() =0
    m3() =0
    m1() =1
    m2() =1
    m3() =1
    m1() =2
    m2() =2
    m3() =2
    m1() =3
    m2() =3
    m3() =3
    m1() =4
    m2() =4
    m3() =4
    m1() =5
    m2() =5
    m3() =5
    m1() =6
    m2() =6
    m3() =6
    m1() =7
    m2() =7
    m3() =7
    m1() =8
    m2() =8
    m3() =8
    m1() =9
    m2() =9
    m3() =9


  • 相关阅读:
    机器学习之线性回归
    机器学*-K*邻算法模型预测实战
    机器学习-特征抽取
    机器学习sklearn和字典特征抽取
    机器学习第一讲
    spark统计每个省份广告点击量top3
    spark统计单位时间内访问量
    并发容器之 ConcurrentHashMap源码剖析
    并发之 Java中的锁
    并发之 深入剖析volatile底层原理
  • 原文地址:https://www.cnblogs.com/jasontec/p/9601680.html
Copyright © 2011-2022 走看看