ariyue 发表于 2013-2-3 10:30:10

JAVA线程实例-----卖票

package test;import java.io.*;public class Test{   public static void main(String[] args)   {         Tickits t = new Tickits();         new Thread(t,"窗口1").start();         new Thread(t,"窗口2").start();   }}class Tickits implements Runnable{   int num;   Tickits()   {         num = 10;   }   public void run()   {                  while (true)             {               synchronized (this)               {                     if (num > 0)                     {                         try                         {                           Thread.sleep(1000);                         } catch (InterruptedException ex)                         {                           ex.printStackTrace();                         }                         System.out.println(Thread.currentThread().getName() +                                          " selling tickit" + num);                         num--;                     } else                         break;               }             }         }}
页: [1]
查看完整版本: JAVA线程实例-----卖票