先上代码

新建一个Thread,代码如下:

  1. package com.thread.test;
  2.  
  3. public class MyThread extends Thread {
  4. private String name;
  5. public MyThread(String name) {
  6. this.name = name;
  7. }
  8. @Override
  9. public void run() {
  10. for (int i = 0; i < 100; i++) {
  11. System.out.println(name+"["+i+"]");
  12. }
  13. super.run();
  14. }
  15. }

之后新建测试类,代码如下:

  1. package com.thread.test;
  2. /*
  3. * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
  4. */
  5. public class ThreadDemo{
  6. public static void main(String[] args) {
  7. MyThread t = new MyThread("A");
  8. t.start();
  9. for (int i = 0; i < 100; i++) {
  10. if (i>50) {
  11. try {
  12. t.join();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. System.out.println("主线程"+"["+i+"]");
  18. }
  19. }
  20. }

下面是Java Platform SE8 API中对Thread中Join方法的解释:

  1. public final void join(long millis)
  2. throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
  3. This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
  4.  
  5. Parameters:
  6. millis - the time to wait in milliseconds
  7. Throws:
  8. IllegalArgumentException - if the value of millis is negative
  9. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

先上代码

新建一个Thread,代码如下:

  1. package com.thread.test;
  2.  
  3. public class MyThread extends Thread {
  4. private String name;
  5. public MyThread(String name) {
  6. this.name = name;
  7. }
  8. @Override
  9. public void run() {
  10. for (int i = 0; i < 100; i++) {
  11. System.out.println(name+"["+i+"]");
  12. }
  13. super.run();
  14. }
  15. }

之后新建测试类,代码如下:

  1. package com.thread.test;
  2. /*
  3. * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
  4. */
  5. public class ThreadDemo{
  6. public static void main(String[] args) {
  7. MyThread t = new MyThread("A");
  8. t.start();
  9. for (int i = 0; i < 100; i++) {
  10. if (i>50) {
  11. try {
  12. t.join();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. System.out.println("主线程"+"["+i+"]");
  18. }
  19. }
  20. }

下面是Java Platform SE8 API中对Thread中Join方法的解释:

  1. public final void join(long millis)
  2. throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
  3. This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
  4.  
  5. Parameters:
  6. millis - the time to wait in milliseconds
  7. Throws:
  8. IllegalArgumentException - if the value of millis is negative
  9. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

我自己的理解就是会强行进入使用join方法的线程,其他线程等待该线程完全执行完后才会进来。

浅谈Java多线程中的join方法的更多相关文章

  1. Java多线程中的join()方法

    一.join()方法介绍 join() 定义在Thread.java中.join()方法把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的join( ...

  2. Java多线程中的join方法

    新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String name ...

  3. java多线程中关于join方法的使用

    Thread的非静态方法join()让一个线程B"加入"到另外一个线程A的尾部.在A执行完毕之前,B不能工作.例如:         Thread t = new MyThread ...

  4. Java并发编程--多线程中的join方法详解

    Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...

  5. 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点

    浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...

  6. java 多线程中的wait方法的详解

    java多线程中的实现方式存在两种: 方式一:使用继承方式 例如: PersonTest extends Thread{ String name; public PersonTest(String n ...

  7. 【JAVA多线程中使用的方法】

    一.sleep和wait的区别. 1.wait可以指定时间,也可以不指定. 而sleep必须制定. 2.在同步的时候,对于CPU的执行权和以及锁的处理不同. wait:释放执行权,释放锁. sleep ...

  8. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  9. 浅谈Java多线程

    线程与进程 什么是进程? 当一个程序进入内存中运行起来它就变为一个进程.因此,进程就是一个处于运行状态的程序.同时进程具有独立功能,进程是操作系统进行资源分配和调度的独立单位. 什么是线程? 线程是进 ...

随机推荐

  1. MySQL学习笔记2(多表操作)

    外键:使两张表之间存在关联 特点: 1.从表外键的值是对主表主键的引用 2.从表外键类型,必须与主表主键类型一致 示例: 创建两个表并准备数据: USE mybase; CREATE TABLE ca ...

  2. ubuntu 16.04 下更换boost版本

    如果是新机器,没装过boost,那么直接一条命令 sudo apt-get install libboost-all-dev 头文件一般安装在 /usr/include 下面有一个目录boost,里面 ...

  3. 使用speex动态链接库过程中遇到问题及解决方法

    本以为speex的应用程序很容易就能跑起来,可是,实际操作中才发现,这里面暴露 的问题还真不少.看来以后不能眼高手低了,知行合一,这个一定要牢记在心中. speex安装成功后,可以一直无法调用动态链接 ...

  4. vue安装及axios、stylus、iview的安装流程整理

    现在做的项目中主要用到以下几个安装包,所以整理下流程: 使用命令行工具npm新创建一个vue项目 vue中axios的安装和使用 在vue项目中stylus的安装及使用 如何在vue中全局引入styl ...

  5. Go语言标准库之time

    Go语言标准库之time 时间的格式化和解析 格式化 Format Go语言和其他语言的时间格式化的方式不同,Go语言格式化的方式更直观,其他的语言一般是yyyy-mm-dd package main ...

  6. 测试工具之RobotFramework界面基本功能使用

    安装好RobotFramework后,直接在运行或者命令行中执行ride.py即可启动RF 启动完成后的界面如下: 界面很简洁,然后我们开始点击file并创建project: 接下来右键project ...

  7. CentOS 部署 NodeBB

    0x00 前言 NodeBB 是基于 NodeJS 的开源 BBS 系统,可以搭配 redis 或 mongodb 数据库,本文中由于使用 NodeBB 配置中默认的 npm 安装会引起不知原因的 5 ...

  8. C语言第六讲,数组

    C语言第六讲,数组 一丶什么是数组 数组,就是一整块的连续内存空间. 且类型都是一样的.大小一样 比如: 1.1数组元素的访问 我们要访问数组,例如上面,我们访问元算2,元素3等等怎么访问.. 比如有 ...

  9. centos swap

    SWAP是Linux中的虚拟内存,用于扩充物理内存不足而用来存储临时数据存在的.它类似于Windows中的虚拟内存.在Windows中,只可以使用文件来当作虚拟内存.而linux可以文件或者分区来当作 ...

  10. 微信小程序中this指向作用域问题this.setData is not a function报错

    在微信小程序中我们一般通过以下方式来修改data中的数据 this.setData({ index1: e.detail.value }) 比如在函数里面修改数据 bindFaChange1: fun ...