本帖最后由 黑马王冬冬 于 2012-8-18 13:25 编辑
- public void start(){
- pool.scheduleAtFixedRate(
- new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- if(tic.getTicNum() <= 0){
- System.out.println("第" + windowNum + "号窗口" + "门票售罄!");
- pool.shutdownNow();
- }
- else if(((num = cus.serveCustomer()) == -1) && (tic.getTicNum() > 0)){
- System.out.println("第" + windowNum + "号窗口" + "暂无顾客,等待中");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else if((tic.getTicNum() > 0)){
- synchronized (tic) {
- System.out.println("第" + windowNum + "号窗口" + "销售一张门票给第" + num + "位顾客");
- tic.setTicNum(tic.getTicNum() - 1);//位置1
- }
- }
- }
- },
- 2,
- 1,
- TimeUnit.MILLISECONDS);
- }
复制代码 同学们,位置1处如何进行有效的同步?加同步代码块以后仍然很神奇的没有起作用,我记得毕老师曾经讲到过,哪位提示一下。
|