package com.ajax;
//多线程Runnable类创建多线程
public class Example01 {
public static void main(String [] args){
TicketWindow tw=new TicketWindow();
new Thread(tw,"窗口1").start();
new Thread(tw,"窗口2").start();
new Thread(tw,"窗口3").start();
new Thread(tw,"窗口4").start();
}
}
class TicketWindow implements Runnable{
private int tickets=10;
public void run(){
while(true){
if(tickets>0){
Thread th=Thread.currentThread();//获取当前线程
String th_name=th.getName();
System.out.println(th_name+"正在发售第"+tickets--+"张票");
}
}
}
}
//结果解释:四个窗口共享资源,发送10张票,没有重复发售
多线程Runnable类创建多线程
最新推荐文章于 2022-06-12 17:07:09 发布