/* * extends THread也可以 不过都是使用下列方法 */ public class MyThread implements Runnable { private int ticket = 5; //5张票 public void run() { for (int i=0; i<=20; i++) { if (this.ticket > 0) { // Thread.sleep(3000);//休眠3秒 //得到线程的名字 System.out.println(Thread.currentThread().getName()+ "正在卖票 "+this.ticket--); } } } @SuppressWarnings("static-access") public static void main(String [] args) throws Exception { MyThread my = new MyThread(); Thread t = new Thread(my, "1号窗口"); //线程是否开启 System.out.println(t.isAlive()+" 线程"); // Thread.sleep(3000); t.start(); t.yield(); System.out.println(t.isAlive()+" 线程"); new Thread(my, "2号窗口").start(); new Thread(my, "3号窗口").start(); } }
时间: 2024-11-10 14:58:07