Simulated train ticket purchase case

 

 

There are N train tickets, and each ticket has a number. At the same time, there are 10 windows for external ticket sales. Please write a simulation program

 

1 Implemented with ArrayList

 

public class TicketSeller1 {

 

    static List<String> tickets = new ArrayList<>();

 

    static {

        SortedMap s = new TreeMap();

        for (int i = 0; i < 10000 ; i++) {

            tickets.add("ticket number: "+i);

        }

    }

 

    public static void main(String[] args) {

        for (int i = 0; i < 10 ; i++) {

            new Thread(()->{

                while (tickets.size()>0){

                  System.out.println("Sold--"+tickets.remove(0));

                }

 

            }).start();

        }

    }

 

}

 

result:

 

 

2 Realize with Vector

 

public class TicketSeller2 {

 

    static Vector<String> tickets = new Vector<>();

 

    static {

        for (int i = 0; i < 1000 ; i++) {

            tickets.add("ticket number: "+i);

        }

    }

 

    public static void main(String[] args) {

        for (int i = 0; i <10 ; i++) {

            new Thread(()->{

 

                while (tickets.size()>0) {

                    try {

                        TimeUnit.MILLISECONDS.sleep(10);

                    } catch (InterruptedException e) {

                        e.printStackTrace ();

                    }

 

                    System.out.println("Sold--" + tickets.remove(0));

                }

            }).start();

        }

    }

 

}

 

result:

 

3 Realize with synchronized+LinkedList

 

public class TicketSeller3 {

 

    static List<String> tickets = new LinkedList<>();

 

    static {

 

        for (int i = 0; i < 1000 ; i++) {

            tickets.add("ticket number:"+i);

        }

 

    }

 

    public static void main(String[] args) {

        for (int i = 0; i <10 ; i++) {

            new Thread(()->{

 

                while (true){

 

                    synchronized (tickets){

 

                        if(tickets.size()<=0)break;

 

                        System.out.println("Sold--" + tickets.remove(0));

                    }

 

                }

 

            }).start();

        }

    }

 

}

 

result:

 

 

4 Implemented with ConcurrentLinkedQueue

public class TicketSeller4 {

 

    static Queue<String> tickets = new ConcurrentLinkedQueue<>();

 

    static {

        for (int i = 0; i < 1000; i++) {

            tickets.add("ticket number:" + 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("sold:" + s);

 

 

                }

 

            }).start();

        }

    }

}

 

 

to sum up:

 

1 ArrayList is not thread-safe, so it can not be realized, there will be more ticket sales

2 Although Vector is thread-safe, it is only thread-safe in terms of methods, as follows:

 

3 synchronized+LinkedList can be realized, and synchronization security can be realized by adding locks.

4 ConcurrentLinkedQueue can be realized, only cas + spin lock can realize thread safety.

Guess you like

Origin blog.csdn.net/huzhiliayanghao/article/details/106768018