这个程序做了两小时还是没成啊 :'( 需求就是一个多线程售票的程序
- public class Station {
- /**
- * @param args
- */
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new Thread(new SealWindow("一号")).start();
- new Thread(new SealWindow("二号")).start();
- new Thread(new SealWindow("三号")).start();
- }
- }
- //售票中心
- class TicketSealCenter{
- private Ticket t;//给票
-
- TicketSealCenter(Ticket t){
- this.t=t;
-
- }
- public synchronized void sellTicket(SealWindow sw){//这里为了安全 用到了关键词synchronized
- if(t.isEmpty()){
-
- Integer i = t.getNumber();//得到票数
- i--;//自减票
- String name = sw.getWindowName();
- System.out.println("第" + name+ "售票窗口卖出了第" + i+ "张票");
- }
- else{
-
- System.out.println("票已售完");
- }
-
- }
-
-
- }
- //票类
- class Ticket{
- private int ticketNumber=200; //定义票的数量
-
- public int getNumber() {
- return ticketNumber;
- }
- public void setNumber(int number) {//找到javaBean
- this.ticketNumber = number;
- }
- public boolean isEmpty(){//判断是否卖完
- if(ticketNumber>0)
- return true;
- else
- return false;
-
- }
-
- }
- //窗口类
- class SealWindow implements Runnable{
-
- private String windowName ;
- private TicketSealCenter center;
-
-
- public void setWindowName(String windowName) {
- this.windowName = windowName;
- }
- public String getWindowName() {
- return windowName;
- }
- SealWindow(String windowName){
-
- this.windowName=windowName;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(true){
- center.sellTicket(this);//卖票
- try {
- Thread.sleep(1000);//卖一次停1秒
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
-
- }
-
-
-
- }
复制代码 结果写完了 调试了半天还是没成 说是报空指针
- center.sellTicket(this);//卖票
复制代码
这个地方 结果我把int 改成了Integer 心想 就不报错了吧 没想到还这样 没办法了
大神帮忙给看看:'( 求指点
|