卖票(使用ConcurrentQueue提高并发性)
package src.main.java.com.qqjx.thread;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class TicketSeller4 {
static Queue<String> tickets = new ConcurrentLinkedQueue<>();
static {
for(int i=0; i<1000; i++) tickets.add("票 编号:" + i);
}
public static void main(String[] args) {
for(int i=0; i<10; i++) {
new Thread(()->{
while(true) {
String s = tickets.poll();
if(s == null) break;
else System.out.println("销售了--" + s);
}
}).start();
}
}
}
销售了--票 编号:997
销售了--票 编号:998
销售了--票 编号:999
销售了--票 编号:769
销售了--票 编号:639
销售了--票 编号:625
销售了--票 编号:603
销售了--票 编号:592
销售了--票 编号:591
销售了--票 编号:578
销售了--票 编号:912
销售了--票 编号:895