变量在线程里访问没问题,但是s放到类下面就会有访问问题(变成多个线程访问同一变量)
public class TicketSeller4 {static Queue<String> tickets = new ConcurrentLinkedQueue<>();// static String s = null;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();}}}
