222

int tickets = 100;

    String string = "";

 

executor.execute(new Runnable() {

            @Override

            public void run() {

                while (tickets > 0) {

                    synchronized (string) {

                        try {

                            if (tickets > 0) {

                                System.out.println(Thread.currentThread().getName()

                                        + "卖出了第" + (tickets--) + "张票");

                                Thread.sleep(1000);

                            }

 

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                }

                

            }

        });

原文地址:https://www.cnblogs.com/yc5518/p/9268548.html