补充:刚刚说的方法是使用Thread方式创建线程
如果用Runnable方式,可修改为- class Ticket extends Thread implements Runnable
- {
- private static int ticket = 100;
-
- public void run()
- {
- while(ticket>0)
- {
- System.out.println(Thread.currentThread().getName()+"--sale--"+ ticket--);
- }
- }
- }
- class ThreadTest1
- {
- public static void main(String[] args)
- {
- Ticket t_1 = new Ticket();
- Ticket t_2 = new Ticket();
- Ticket t_3 = new Ticket();
- Ticket t_4 = new Ticket();
- new Thread(t_1,"one").start();
- new Thread(t_2,"two").start();
- new Thread(t_3,"three").start();
- new Thread(t_4,"four").start();
- }
- }
复制代码 总之不要两个方法一起用就好。。。对于class Ticket extends Thread implements Runnable位置我没有改,虽然不影响编译,但是最好还是去掉,这样也能明显的看出来用的是Thread方法还是Runnable方法 |