本帖最后由 刘文飞 于 2012-11-15 14:19 编辑
- public class ThreadSyncDemo01 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Runnable run = new TicketThread();
- Thread t1 = new Thread(run);
- Thread t2 = new Thread(run);
- t1.start();
- t2.start();
- }
- }
- class Ticket{
- private int amount = 100;
- public void sell(){
- amount--;
- }
- public int getAmount(){
- return this.amount;
- }
- }
- class TicketThread implements Runnable{
- private Ticket T;
- public void run(){
- while(T.getAmount()>0){
- T.sell();
- System.out.println("售出一张票! " + T.getAmount());
- }
- System.out.println("票已经卖完了!");
- }
- }
复制代码 G:\hezi\Code>java ThreadSyncDemo01
Exception in thread "Thread-0" java.lang.NullPointerException
at TicketThread.run(ThreadSyncDemo01.java:30)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-1" java.lang.NullPointerException
at TicketThread.run(ThreadSyncDemo01.java:30)
at java.lang.Thread.run(Unknown Source)
|