aaaaaaaaaaa- package ticket.demo;
- public class SellDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- SharedTicket st = new SharedTicket();
- new Thread(st).start();
- new Thread(st).start();
- new Thread(st).start();
- }
- }
- class SharedTicket implements Runnable {
- private int ticket = 1000;
- @Override
- public void run() {
- while (true) {
- sell();
- // synchronized (this) {
- // if (ticket > 0) {
- // ticket--;
- // System.out.println(Thread.currentThread().getName()
- // + "...售出一张,剩余" + ticket);
- // } else
- // break;
- // }
- }
- }
- private synchronized void sell() {
- if (ticket > 0) {
- ticket--;
- System.out.println(Thread.currentThread().getName()
- + "...售出一张,剩余" + ticket);
- }
- else
- System.exit(0) ;
- }
- }
复制代码
|
|