• 两个线程交替打印数字:1,2,...,100


    /**
     * @author:ypf
     * @createTime:2019/5/24
     * @desc:两个线程交替打印数字1,2,...,100
     */
    public class CommonData {
    
        private volatile static boolean flag = false;
    
        private volatile static int i = 1;
    
        private volatile static int k = 100;
    
        public void printThread1(){
            try{
                while (k > 0){
                    synchronized (this){
                        if(flag){
                            this.wait();
                        }else{
                            this.notify();
                            System.out.println(Thread.currentThread().getName() + "----------> i = " + i);
                            i ++;
                            flag = true;
                            k --;
                        }
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    
        public void printThread2(){
            try{
                while (k > 0){
                    synchronized (this){
                        if(!flag){
                            this.wait();
                        }else{
                            this.notify();
                            System.out.println(Thread.currentThread().getName() + "----------> i = " + i);
                            i = i + 1;
                            flag = false;
                            k --;
                        }
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    
    }
    /**
     * @author ypf
     * @createTime:2019/5/24
     * @desc:线程1
     */
    public class ThreadA extends Thread{
    
        private CommonData data;
    
        public ThreadA(CommonData data){
            this.data = data;
        }
    
        @Override
        public void run() {
            data.printThread1();
        }
    }
    /**
     * @author ypf
     * @createTime:2019/5/24
     * @desc:线程2
     */
    public class ThreadB extends Thread{
    
        private CommonData data;
    
        public ThreadB(CommonData data){
            this.data = data;
        }
    
        @Override
        public void run() {
            data.printThread2();
        }
    }
    /**
     * @author ypf
     * @createTime 2019/5/24
     * @desc 测试
     */
    public class Main {
        public static void main(String[] args) {
            CommonData data = new CommonData();
            ThreadA threadA = new ThreadA(data);
            ThreadB threadB = new ThreadB(data);
            threadA.start();
            threadB.start();
        }
    }
    "C:Program FilesJavajdk1.8.0_171injava" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61154,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_171jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_171jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_171jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_171jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_171jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_171jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_171jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_171jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_171jrelibext
    ashorn.jar;C:Program FilesJavajdk1.8.0_171jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_171jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_171jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_171jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_171jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_171jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_171jrelibjce.jar;C:Program FilesJavajdk1.8.0_171jrelibjfr.jar;C:Program FilesJavajdk1.8.0_171jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_171jrelibjsse.jar;C:Program FilesJavajdk1.8.0_171jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_171jrelibplugin.jar;C:Program FilesJavajdk1.8.0_171jrelib
    esources.jar;C:Program FilesJavajdk1.8.0_171jrelib
    t.jar;F:my-projectdemo	argetclasses;C:Userslihong.m2
    epositoryorgspringframeworkootspring-boot-starter2.0.6.RELEASEspring-boot-starter-2.0.6.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkootspring-boot2.0.6.RELEASEspring-boot-2.0.6.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-context5.0.10.RELEASEspring-context-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-aop5.0.10.RELEASEspring-aop-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-beans5.0.10.RELEASEspring-beans-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-expression5.0.10.RELEASEspring-expression-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkootspring-boot-autoconfigure2.0.6.RELEASEspring-boot-autoconfigure-2.0.6.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkootspring-boot-starter-logging2.0.6.RELEASEspring-boot-starter-logging-2.0.6.RELEASE.jar;C:Userslihong.m2
    epositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:Userslihong.m2
    epositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:Userslihong.m2
    epositoryorgapachelogginglog4jlog4j-to-slf4j2.10.0log4j-to-slf4j-2.10.0.jar;C:Userslihong.m2
    epositoryorgapachelogginglog4jlog4j-api2.10.0log4j-api-2.10.0.jar;C:Userslihong.m2
    epositoryorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;C:Userslihong.m2
    epositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-core5.0.10.RELEASEspring-core-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgspringframeworkspring-jcl5.0.10.RELEASEspring-jcl-5.0.10.RELEASE.jar;C:Userslihong.m2
    epositoryorgyamlsnakeyaml1.19snakeyaml-1.19.jar;C:Userslihong.m2
    epositoryorgspringframeworkootspring-boot-devtools2.0.6.RELEASEspring-boot-devtools-2.0.6.RELEASE.jar;C:Userslihong.m2
    epositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;D:IntelliJ-IDEA-2017IntelliJ IDEA 2017.1.5libidea_rt.jar" com.example.t1.Main
    Connected to the target VM, address: '127.0.0.1:61154', transport: 'socket'
    Thread-0----------> i = 1
    Thread-1----------> i = 2
    Thread-0----------> i = 3
    Thread-1----------> i = 4
    Thread-0----------> i = 5
    Thread-1----------> i = 6
    Thread-0----------> i = 7
    Thread-1----------> i = 8
    Thread-0----------> i = 9
    Thread-1----------> i = 10
    Thread-0----------> i = 11
    Thread-1----------> i = 12
    Thread-0----------> i = 13
    Thread-1----------> i = 14
    Thread-0----------> i = 15
    Thread-1----------> i = 16
    Thread-0----------> i = 17
    Thread-1----------> i = 18
    Thread-0----------> i = 19
    Thread-1----------> i = 20
    Thread-0----------> i = 21
    Thread-1----------> i = 22
    Thread-0----------> i = 23
    Thread-1----------> i = 24
    Thread-0----------> i = 25
    Thread-1----------> i = 26
    Thread-0----------> i = 27
    Thread-1----------> i = 28
    Thread-0----------> i = 29
    Thread-1----------> i = 30
    Thread-0----------> i = 31
    Thread-1----------> i = 32
    Thread-0----------> i = 33
    Thread-1----------> i = 34
    Thread-0----------> i = 35
    Thread-1----------> i = 36
    Thread-0----------> i = 37
    Thread-1----------> i = 38
    Thread-0----------> i = 39
    Thread-1----------> i = 40
    Thread-0----------> i = 41
    Thread-1----------> i = 42
    Thread-0----------> i = 43
    Thread-1----------> i = 44
    Thread-0----------> i = 45
    Thread-1----------> i = 46
    Thread-0----------> i = 47
    Thread-1----------> i = 48
    Thread-0----------> i = 49
    Thread-1----------> i = 50
    Thread-0----------> i = 51
    Thread-1----------> i = 52
    Thread-0----------> i = 53
    Thread-1----------> i = 54
    Thread-0----------> i = 55
    Thread-1----------> i = 56
    Thread-0----------> i = 57
    Thread-1----------> i = 58
    Thread-0----------> i = 59
    Thread-1----------> i = 60
    Thread-0----------> i = 61
    Thread-1----------> i = 62
    Thread-0----------> i = 63
    Thread-1----------> i = 64
    Thread-0----------> i = 65
    Thread-1----------> i = 66
    Thread-0----------> i = 67
    Thread-1----------> i = 68
    Thread-0----------> i = 69
    Thread-1----------> i = 70
    Thread-0----------> i = 71
    Thread-1----------> i = 72
    Thread-0----------> i = 73
    Thread-1----------> i = 74
    Thread-0----------> i = 75
    Thread-1----------> i = 76
    Thread-0----------> i = 77
    Thread-1----------> i = 78
    Thread-0----------> i = 79
    Thread-1----------> i = 80
    Thread-0----------> i = 81
    Thread-1----------> i = 82
    Thread-0----------> i = 83
    Thread-1----------> i = 84
    Thread-0----------> i = 85
    Thread-1----------> i = 86
    Thread-0----------> i = 87
    Thread-1----------> i = 88
    Thread-0----------> i = 89
    Thread-1----------> i = 90
    Thread-0----------> i = 91
    Thread-1----------> i = 92
    Thread-0----------> i = 93
    Thread-1----------> i = 94
    Thread-0----------> i = 95
    Thread-1----------> i = 96
    Thread-0----------> i = 97
    Thread-1----------> i = 98
    Thread-0----------> i = 99
    Thread-1----------> i = 100
    Disconnected from the target VM, address: '127.0.0.1:61154', transport: 'socket'
    
  • 相关阅读:
    Eclipse运行单个Junit 单元测试
    梯形法求定积分(一)设计梯形法求积分的类模板,梯形法
    写一个顺序表模板,包括顺序表的基本功能,例如查找,插
    对于静态成员函数和静态成员变量的练习
    梯形法求定积分(二)设计一个函数模板,独立的非成员函
    各位高手帮我看看这个清屏程序
    对于静态成员函数和静态成员变量的练习
    二分法的递归算法和迭代算法,算法作为有序表模板类的成
    梯形法求定积分(一)设计梯形法求积分的类模板,梯形法
    二分法的递归算法和迭代算法,算法作为有序表模板类的成
  • 原文地址:https://www.cnblogs.com/myxcf/p/10916923.html
Copyright © 2020-2023  润新知