本帖最后由 焚雨成灰 于 2014-3-20 10:18 编辑
- public class Test1{
- public static void main(String[] args) {
- new Thread(new TicketSeller()).start();
- new Thread(new TicketSeller()).start();
- new Thread(new TicketSeller()).start();
- new Thread(new TicketSeller()).start();
- }
- }
- class TicketSeller implements Runnable {
- private static int ticket = 100;
- Object obj = new Object();
- public void run() {
- while(ticket > 0) {
- synchronized(obj) {
- System.out.println(Thread.currentThread().getName()+" sell a ticket,\t"+"********"+ ticket--);
- }
- }
- }
- }
1.在17行,我用的是while(ticket>0),而毕老师是while(true) if(ticket>0) 这两个有什么区别?
2.在18行加了同步代码块,为什么打印出来的票数不是100往下步进1递减的
|