多线程:控制多线程按顺序执行
2018-02-08 11:30
447 查看
示例代码
static Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread1");
}
});
static Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread2");
}
});
static Thread t3 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread3");
}
});
1.使用join
public static void main(String[] args) throws InterruptedException {
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
join源码:
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
join让主线程等待,至子线程执行结束
2.使用ExecutorService
static ExecutorService es = Executors.newSingleThreadExecutor();
public static void main(String[] args) throws InterruptedException {
es.submit(t1);
es.submit(t2);
es.submit(t3);
es.shutdown();
}
ExecutorService 提供了4种线程池,分别是:
newCachedThreadPool;缓存线程池,可灵活回收、新建
newFixedThreadPool;定长线程池,可控制最大并发数,超出在队列中等待
newScheduledThreadPool;定长线程池,可定时、周期执行
newSingleThreadExecutor;单一线程池,按顺序执行(FIFO,LIFO,优先级)
static Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread1");
}
});
static Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread2");
}
});
static Thread t3 = new Thread(new Runnable() {
public void run() {
System.out.println("Thread3");
}
});
1.使用join
public static void main(String[] args) throws InterruptedException {
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
join源码:
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
join让主线程等待,至子线程执行结束
2.使用ExecutorService
static ExecutorService es = Executors.newSingleThreadExecutor();
public static void main(String[] args) throws InterruptedException {
es.submit(t1);
es.submit(t2);
es.submit(t3);
es.shutdown();
}
ExecutorService 提供了4种线程池,分别是:
newCachedThreadPool;缓存线程池,可灵活回收、新建
newFixedThreadPool;定长线程池,可控制最大并发数,超出在队列中等待
newScheduledThreadPool;定长线程池,可定时、周期执行
newSingleThreadExecutor;单一线程池,按顺序执行(FIFO,LIFO,优先级)
相关文章推荐
- Java控制多线程执行顺序
- java控制多线程执行顺序
- OC中多线程执行顺序的控制
- Java多线程系列四——控制线程执行顺序
- java 多线程控制线程执行顺序
- java控制多线程执行顺序
- java多线程-使用notify和wait完成多线程的顺序执行
- 命令执行顺序控制与管道
- 1.8命令执行顺序控制与管道(学习过程)
- Hibernate 语句执行顺序控制
- CountDownLatch 控制多线程 让多个线程执行完后再依次做其他的
- 控制线程顺序执行
- 控制台应用执行多线程时输出控制
- JUC 多线程顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- 使多线程按顺序执行的一种方法
- 关于linux c 多线程调度执行顺序
- 使用 CountDownLatch 控制多个线程执行顺序
- Servlet 3.0 之@WebFilter怎么控制多个filter的执行顺序
- 使用信号量控制线程执行顺序,进而控制不同视频流的解码顺序