0%
Java创建多线程有两种方式:
- 1、继承Thread类的方式
- 2、实现Runnable接口的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
class Window1 implements Runnable {
private int ticket = 100;
@Override public void run() { while (true) { if (ticket > 0) { System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket); ticket--; } else { break; } } } }
public class WindowTest1 { public static void main(String[] args) { Window1 w1 = new Window1();
Thread t1 = new Thread(w1); Thread t2 = new Thread(w1); Thread t3 = new Thread(w1);
t1.setName("窗口1"); t2.setName("窗口2"); t3.setName("窗口3");
t1.start(); t2.start(); t3.start(); } }
|