Java中线程顺序执行
现有线程threadone、threadtwo和threadthree,想要的运行顺序为threadone->threadtwo->threadthree,应该如何处理?这里需要用到一个简单的线程方法join().
join()方法的说明:join方法挂起当前调用线程,直到被调用线程完成后在继续执行(join() method suspends the execution of the calling thread until the object called finishes its execution).
下面看一个例子:
/**
* Thread one for test.
*/
public class ThreadOne implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start..."); System.out.println(threadName + " end.");
}
}
/**
* Thread two for test.
*/
public class ThreadTwo implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start..."); System.out.println(threadName + " end.");
}
}
/**
* Thread three for test.
*/
public class ThreadThree implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start..."); System.out.println(threadName + " end.");
}
}
public class JoinMainTest {
public static void main(String[] args) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start...");
Thread firstThread = new Thread(new ThreadOne());
Thread secondThread = new Thread(new ThreadTwo());
Thread thirdThread = new Thread(new ThreadThree());
// 1. no order thread run
firstThread.start();
secondThread.start();
thirdThread.start();
System.out.println(threadName + " end.");
}
}
上面得到的结果如下:
/**
*
main start...
Thread-0 start...
main end.
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
*/
public class JoinMainTest {
public static void main(String[] args) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start...");
Thread firstThread = new Thread(new ThreadOne());
Thread secondThread = new Thread(new ThreadTwo());
Thread thirdThread = new Thread(new ThreadThree());
// 2. thread run in order
try {
firstThread.start();
firstThread.join();
secondThread.start();
secondThread.join();
thirdThread.start();
thirdThread.join();
} catch (Exception ex) {
System.out.println("thread join exception.");
}
System.out.println(threadName + " end.");
}
}
这里得到的结果就是顺序执行的了:
/**
*
main start...
Thread-0 start...
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
main end.
*/
再来看一下join()的源码,join()调用的就是join(0).可以看到,其实join就是wait,直到线程执行完成。
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;
}
}
}
Java中线程顺序执行的更多相关文章
- 面试官:Java中线程是按什么顺序执行的?
摘要:Java中多线程并发的执行顺序历来是面试中的重点,掌握Java中线程的执行顺序不仅能够在面试中让你脱颖而出,更能够让你在平时的工作中,迅速定位由于多线程并发问题导致的"诡异" ...
- Java中如何保证线程顺序执行
只要了解过多线程,我们就知道线程开始的顺序跟执行的顺序是不一样的.如果只是创建三个线程然后执行,最后的执行顺序是不可预期的.这是因为在创建完线程之后,线程执行的开始时间取决于CPU何时分配时间片,线程 ...
- Android中让多个线程顺序执行探究
线程调度是指按照特定机制为多个线程分配CPU的使用权. 有两种调度模型:分时调度模型和抢占式调度模型. 分时调度模型:是指让所有的线程轮流获得cpu的使用权,并且平均分配每个线程占用的CPU的时间片. ...
- [转]JAVA程序执行顺序,你了解了吗:JAVA中执行顺序,JAVA中赋值顺序
本文主要介绍以下两块内容的执行顺序,熟悉的大虾可以直接飘过. 一.JAVA中执行顺序 静态块 块 构造器 父类构造器 二.JAVA中赋值顺序 静态块直接赋值 块直接赋值 父类继承的属性已赋值 静态变量 ...
- 沉淀再出发:java中线程池解析
沉淀再出发:java中线程池解析 一.前言 在多线程执行的环境之中,如果线程执行的时间短但是启动的线程又非常多,线程运转的时间基本上浪费在了创建和销毁上面,因此有没有一种方式能够让一个线程执行完自己的 ...
- java中线程机制
java中线程机制,一开始我们都用的单线程.现在接触到多线程了. 多线性首先要解决的问题是:创建线程,怎么创建线程的问题: 1.线程的创建: 四种常用的实现方法 1.继承Thread. Thread是 ...
- Java中线程的使用 (2)-多线程、线程优先级、线程睡眠、让步、阻塞
Java中线程的使用 (2)-多线程.线程优先级.线程睡眠.让步.阻塞 (一)多线程使用方法 说明:创建每个新的线程,一定要记得启动每个新的线程(调用.start()方法) class Xc3 ext ...
- JAVA中线程同步方法
JAVA中线程同步方法 1 wait方法: 该方法属于Object的方法,wait方法的作用是使得当前调用wait方法所在部分(代码块)的线程停止执行,并释放当前获得的调用wait所 ...
- 多线程(三) java中线程的简单使用
java中,启动线程通常是通过Thread或其子类通过调用start()方法启动. 常见使用线程有两种:实现Runnable接口和继承Thread.而继承Thread亦或使用TimerTask其底层依 ...
随机推荐
- iOS 网络编程
iOS 开发中所需的数据基本都是来自网络,网络数据请求是 iOS 编程中必不可少的,应该熟练掌握网络请求. 网络请求方式有 :GET , POST , PUT ,DELETE 等,其中常用的就是 GE ...
- Spring+MyBatis实践—MyBatis数据库访问
关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出.在此,记录一下几种通过MyBatis访问数据库的方式. 通过sqlSessionTempl ...
- 深入了解nagios的各配置文件
转自http://wolfword.blog.51cto.com/4892126/1220209 对每个配置文件进行讲解,深入理解nagios,好好学习,天天向上~ (1)templates.cf ...
- *[topcoder]GUMIAndSongsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12706&rd=15700 这题有意思.首先要观察到,如果选定一些 ...
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简介
http://www.cnblogs.com/kflwz/articles/1337310.html 1.下载最新版FreeTextBox(版本3.1.6),解压 FreeTextBox 3. ...
- USB Type-C工作原理解析
自从苹果发布了新MacBook,USB Type-C接口就成为了热议对象.我来从硬件角度解析下这个USB Type-C,以便大家更好的了解USB Type-C的工作原理. 特色 尺寸小,支持正反插,速 ...
- 负载均衡server load balancer
负载均衡(Server Load Balancer,简称SLB)是对多台云服务器进行流量分发的负载均衡服务.SLB可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性. ( ...
- Java版本的在指定目录及子目录下创建指定的文件
和删除指定目录及子目录下名叫“xxx.txt”的所有文件一样,也是使用递归的方式实现的. 代码如下: public class Example826003 { private static FileO ...
- POJ 2398 Toy Storage
这道题和POJ 2318几乎是一样的. 区别就是输入中坐标不给排序了,=_=|| 输出变成了,有多少个区域中有t个点. #include <cstdio> #include <cma ...
- Deployed component GUIs and figures have different look and feel than MATLAB desktop
原文:http://www.mathworks.com/support/bugreports/1293244 Description Deployed GUIs and figures look an ...