本帖最后由 madianguo 于 2013-3-26 21:01 编辑
- public class RunnableDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Ticket1 t = new Ticket1();
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- t1.start();
- t2.start();
- t3.start();
- }
- }
- class Ticket1 implements Runnable{
-
- private int tick = 1000;
- Object mutex = new Object();
- public void run(){
- while(true){
- synchronized (mutex ) {
- if (tick > 0)
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- }
- System.out.println(Thread.currentThread().getName() + " "
- + "ticket" + tick--);
- }
- }
- }
- }
复制代码 不知道哪里的问题。线程根本没有办法停止。只能强制结束 输出的结果一开始正常,但tick变成负数后,程序还一直运行下去
。。。。。。
Thread-2 ticket-55636Thread-2 ticket-55637
Thread-2 ticket-55638
Thread-2 ticket-55639
Thread-2 ticket-55640
Thread-2 ticket-55641
|