1、具体实现
- 创建线程类 MyThread 继承 Thread。
- 创建线程对象:MyThread thread1 = new MyThread()。
- 调用线程对象的 start() 方法。
public class ThreadDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread("线程一");
MyThread thread2 = new MyThread("线程二");
thread1.start();
thread2.start();
}
static class MyThread extends Thread {
// 100 张票
private static int ticket = 100;
public MyThread() {
super();
}
public MyThread(String name) {
super(name);
}
@Override
public void run() {
while (ticket > 0) {
ticket--;
System.out.println(Thread.currentThread() + "卖掉了1张票,剩余票数为:" + ticket);
}
}
}
}