很简单的一个例子:比如赛车,同时启动,如何用多线程编写,我知道的一种方法是让几个线程进入阻塞等待,然后notifyAll,脑子懵的不知道怎么coding了  求指导一下

解决方案 »

  1.   

    public static void main(String[] args) throws Exception {
            Thread t1 = getThread("小明");
            Thread t2 = getThread("张三");
            Thread t3 = getThread("李四");
            t1.start();
            t2.start();
            t3.start();
            t1.join();
            t2.join();
            t3.join();
            System.out.println("全部到达终点 比赛结束");
        }    private static Thread getThread(String name) {
            return new Thread(name){
                   @Override
                   public void run() {
                       System.out.println( Thread.currentThread().getName()+" 赛车启动");
                       try {
                           Thread.sleep(2000);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       System.out.println(Thread.currentThread().getName()+" 到达终点");
                   }
               };
        }
      

  2.   

    这个我懂,如何用wait和notifyAll呢
      

  3.   

    我觉得应该是这样,只要当前线程执行了run方法,然后调用wait,直到所有线程都调用了run方法,再最后在调用notifyAll,楼主可以考虑一下。
      

  4.   

    同时启动不可能的,总要有先后,即使你挂起阻塞,设置优先级,然后notifyall,也是随机的,不是所有线程都一定的,不用纠结了
      

  5.   

    楼主描述的情况:几个线程同时准备好了,然后一块运行,类似于跑步比赛,运动员准备好后,大家同时跑。
    这种常见的需求可是使用jdk1.5提供的java.util.concurrent 里面的 CyclicBarrier 和 CountDownLatch 工具类实现。CyclicBarrier实现如下:
    package concurrentutil;import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    import java.util.concurrent.TimeUnit;public class CyclicBarrierTest {
    public static void main(String[] args) {
    CyclicBarrier barrier = new CyclicBarrier(3);
    new Thread(()->{
    try {
    System.out.println("t1 ready");
    TimeUnit.SECONDS.sleep(2);
    System.out.println("t1 ok");
    barrier.await();
    } catch (InterruptedException | BrokenBarrierException e) {
    e.printStackTrace();
    }
    System.out.println("t1 go");
    }).start();

    new Thread(()->{
    try {
    System.out.println("t2 ready");
    TimeUnit.SECONDS.sleep(1);
    System.out.println("t2 ok");
    barrier.await();
    } catch (InterruptedException | BrokenBarrierException e) {
    e.printStackTrace();
    }
    System.out.println("t2 go");
    }).start();

    new Thread(()->{
    try {
    System.out.println("t3 ready");
    TimeUnit.SECONDS.sleep(4);
    System.out.println("t3 ok");
    barrier.await();
    } catch (InterruptedException | BrokenBarrierException e) {
    e.printStackTrace();
    }
    System.out.println("t3 go");
    }).start();

    }
    }
    CountDownLatch实现如下:
    package concurrentutil;import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
    CountDownLatch count = new CountDownLatch(1);
    new Thread(() -> {
    try {
    System.out.println(Thread.currentThread().getName()+"进入等待...");
    count.await();
    System.out.println(Thread.currentThread().getName()+"运行完毕");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }).start();

    new Thread(() -> {
    try {
    System.out.println(Thread.currentThread().getName()+"进入等待...");
    count.await();
    System.out.println(Thread.currentThread().getName()+"运行完毕");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }).start();

    System.out.println(Thread.currentThread().getName()+"线程等待...");
    TimeUnit.SECONDS.sleep(3);
    count.countDown();
    }
    }