- private int ticket = 100;
- @Override
- public void run() {
- while (true) {
- synchronized ("lock") {
- if (ticket > 0) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName()
- + "正在卖剩余的最后" + ticket-- + "张票");
- }
- }
- }
复制代码
在模拟售票窗口时 加了sleep和不加sleep结果有不小的差异
但是sleep也是相对时间一起执行,为何会有差异 |
|