public class RunnableDemo { public static void main(String[] args) { MyThrd mt = new MyThrd(); Thread t1 = new Thread(mt); Thread t2 = new Thread(mt); t1.start(); t2.start(); } }
class MyThrd implements Runnable { private int ticket = 5; public void run() { for (int i = 0; i < 5; i++) { if (ticket > 0) { System.out.println("剩余票数:" + ticket--); } } } }