Java线程安全问题(栗子)

线程类

public class SellTicketTwo extends Thread {
    private static int tickets = 100;
    private static Object lock = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (lock/*锁对象,要求唯一*/) {
                if (tickets <= 0) {
                    System.out.println("票已售完____");
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(this.getName() + "线程卖出一张票,当前剩余票" + (--tickets) + "======");
            }
        }
    }
}

运行

public class TestSellTicketTwo {

    public static void main(String[] args) {
        SellTicketTwo sellTicketOne0 = new SellTicketTwo();
        SellTicketTwo sellTicketOne1 = new SellTicketTwo();
        SellTicketTwo sellTicketOne2 = new SellTicketTwo();
        sellTicketOne0.start();
        sellTicketOne1.start();
        sellTicketOne2.start();
    }
}

结果:

Thread-0线程卖出一张票,当前剩余票99======
Thread-0线程卖出一张票,当前剩余票98======
Thread-0线程卖出一张票,当前剩余票97======
Thread-0线程卖出一张票,当前剩余票96======
Thread-0线程卖出一张票,当前剩余票95======
Thread-0线程卖出一张票,当前剩余票94======
Thread-2线程卖出一张票,当前剩余票93======
Thread-1线程卖出一张票,当前剩余票92======
Thread-1线程卖出一张票,当前剩余票91======
Thread-1线程卖出一张票,当前剩余票90======
Thread-1线程卖出一张票,当前剩余票89======
Thread-1线程卖出一张票,当前剩余票88======
Thread-2线程卖出一张票,当前剩余票87======
Thread-0线程卖出一张票,当前剩余票86======
Thread-0线程卖出一张票,当前剩余票85======
Thread-0线程卖出一张票,当前剩余票84======
Thread-0线程卖出一张票,当前剩余票83======
Thread-0线程卖出一张票,当前剩余票82======
Thread-0线程卖出一张票,当前剩余票81======
Thread-0线程卖出一张票,当前剩余票80======
Thread-0线程卖出一张票,当前剩余票79======
Thread-0线程卖出一张票,当前剩余票78======
Thread-2线程卖出一张票,当前剩余票77======
Thread-2线程卖出一张票,当前剩余票76======
Thread-2线程卖出一张票,当前剩余票75======
Thread-2线程卖出一张票,当前剩余票74======
Thread-2线程卖出一张票,当前剩余票73======
Thread-2线程卖出一张票,当前剩余票72======
Thread-2线程卖出一张票,当前剩余票71======
Thread-2线程卖出一张票,当前剩余票70======
Thread-2线程卖出一张票,当前剩余票69======
Thread-1线程卖出一张票,当前剩余票68======
Thread-1线程卖出一张票,当前剩余票67======
Thread-1线程卖出一张票,当前剩余票66======
Thread-1线程卖出一张票,当前剩余票65======
Thread-1线程卖出一张票,当前剩余票64======
Thread-2线程卖出一张票,当前剩余票63======
Thread-0线程卖出一张票,当前剩余票62======
Thread-0线程卖出一张票,当前剩余票61======
Thread-0线程卖出一张票,当前剩余票60======
Thread-0线程卖出一张票,当前剩余票59======
Thread-0线程卖出一张票,当前剩余票58======
Thread-0线程卖出一张票,当前剩余票57======
Thread-0线程卖出一张票,当前剩余票56======
Thread-0线程卖出一张票,当前剩余票55======
Thread-0线程卖出一张票,当前剩余票54======
Thread-2线程卖出一张票,当前剩余票53======
Thread-2线程卖出一张票,当前剩余票52======
Thread-2线程卖出一张票,当前剩余票51======
Thread-2线程卖出一张票,当前剩余票50======
Thread-2线程卖出一张票,当前剩余票49======
Thread-2线程卖出一张票,当前剩余票48======
Thread-2线程卖出一张票,当前剩余票47======
Thread-2线程卖出一张票,当前剩余票46======
Thread-2线程卖出一张票,当前剩余票45======
Thread-1线程卖出一张票,当前剩余票44======
Thread-1线程卖出一张票,当前剩余票43======
Thread-2线程卖出一张票,当前剩余票42======
Thread-2线程卖出一张票,当前剩余票41======
Thread-0线程卖出一张票,当前剩余票40======
Thread-0线程卖出一张票,当前剩余票39======
Thread-2线程卖出一张票,当前剩余票38======
Thread-1线程卖出一张票,当前剩余票37======
Thread-2线程卖出一张票,当前剩余票36======
Thread-2线程卖出一张票,当前剩余票35======
Thread-0线程卖出一张票,当前剩余票34======
Thread-0线程卖出一张票,当前剩余票33======
Thread-2线程卖出一张票,当前剩余票32======
Thread-2线程卖出一张票,当前剩余票31======
Thread-1线程卖出一张票,当前剩余票30======
Thread-1线程卖出一张票,当前剩余票29======
Thread-1线程卖出一张票,当前剩余票28======
Thread-1线程卖出一张票,当前剩余票27======
Thread-2线程卖出一张票,当前剩余票26======
Thread-2线程卖出一张票,当前剩余票25======
Thread-2线程卖出一张票,当前剩余票24======
Thread-2线程卖出一张票,当前剩余票23======
Thread-2线程卖出一张票,当前剩余票22======
Thread-2线程卖出一张票,当前剩余票21======
Thread-0线程卖出一张票,当前剩余票20======
Thread-0线程卖出一张票,当前剩余票19======
Thread-0线程卖出一张票,当前剩余票18======
Thread-0线程卖出一张票,当前剩余票17======
Thread-2线程卖出一张票,当前剩余票16======
Thread-2线程卖出一张票,当前剩余票15======
Thread-2线程卖出一张票,当前剩余票14======
Thread-2线程卖出一张票,当前剩余票13======
Thread-1线程卖出一张票,当前剩余票12======
Thread-1线程卖出一张票,当前剩余票11======
Thread-2线程卖出一张票,当前剩余票10======
Thread-2线程卖出一张票,当前剩余票9======
Thread-0线程卖出一张票,当前剩余票8======
Thread-0线程卖出一张票,当前剩余票7======
Thread-2线程卖出一张票,当前剩余票6======
Thread-1线程卖出一张票,当前剩余票5======
Thread-2线程卖出一张票,当前剩余票4======
Thread-2线程卖出一张票,当前剩余票3======
Thread-2线程卖出一张票,当前剩余票2======
Thread-2线程卖出一张票,当前剩余票1======
Thread-2线程卖出一张票,当前剩余票0======
票已售完____
票已售完____
票已售完____

进程已结束,退出代码为 0

你可能感兴趣的:(java,开发语言)