多线程顺序执行

2018-05-10  本文已影响0人  totohui

第一种方法是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();

上一篇下一篇

猜你喜欢

热点阅读