两个线程交替打印数字:1|两个线程交替打印数字: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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/new CommonData(); ThreadA threadA = new ThreadA(data); ThreadB threadB = new ThreadB(data); threadA.start(); threadB.start(); } }

"C:\Program Files\Java\jdk1.8.0_171\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61154,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar; C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar; F:\my-project\demo\target\classes; C:\Users\lihong\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.6.RELEASE\spring-boot-starter-2.0.6.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\boot\spring-boot\2.0.6.RELEASE\spring-boot-2.0.6.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-context\5.0.10.RELEASE\spring-context-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-aop\5.0.10.RELEASE\spring-aop-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-beans\5.0.10.RELEASE\spring-beans-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-expression\5.0.10.RELEASE\spring-expression-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.6.RELEASE\spring-boot-autoconfigure-2.0.6.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.6.RELEASE\spring-boot-starter-logging-2.0.6.RELEASE.jar; C:\Users\lihong\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar; C:\Users\lihong\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar; C:\Users\lihong\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar; C:\Users\lihong\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar; C:\Users\lihong\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar; C:\Users\lihong\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-core\5.0.10.RELEASE\spring-core-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\springframework\spring-jcl\5.0.10.RELEASE\spring-jcl-5.0.10.RELEASE.jar; C:\Users\lihong\.m2\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar; C:\Users\lihong\.m2\repository\org\springframework\boot\spring-boot-devtools\2.0.6.RELEASE\spring-boot-devtools-2.0.6.RELEASE.jar; C:\Users\lihong\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar; D:\IntelliJ-IDEA-2017\IntelliJ IDEA 2017.1.5\lib\idea_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'

【两个线程交替打印数字:1|两个线程交替打印数字:1,2,...,100】
转载于:https://www.cnblogs.com/myxcf/p/10916923.html

    推荐阅读