多线程,模拟卖票
package JAVA实验;
public class Ticket implements Runnable{
int number;
public Ticket() {
number = 100;
}
public static void main(String[] args) {
Ticket application = new Ticket();
for(int i = 0; i < 5; ++ i){
new Thread(application).start();
}
}
public void run() {
while(number > 0) {
ticketNumber();
}
}
private synchronized void ticketNumber() {
if(number > 0) {
try{
Thread.sleep(100);
} catch (Exception e) {
System.out.println("error");
}
String name = Thread.currentThread().getName();
int cur = name.charAt(name.length() - 1) - '0' + 1;
System.out.println(cur + "_____________" + number);
number--;
}
}
}