java吧 关注:1,258,786贴子:12,754,671
  • 14回复贴,共1

求助!麻烦在线的同志们帮帮忙~(加急)

只看楼主收藏回复

问题:设计一个多线程程序,要求如下:
设计一个火车票售票模拟程序。假如火车站有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟5个售票点的售票情况。。
1,要求打印出每个售票点所卖出的票号。
2,各售票点不能售出相同票号的火车票。


IP属地:广东1楼2010-05-06 21:52回复
    写出程序和执行结果。在线的朋友,会的帮帮忙。


    IP属地:广东2楼2010-05-06 21:54
    回复
      2025-05-10 08:57:56
      广告
      有没有人在呀?


      IP属地:广东3楼2010-05-06 21:56
      回复
        有人没人哪?


        4楼2010-05-06 22:01
        回复
          悲剧,发的代码居然要审核!?.......!@#$%^&


          5楼2010-05-06 22:32
          回复
            还是要审核...放弃了...


            6楼2010-05-06 22:35
            回复
              public class Ti_cket {
              public static void main(String[] args) {
              Se_ll1 sell1 = new Se_ll1();
              new cust_om1(sell1, 1).start();
              new cust_om1(sell1, 2).start();
              new cust_om1(sell1, 3).start();
              new cust_om1(sell1, 4).start();
              new cust_om1(sell1, 5).start();
              }
              }
              class Se_ll1 {
              int ticket_count = 100;
              boolean b = false;
              public int se_ll1(int n) {
              if(ticket_count>0)
              {
              System.out.println(n + " 号售票站出(死百度死百度死百度死百度)售" + ticket_count+"号车票");
              ticket_count--;
              }
              return ticket_count;
              }
              }
              class cust_om1 extends Thread {
              Se_ll1 se_ll1;
              int n;
              cust_om1(Se_ll1 se_ll1, int n) {
              this.se_ll1 = se_ll1;
              this.n = n;
              }
              public void run() {
              while (this.se_ll1.se_ll1(n) > 0) {
              try {
              sleep((int) (Math.random() * 1000));
              } catch (Exception e) {
              System.out.println(e);
              }
              se_ll1.se_ll1(n);
              }
              }
              }


              7楼2010-05-06 22:44
              回复
                终于发出来了,不容易啊,也不知道是不是你要的那种...


                8楼2010-05-06 22:45
                回复
                  2025-05-10 08:51:56
                  广告
                  public class Ti_cket {
                  public static void main(String[] args) {
                  Se_ll1 sell1 = new Se_ll1();
                  new cust_om1(sell1, 1).start();
                  new cust_om1(sell1, 2).start();
                  new cust_om1(sell1, 3).start();
                  new cust_om1(sell1, 4).start();
                  new cust_om1(sell1, 5).start();
                  }
                  }
                  class Se_ll1 {
                  int ticket_count = 100;
                  boolean b = false;
                  public int se_ll1(int n) {
                  if(ticket_count>0)
                  {
                  System.out.println(n + " 号售票站出售" + ticket_count+"号车票");
                  ticket_count--;
                  }
                  return ticket_count;
                  }
                  }
                  class cust_om1 extends Thread {
                  Se_ll1 se_ll1;
                  int n;
                  cust_om1(Se_ll1 se_ll1, int n) {
                  this.se_ll1 = se_ll1;
                  this.n = n;
                  }
                  public void run() {
                  while (this.se_ll1.se_ll1(n) > 0) {
                  try {
                  sleep((int) (Math.random() * 1000));
                  } catch (Exception e) {
                  System.out.println(e);
                  }
                  se_ll1.se_ll1(n);
                  }
                  }
                  }


                  9楼2010-05-06 23:23
                  回复
                    public class Ticket {
                    public static void main(String[] args) {
                    Sell1 sell1 = new Sell1();
                    new custom1(sell1, 1).start();
                    new custom1(sell1, 2).start();
                    new custom1(sell1, 3).start();
                    new custom1(sell1, 4).start();
                    new custom1(sell1, 5).start();
                    }
                    }
                    class Sell1 {
                    int ticket_count = 100;
                    boolean b = false;
                    public int sell1(int n) {
                    if(ticket_count>0)
                    {
                    System.out.println(n + " 号售票站出售" + ticket_count+"号车票");
                    ticket_count--;
                    }
                    return ticket_count;
                    }
                    }
                    class custom1 extends Thread {
                    Sell1 sell1;
                    int n;
                    custom1(Sell1 sell1, int n) {
                    this.sell1 = sell1;
                    this.n = n;
                    }
                    Ticket ticket;
                    public void run() {
                    while (this.sell1.sell1(n) > 0) {
                    try {
                    sleep((int) (Math.random() * 1000));
                    } catch (Exception e) {
                    System.out.println(e);
                    }
                    sell1.sell1(n);
                    }
                    }
                    }


                    10楼2010-05-06 23:23
                    回复
                      public class Ticket {
                      public static void main(String[] args) {
                      Sell1 sell1 = new Sell1();
                      new custom(sell1, 1).start();
                      new custom(sell1, 2).start();
                      new custom(sell1, 3).start();
                      new custom(sell1, 4).start();
                      new custom(sell1, 5).start();
                      }
                      }
                      class Sell1 {
                      int ticket_count = 100;
                      boolean b = false;
                      public int sell1(int n) {
                      if(ticket_count>0)
                      {
                      System.out.println(n + " 号售票站出售" + ticket_count+"号车票");
                      ticket_count--;
                      }
                      return ticket_count;
                      }
                      }
                      class custom extends Thread {
                      Sell1 sell1;
                      int n;
                      custom(Sell1 sell1, int n) {
                      this.sell1 = sell1;
                      this.n = n;
                      }
                      Ticket ticket;
                      public void run() {
                      while (this.sell1.sell1(n) > 0) {
                      try {
                      sleep((int) (Math.random() * 1000));
                      } catch (Exception e) {
                      System.out.println(e);
                      }
                      sell1.sell1(n);
                      }
                      }
                      }


                      11楼2010-05-06 23:27
                      回复
                        public class Ticket {
                        public static void main(String[] args) {
                        Sell sell = new Sell();
                        new custom(sell, 1).start();
                        new custom(sell, 2).start();
                        new custom(sell, 3).start();
                        new custom(sell, 4).start();
                        new custom(sell, 5).start();
                        }
                        }
                        class Sell {
                        int ticket_count = 100;
                        boolean b = false;
                        public int sell(int n) {
                        if(ticket_count>0)
                        {
                        System.out.println(n + " 号售票站出售" + ticket_count+"号车票");
                        ticket_count--;
                        }
                        return ticket_count;
                        }
                        }
                        class custom extends Thread {
                        Sell sell;
                        int n;
                        custom(Sell sell, int n) {
                        this.sell = sell;
                        this.n = n;
                        }
                        Ticket ticket;
                        public void run() {
                        while (this.sell.sell(n) > 0) {
                        try {
                        sleep((int) (Math.random() * 1000));
                        } catch (Exception e) {
                        System.out.println(e);
                        }
                        sell.sell(n);
                        }
                        }
                        }


                        12楼2010-05-06 23:30
                        回复
                          public class Ticket {
                          public static void main(String[] args) {
                          Sell sell = new Sell();
                          new custom(sell, 1).start();
                          new custom(sell, 2).start();
                          new custom(sell, 3).start();
                          new custom(sell, 4).start();
                          new custom(sell, 5).start();
                          }
                          }
                          class Sell {
                          int ticket_count = 100;
                          boolean b = false;
                          public int sell(int n) {
                          if(ticket_count>0)
                          {
                          System.out.println(n + " 号售票站出售" + ticket_count+"号车票");
                          ticket_count--;
                          }
                          return ticket_count;
                          }
                          }
                          class custom extends Thread {
                          Sell sell;
                          int n;
                          custom(Sell sell, int n) {
                          this.sell = sell;
                          this.n = n;
                          }
                          Ticket ticket;
                          public void run() {
                          while (this.sell.sell(n) > 0) {
                          try {
                          sleep((int) (Math.random() * 1000));
                          } catch (Exception e) {
                          System.out.println(e);
                          }
                          sell.sell(n);
                          }
                          }
                          }
                          对多线程实在不熟啊,貌似很不严谨,结果是没问题的,只是不知道是不是你想要的那种...凑合着看吧


                          13楼2010-05-06 23:30
                          回复
                            public class ThreadTest implements Runnable {
                                 private static int ticket = 100;
                                 private static int num;
                                 public void run() {
                                     while (ticket > 0) {
                                         System.out.println(Thread.currentThread().getName() + "售出第"
                                                 + ticket-- + "张火车票");
                                         num++;
                                         try {
                                             Thread.currentThread().sleep(1);
                                         } catch (InterruptedException e) {
                                             // TODO Auto-generated catch block
                                             e.printStackTrace();
                                         }
                                     }
                                    
                                     if(ticket == 0){    
                                         System.out.println("火车票已卖完,共卖出"+num+"张车票");
                                         return;
                                     }
                                 }
                                 public static void main(String[] args) {
                                    
                                     ThreadTest t = new ThreadTest();
                                     new Thread(t, "1号售票口").start();
                                     new Thread(t, "2号售票口").start();
                                     new Thread(t, "3号售票口").start();
                                     new Thread(t, "4号售票口").start();
                                     new Thread(t, "5号售票口").start();
                                    
                                 }
                            }


                            IP属地:北京14楼2010-05-07 13:15
                            回复
                              2025-05-10 08:45:56
                              广告
                              非常感谢~


                              IP属地:广东15楼2010-05-07 14:45
                              回复