a simple program reveal thread synchronization

Here's a simple program but reveal thread synchronization mechanism:
- only one thread can enter critical region at a fix time
- two thread enter critical region one by one, and loop
- the time won't affect thread synchronization
- two thread work together like pass ball

[code]
package com.demo.thread;

public class DemoThread {
public static void main(String[] args) {
Object obj = new Object();

Working worker1 = new Working("worker-1", obj, 2000);
Working worker2 = new Working("worker-2", obj, 20);

worker1.start();
worker2.start();
}

}

class Working extends Thread {
private String mName;
private Object mObj;
private long mTime;

public Working(String name, Object obj, long time) {
mName = name;
mObj = obj;
mTime = time;
}

public void run() {
while(true) {
try {
System.out.println(mName + " is waiting");
Thread.sleep(mTime);
System.out.println(mName + " is trying to enter critical region");
synchronized(mObj) {
System.out.println(mName + " has entered into critical region");
System.out.println(mName + " will notify other thread");
mObj.notify();
System.out.println(mName + " will hang");
mObj.wait();
System.out.println(mName + " will quit critical region");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

[/code]

the output will like:
[code]
worker-1 is waiting
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
worker-1 will hang
worker-2 will quit critical region
worker-2 is waiting
worker-2 is trying to enter critical region
worker-2 has entered into critical region
worker-2 will notify other thread
worker-2 will hang
worker-1 will quit critical region
worker-1 is waiting
worker-1 is trying to enter critical region
worker-1 has entered into critical region
worker-1 will notify other thread
.....

[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值