importjava.util.ArrayList;importjava.util.List;//集合publicclassUnsafeList{publicstaticvoidmain(String[] args){List<String> list =newArrayList<String>();for(int i =0; i <10000; i++){newThread(()->{synchronized(list){
list.add(Thread.currentThread().getName());}}).start();}try{Thread.sleep(3000);}catch(InterruptedException e){// TODO Auto-generated catch block
e.printStackTrace();}System.out.println(list.size());}}
同步方法同步块代码演示买票//买票public class UnsafeBuyTicket { public static void main(String[] args) { BuyTicket b = new BuyTicket(); new Thread(b, "a").start(); new Thread(b, "b").start(); new Thread(b, "c").start(); }}class BuyTicket implements