package com.wsy.model; /** * 完毕主线程循环10次,接着子线程循环5次,如此往复10次 * * @author wsy * */ public class TraditionalThreadCommunication { static Bussiness bussiness = new Bussiness(); public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { bussiness.sub(i); } } }).start(); for (int i = 1; i <= 10; i++) { bussiness.main(i); } } } class Bussiness { static boolean isThreadSub = true; public synchronized void sub(int i) { while (!isThreadSub) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 10; j++) { System.out.println("sub: " + j + " of loop " + i); } isThreadSub = false; this.notify(); } public synchronized void main(int i) { while (isThreadSub) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 5; j++) { System.out.println("main: " + j + " of loop " + i); } isThreadSub = true; this.notify(); } }
输出结果:
sub: 1 of loop 1
sub: 2 of loop 1
sub: 3 of loop 1
sub: 4 of loop 1
sub: 5 of loop 1
sub: 6 of loop 1
sub: 7 of loop 1
sub: 8 of loop 1
sub: 9 of loop 1
sub: 10 of loop 1
main: 1 of loop 1
main: 2 of loop 1
main: 3 of loop 1
main: 4 of loop 1
main: 5 of loop 1
sub: 1 of loop 2
sub: 2 of loop 2
sub: 3 of loop 2
sub: 4 of loop 2
sub: 5 of loop 2
sub: 6 of loop 2
sub: 7 of loop 2
sub: 8 of loop 2
sub: 9 of loop 2
sub: 10 of loop 2
main: 1 of loop 2
main: 2 of loop 2
main: 3 of loop 2
main: 4 of loop 2
main: 5 of loop 2
sub: 1 of loop 3
sub: 2 of loop 3
sub: 3 of loop 3
sub: 4 of loop 3
sub: 5 of loop 3
sub: 6 of loop 3
sub: 7 of loop 3
sub: 8 of loop 3
sub: 9 of loop 3
sub: 10 of loop 3
main: 1 of loop 3
main: 2 of loop 3
main: 3 of loop 3
main: 4 of loop 3
main: 5 of loop 3
sub: 1 of loop 4
sub: 2 of loop 4
sub: 3 of loop 4
sub: 4 of loop 4
sub: 5 of loop 4
sub: 6 of loop 4
sub: 7 of loop 4
sub: 8 of loop 4
sub: 9 of loop 4
sub: 10 of loop 4
main: 1 of loop 4
main: 2 of loop 4
main: 3 of loop 4
main: 4 of loop 4
main: 5 of loop 4
sub: 1 of loop 5
sub: 2 of loop 5
sub: 3 of loop 5
sub: 4 of loop 5
sub: 5 of loop 5
sub: 6 of loop 5
sub: 7 of loop 5
sub: 8 of loop 5
sub: 9 of loop 5
sub: 10 of loop 5
main: 1 of loop 5
main: 2 of loop 5
main: 3 of loop 5
main: 4 of loop 5
main: 5 of loop 5
sub: 1 of loop 6
sub: 2 of loop 6
sub: 3 of loop 6
sub: 4 of loop 6
sub: 5 of loop 6
sub: 6 of loop 6
sub: 7 of loop 6
sub: 8 of loop 6
sub: 9 of loop 6
sub: 10 of loop 6
main: 1 of loop 6
main: 2 of loop 6
main: 3 of loop 6
main: 4 of loop 6
main: 5 of loop 6
sub: 1 of loop 7
sub: 2 of loop 7
sub: 3 of loop 7
sub: 4 of loop 7
sub: 5 of loop 7
sub: 6 of loop 7
sub: 7 of loop 7
sub: 8 of loop 7
sub: 9 of loop 7
sub: 10 of loop 7
main: 1 of loop 7
main: 2 of loop 7
main: 3 of loop 7
main: 4 of loop 7
main: 5 of loop 7
sub: 1 of loop 8
sub: 2 of loop 8
sub: 3 of loop 8
sub: 4 of loop 8
sub: 5 of loop 8
sub: 6 of loop 8
sub: 7 of loop 8
sub: 8 of loop 8
sub: 9 of loop 8
sub: 10 of loop 8
main: 1 of loop 8
main: 2 of loop 8
main: 3 of loop 8
main: 4 of loop 8
main: 5 of loop 8
sub: 1 of loop 9
sub: 2 of loop 9
sub: 3 of loop 9
sub: 4 of loop 9
sub: 5 of loop 9
sub: 6 of loop 9
sub: 7 of loop 9
sub: 8 of loop 9
sub: 9 of loop 9
sub: 10 of loop 9
main: 1 of loop 9
main: 2 of loop 9
main: 3 of loop 9
main: 4 of loop 9
main: 5 of loop 9
sub: 1 of loop 10
sub: 2 of loop 10
sub: 3 of loop 10
sub: 4 of loop 10
sub: 5 of loop 10
sub: 6 of loop 10
sub: 7 of loop 10
sub: 8 of loop 10
sub: 9 of loop 10
sub: 10 of loop 10
main: 1 of loop 10
main: 2 of loop 10
main: 3 of loop 10
main: 4 of loop 10
main: 5 of loop 10
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119273.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...