本帖最后由 pywepe 于 2010-07-28 18:29:55 编辑

解决方案 »

  1.   

    按ABC的顺序打印10次ABC?
    那为啥还要用线程。
    感觉和调用方法没有区别, join()可以达到和调用方法一样的效果
      

  2.   


    用线程是题目给出的要求呢
    join()我去试试 不过上面的程序死锁是什么原因?
      

  3.   

    package com.xuz.csdn.june27;public class ThreadABC {
    private Object lock = new Object();
    private int intNum; public void execute() { new Thread() {
    public void run() {
    while (true) {
    if (intNum < 10) {
    synchronized (lock) {
    try {
    lock.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    } System.out.println(Thread.currentThread().getName() + " print ABC" + intNum);
    intNum++;
    } else {
    break;
    }
    } }
    }.start(); new Thread() {
    public void run() {
    while (true) {
    if (intNum < 10) {
    System.out.println(Thread.currentThread().getName()+" print ABC" + intNum);
    intNum++;

    synchronized (lock) {
    lock.notify();
    }
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    break;
    }
    }
    }
    }.start(); } public static void main(String[] argv) {
    new ThreadABC().execute();
    }}二个线程的。
      

  4.   

    可以有理解错题目的意思呢
    是按顺序打印线程的name,在这里是A B C罢了
      

  5.   

    package com.xuz.csdn.june27;import java.util.concurrent.atomic.AtomicInteger;public class ThreadABC {
    private Object lock1 = new Object();
    private Object lock2 = new Object();
    private Object lock3 = new Object();

    private AtomicInteger i = new AtomicInteger(0); public void execute() { new Thread() {
    public void run() {
    while (true) {
    if (i.getAndIncrement() < 10) {
    System.out.println(Thread.currentThread().getName());

    synchronized (lock1) {
    try {
    lock1.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    synchronized (lock2) {
    lock2.notify();
    }
    } else {
    synchronized (lock2) {
    lock2.notify();
    }
    break;
    }
    } }
    }.start(); new Thread() {
    public void run() {
    while (true) {
    if (i.getAndIncrement() < 10) {
    System.out.println(Thread.currentThread().getName());

    synchronized (lock2) {
    try {
    lock2.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    synchronized (lock3) {
    lock3.notify();
    }
    } else {
    synchronized (lock3) {
    lock3.notify();
    }
    break;
    }
    }
    }
    }.start();

    new Thread() {
    public void run() {
    while (true) {
    if (i.getAndIncrement() < 10) {
    System.out.println(Thread.currentThread().getName());

    synchronized (lock1) {
    lock1.notify();
    }

    synchronized (lock3) {
    try {
    lock3.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    synchronized (lock1) {
    lock1.notify();
    }
    break;
    }
    }
    }
    }.start();

    } public static void main(String[] argv) {
    new ThreadABC().execute();
    }}可以实现功能,但是觉得写得不好。
      

  6.   

    http://topic.csdn.net/u/20100801/12/e5265ef6-eaed-4cf7-a524-1e5592fa5967.html
      

  7.   

    死锁的原因在于 notify 和 简单的wait首先,多线程的运行是不确定的。因此
    main函数里aLk.notify();时,打印A的线程有可能还没开始运行
      这样就死锁了.
      同样,bLk.notify()时,如果打印B的线程还没开始运行,也有可能出现死锁
      

  8.   

    同步写的有问题啊。把你的线程里面的run方法加个sleep就行了。
    Thread a = new Thread("A") {
                @Override
                public void run() {
                 System.out.println("thread-A");
                    for (int i = 0; i < 10; i++) {
                        synchronized (aLk) {                        try {
                                aLk.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }                        System.out.println(this.getName() + " " + i);
                            try
    {
    Thread.sleep(10);
    }
    catch (InterruptedException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

                            synchronized (bLk) {
                                bLk.notify();
                            }
                        }
                    }
                }
            };