美文网首页
多线程顺序执行

多线程顺序执行

作者: totohui | 来源:发表于2018-05-10 14:18 被阅读0次

    第一种方法是join;第二种方法是线程池newSingleThreadExecutor

    final Thread t1 = new Thread(new Runnable() {

    @Override public void run() {

    System.out.println("1");

    }

    });

    final Thread t2 = new Thread(new Runnable() {

    @Override public void run() {

    try {

    t1.join();

    } catch (InterruptedException e) {

    e.printStackTrace(); }

    System.out.println("2");

    }

    });

    Thread t3 = new Thread(new Runnable() {

    @Override public void run() {

    try {

    t2.join();

    } catch (InterruptedException e) {

    e.printStackTrace(); }

    System.out.println("3"); } });

    t1.start();

    t2.start();

    t3.start();

    final Thread t1 = new Thread(new Runnable() {

    @Override public void run() {

    System.out.println("1");

    }

    });

    final Thread t2 = new Thread(new Runnable() {

    @Override public void run() {

    System.out.println("2");

    }

    });

    Thread t3 = new Thread(new Runnable() {

    @Override public void run() {

    System.out.println("3");

    }

    });

    ExecutorService es = Executors.newSingleThreadExecutor();

    es.submit(t1);

    es.submit(t2);

    es.submit(t3);

    es.shutdown();

    相关文章

      网友评论

          本文标题:多线程顺序执行

          本文链接:https://www.haomeiwen.com/subject/jkbzrftx.html