一个窗口共卖出5张票和每个窗口都卖出5张票-----难道是run里面循环造成的结构???

xiaoxiao2021-02-28  131

一个窗口共卖出5张票

package com.sxt; class MyThread implements Runnable{ private int ticketsCont=5; @Override public void run() { while(ticketsCont>0){ ticketsCont--; System.out.println(Thread.currentThread().getName()+"卖出了第"+ticketsCont+"张票"); } } } public class TicketsRunnable { public static void main(String[] args) { MyThread mt=new MyThread(); Thread th1=new Thread(mt,"窗口1"); Thread th2=new Thread(mt,"窗口2"); Thread th3=new Thread(mt,"窗口3"); th1.start(); th2.start(); th3.start(); } }

每个窗口都卖出5张票

package com.sxt; class Test implements Runnable{ int ticket=5; @Override public void run() { for (int i = ticket; i >0; i--) { System.out.println(Thread.currentThread().getName()+"卖出了第"+i+"张票"); } } } public class RunnableDemo{ public static void main(String[] args) { Test ra=new Test(); Thread t=new Thread(ra,"窗口1"); Thread t1=new Thread(ra,"窗口2"); t.start(); t1.start(); } }

转载请注明原文地址: https://www.6miu.com/read-23016.html

最新回复(0)