package lianxi;
public class MaiPiao {
多线程的题希望对大家有帮助
/**
* @param args
*/
public static void main(String[] args) {
Ticket tickets=new Ticket();
Thread thread=new Thread(tickets,"张三");
Thread thread2 =new Thread(tickets,"李四");
Thread thread3=new Thread(tickets,"王五");
thread.start();
thread2.start();
thread3.start();
}
}
package lianxi;
import java.util.concurrent.SynchronousQueue;
public class Ticket implements Runnable {
private int number=1000;
@Override
public void run() {
while (true){
synchronized (this){
if(number>0){
System.out.println(Thread.currentThread().getName()+this.getClass()+"正在出售第"+number+"张票");
number--;
}else{
break;
}
}
}
}
}
|