2 qq 35639030 qq_35639030 于 2016.09.20 00:04 提问

Java多线程继承Thread,简单购票,能不能有另一种方法。这个是我的方法

package cn.text;

public class ThreadSell extends Thread{
public int ticks=100;
public ThreadSell(String str){
super(str);
}
public void run(){
while(ticks>0)
{
synchronized(this){
try
{
Thread.sleep(100);
sell();
}
catch(InterruptedException e)
{
e.printStackTrace();
}

              }
      }

}
public void sell(){
    if(ticks>0)
    {
        System.out.println(Thread.currentThread().getName()+"卖出第"+ticks+"张票");
        ticks--;
    }
    else return;
}
public static void main(String[] arge){
    ThreadSell t =new ThreadSell("");
    Thread t1 =new Thread(t,"窗口1");
    Thread t2 =new Thread(t,"窗口2");
    t1.start();
    t2.start();

}

}

1个回答

caozhy
caozhy   Ds   Rxr 2016.09.20 00:10
qq_35639030
qq_35639030 能不能有继承Thread类的方法,这个接口的我用过了。嘿嘿
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片