目录
问题引出
//如何控制线程的执行顺序
public class Main {
static Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread1...");
}
});
static Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread2...");
}
});
static Thread thread3=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread3...");
}
});
public static void main(String[] args) throws Exception{
thread1.start();
thread2.start();
thread3.start();
}
}
顺序是随机的
那么问题来了,怎么控制线程执行的顺序呢
比如说我想让thread1先执行,thread2在执行…
解决方法1:
join方法 ——-> 保证多线程的顺序性
public class Main {
static Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread1...");
}
});
static Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread2...");
}
});
static Thread thread3=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread3...");
}
});
public static void main(String[] args) throws Exception{
thread1.start();
thread1.join();
thread2.start();
thread2.join();
thread3.start();
}
}
join的作用:让主线程的等待子线程执行完后再执行
主线程会放弃主线程的执行权
看一哈join的源码:
/**
* Waits for this thread to die.
*
* <p> An invocation of this method behaves in exactly the same
* way as the invocation
*
* <blockquote>
* {@linkplain #join(long) join}{@code (0)}
* </blockquote>
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final void join() throws InterruptedException {
join(0);
}
调用join(0)
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;
}
}
}
调用wait(0)
public final native void wait(long timeout) throws InterruptedException;
最终是调用object的wait方法
在这里thread1.join()调用后,main Thread会阻塞起来。
那么main Thread被阻塞后是在哪收到notify而继续运行的呢?
这个过程涉及到了JVM底层了。thread1线程执行完毕了exit过程会有一个notifyall操作
在网上找的:
1.java.lang.Thread.join方法通过循环阻塞主线程的方式保证当前线程优先执
2.当前线程执行完之后会立马唤醒主线程继续执行 注意lock.notify_all(thread)
解决方法2:
用线程池的方法
public class Main2 {
static Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread1...");
}
});
static Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread2...");
}
});
static Thread thread3=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread3...");
}
});
static ExecutorService executorService = Executors.newSingleThreadExecutor(); //FIFO 先进先出
public static void main(String[] args) throws Exception{
executorService.submit(thread1);
executorService.submit(thread2);
executorService.submit(thread3);
executorService.shutdown();
}
}
转载请注明:汪明鑫的个人博客 » 如何控制多线程执行顺序
说点什么
您将是第一位评论人!