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();
????}
}
2020-12-01
new多次就不是用同一資源了
2019-02-16
我發(fā)現(xiàn)是你說的那樣昵,不管是繼承Thread還是實(shí)現(xiàn)Runnable,關(guān)鍵還是看new了幾個線程,如果都只是new1個線程,就一共賣票5張,因?yàn)?個對象繼承Thread或者實(shí)現(xiàn)Runnable的對象都共享同一個資源。