多线程的简单举例
这里以窗口卖票为例,一共有10张票、3个窗口,卖的票不能重、不能多。
多线程的基本实现有好几种方法,包括继承Thread类,实现Runnable接口,使用线程池等。
实际开发中一般会采用线程池,这里初学演示使用Runnable接口。
public class TicketRunnable implements Runnable {
private int ticket=10;
@Override
public void run() {
while (true){
synchronized (this){
if (ticket>0){
System.out.println(Thread.currentThread().getName()+":"+ticket);
ticket--;
}else