线程同步通信技术Communication 02

package cn.itcast.heima2;


public class TraditionalThreadCommunication {

public static void main(String[] args) {
final Business business=new Business();
new Thread(
new Runnable() {

@Override
public void run() {
for(int i=1;i<=50;i++){
business.sub(i);
/*synchronized (ThaditionalThreadSynchronized.class) {
for (int j = 0; j <= 10; j++) {
System.out.println("sub thread sequece of"+i+",loop of"+j);
}
}*/
}
}
}
   ).start();
//主线程
for(int i=1;i<=50;i++){
business.main(i);
/*synchronized (ThaditionalThreadSynchronized.class) {
for (int j = 0; j <= 10; j++) {
System.out.println("main thread sequece of"+i+",loop of"+j);
}
}*/
}



}



}


class Business{
private boolean bShouldSub = true;

public synchronized void sub(int i){
if(bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int j = 0; j <= 10; j++) {
System.out.println("sub thread sequece of"+i+",loop of"+j);
}
bShouldSub =false;
this.notify();
}
public synchronized void main(int i){
if(!bShouldSub){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int j = 0; j <= 10; j++) {
System.out.println("main thread sequece of"+i+",loop of"+j);
}
bShouldSub =true;
this.notify();
}

}


main thread sequece of1,loop of0
main thread sequece of1,loop of1
main thread sequece of1,loop of2
main thread sequece of1,loop of3
main thread sequece of1,loop of4
main thread sequece of1,loop of5
main thread sequece of1,loop of6
main thread sequece of1,loop of7
main thread sequece of1,loop of8
main thread sequece of1,loop of9
main thread sequece of1,loop of10
main thread sequece of2,loop of0
main thread sequece of2,loop of1
main thread sequece of2,loop of2
main thread sequece of2,loop of3
main thread sequece of2,loop of4
main thread sequece of2,loop of5
main thread sequece of2,loop of6
main thread sequece of2,loop of7
main thread sequece of2,loop of8
main thread sequece of2,loop of9
main thread sequece of2,loop of10
main thread sequece of3,loop of0
main thread sequece of3,loop of1
main thread sequece of3,loop of2
main thread sequece of3,loop of3
main thread sequece of3,loop of4
main thread sequece of3,loop of5
main thread sequece of3,loop of6
main thread sequece of3,loop of7
main thread sequece of3,loop of8
main thread sequece of3,loop of9
main thread sequece of3,loop of10
main thread sequece of4,loop of0
main thread sequece of4,loop of1
main thread sequece of4,loop of2
main thread sequece of4,loop of3
main thread sequece of4,loop of4
main thread sequece of4,loop of5
main thread sequece of4,loop of6
main thread sequece of4,loop of7
main thread sequece of4,loop of8
main thread sequece of4,loop of9
main thread sequece of4,loop of10
main thread sequece of5,loop of0
main thread sequece of5,loop of1
main thread sequece of5,loop of2
main thread sequece of5,loop of3
main thread sequece of5,loop of4
main thread sequece of5,loop of5
main thread sequece of5,loop of6
main thread sequece of5,loop of7
main thread sequece of5,loop of8
main thread sequece of5,loop of9
main thread sequece of5,loop of10
main thread sequece of6,loop of0
main thread sequece of6,loop of1
main thread sequece of6,loop of2
main thread sequece of6,loop of3
main thread sequece of6,loop of4
main thread sequece of6,loop of5
main thread sequece of6,loop of6
main thread sequece of6,loop of7
main thread sequece of6,loop of8
main thread sequece of6,loop of9
main thread sequece of6,loop of10
main thread sequece of7,loop of0
main thread sequece of7,loop of1
main thread sequece of7,loop of2
main thread sequece of7,loop of3
main thread sequece of7,loop of4
main thread sequece of7,loop of5
main thread sequece of7,loop of6
main thread sequece of7,loop of7
main thread sequece of7,loop of8
main thread sequece of7,loop of9
main thread sequece of7,loop of10
main thread sequece of8,loop of0
main thread sequece of8,loop of1
main thread sequece of8,loop of2
main thread sequece of8,loop of3
main thread sequece of8,loop of4
main thread sequece of8,loop of5
main thread sequece of8,loop of6
main thread sequece of8,loop of7
main thread sequece of8,loop of8
main thread sequece of8,loop of9
main thread sequece of8,loop of10
main thread sequece of9,loop of0
main thread sequece of9,loop of1
main thread sequece of9,loop of2
main thread sequece of9,loop of3
main thread sequece of9,loop of4
main thread sequece of9,loop of5
main thread sequece of9,loop of6
main thread sequece of9,loop of7
main thread sequece of9,loop of8
main thread sequece of9,loop of9
main thread sequece of9,loop of10
main thread sequece of10,loop of0
main thread sequece of10,loop of1
main thread sequece of10,loop of2
main thread sequece of10,loop of3
main thread sequece of10,loop of4
main thread sequece of10,loop of5
main thread sequece of10,loop of6
main thread sequece of10,loop of7
main thread sequece of10,loop of8
main thread sequece of10,loop of9
main thread sequece of10,loop of10
main thread sequece of11,loop of0
main thread sequece of11,loop of1
main thread sequece of11,loop of2
main thread sequece of11,loop of3
main thread sequece of11,loop of4
main thread sequece of11,loop of5
main thread sequece of11,loop of6
main thread sequece of11,loop of7
main thread sequece of11,loop of8
main thread sequece of11,loop of9
main thread sequece of11,loop of10
main thread sequece of12,loop of0
main thread sequece of12,loop of1
main thread sequece of12,loop of2
main thread sequece of12,loop of3
main thread sequece of12,loop of4
main thread sequece of12,loop of5
main thread sequece of12,loop of6
main thread sequece of12,loop of7
main thread sequece of12,loop of8
main thread sequece of12,loop of9
main thread sequece of12,loop of10
main thread sequece of13,loop of0
main thread sequece of13,loop of1
main thread sequece of13,loop of2
main thread sequece of13,loop of3
main thread sequece of13,loop of4
main thread sequece of13,loop of5
main thread sequece of13,loop of6
main thread sequece of13,loop of7
main thread sequece of13,loop of8
main thread sequece of13,loop of9
main thread sequece of13,loop of10
main thread sequece of14,loop of0
main thread sequece of14,loop of1
main thread sequece of14,loop of2
main thread sequece of14,loop of3
main thread sequece of14,loop of4
main thread sequece of14,loop of5
main thread sequece of14,loop of6
main thread sequece of14,loop of7
main thread sequece of14,loop of8
main thread sequece of14,loop of9
main thread sequece of14,loop of10
main thread sequece of15,loop of0
main thread sequece of15,loop of1
main thread sequece of15,loop of2
main thread sequece of15,loop of3
main thread sequece of15,loop of4
main thread sequece of15,loop of5
main thread sequece of15,loop of6
main thread sequece of15,loop of7
main thread sequece of15,loop of8
main thread sequece of15,loop of9
main thread sequece of15,loop of10
main thread sequece of16,loop of0
main thread sequece of16,loop of1
main thread sequece of16,loop of2
main thread sequece of16,loop of3
main thread sequece of16,loop of4
main thread sequece of16,loop of5
main thread sequece of16,loop of6
main thread sequece of16,loop of7
main thread sequece of16,loop of8
main thread sequece of16,loop of9
main thread sequece of16,loop of10
main thread sequece of17,loop of0
main thread sequece of17,loop of1
main thread sequece of17,loop of2
main thread sequece of17,loop of3
main thread sequece of17,loop of4
main thread sequece of17,loop of5
main thread sequece of17,loop of6
main thread sequece of17,loop of7
main thread sequece of17,loop of8
main thread sequece of17,loop of9
main thread sequece of17,loop of10
main thread sequece of18,loop of0
main thread sequece of18,loop of1
main thread sequece of18,loop of2
main thread sequece of18,loop of3
main thread sequece of18,loop of4
main thread sequece of18,loop of5
main thread sequece of18,loop of6
main thread sequece of18,loop of7
main thread sequece of18,loop of8
main thread sequece of18,loop of9
main thread sequece of18,loop of10
main thread sequece of19,loop of0
main thread sequece of19,loop of1
main thread sequece of19,loop of2
main thread sequece of19,loop of3
main thread sequece of19,loop of4
main thread sequece of19,loop of5
main thread sequece of19,loop of6
main thread sequece of19,loop of7
main thread sequece of19,loop of8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值