【JAVA】多线程

class TicketWindow extends Thread
{
    private static int tickets = 100;

    @Override
    public void run() {
        while (true)
        {
            if (tickets > 0)
            {
                Thread thread = Thread.currentThread();
                String threadName = thread.getName();
                System.out.println(threadName + "1: " + tickets--);
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class G {
    public static void main(String[] args) {
        new TicketWindow().start();
        new TicketWindow().start();
        new TicketWindow().start();
        new TicketWindow().start();
    }
}

运行结果:

【JAVA】多线程_第1张图片

你可能感兴趣的:(java,算法,前端)