peanut丶w
2019-02-16 14:28
public class Ticket extends Thread { private int tic = 5; @Override public void run() { while (tic > 0) { --tic; System.out.println(Thread.currentThread().getName() + "剩下" + tic + "张票"); } } public static void main(String[] args) { Ticket ticket = new Ticket(); Thread thread1 = new Thread(ticket, "线程1"); Thread thread2 = new Thread(ticket, "线程2"); Thread thread3 = new Thread(ticket, "线程3"); thread1.start(); thread2.start(); thread3.start(); } }
new多次就不是用同一资源了
我发现是你说的那样昵,不管是继承Thread还是实现Runnable,关键还是看new了几个线程,如果都只是new1个线程,就一共卖票5张,因为3个对象继承Thread或者实现Runnable的对象都共享同一个资源。
细说多线程之Thread VS Runnable
55327 学习 · 68 问题
相似问题