java线程池示例

Java Thread join method can be used to pause the current thread execution until unless the specified thread is dead. There are three overloaded join functions.

Java Thread join方法可用于暂停当前线程执行,直到指定的线程死亡为止。 有三个重载的联接函数。

Java线程连接 (Java Thread join)

public final void join(): This java thread join method puts the current thread on wait until the thread on which it’s called is dead. If the thread is interrupted, it throws InterruptedException.

public final void join() :此java线程连接方法将当前线程置于等待状态,直到调用它的线程死亡为止。 如果线程被中断,则抛出InterruptedException。

public final synchronized void join(long millis): This java thread join method is used to wait for the thread on which it’s called to be dead or wait for specified milliseconds. Since thread execution depends on OS implementation, it doesn’t guarantee that the current thread will wait only for given time.

公共最终同步的void join(long millis) :此java线程连接方法用于等待调用它的线程失效或等待指定的毫秒数。 由于线程执行取决于OS实现,因此不能保证当前线程将仅等待给定时间。

public final synchronized void join(long millis, int nanos): This java thread join method is used to wait for thread to die for given milliseconds plus nanoseconds.

公共最终同步void join(long millis,int nanos) :此java线程连接方法用于在给定的毫秒数与纳秒之间等待线程死亡。

Here is a simple example showing usage of Thread join methods. The goal of the program is to make sure main is the last thread to finish and third thread starts only when first one is dead.

这是一个简单的示例,显示了线程连接方法的用法。 该程序的目标是确保main是最后一个线程完成而第三个线程仅在第一个线程死时才启动。

package com.journaldev.threads;public class ThreadJoinExample {public static void main(String[] args) {Thread t1 = new Thread(new MyRunnable(), "t1");Thread t2 = new Thread(new MyRunnable(), "t2");Thread t3 = new Thread(new MyRunnable(), "t3");t1.start();//start second thread after waiting for 2 seconds or if it's deadtry {t1.join(2000);} catch (InterruptedException e) {e.printStackTrace();}t2.start();//start third thread only when first thread is deadtry {t1.join();} catch (InterruptedException e) {e.printStackTrace();}t3.start();//let all threads finish execution before finishing main threadtry {t1.join();t2.join();t3.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("All threads are dead, exiting main thread");}}class MyRunnable implements Runnable{@Overridepublic void run() {System.out.println("Thread started:::"+Thread.currentThread().getName());try {Thread.sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Thread ended:::"+Thread.currentThread().getName());}}

Output of the above program is:

上面程序的输出是:

Thread started:::t1
Thread started:::t2
Thread ended:::t1
Thread started:::t3
Thread ended:::t2
Thread ended:::t3
All threads are dead, exiting main thread

That’s all for a quick roundup on java thread join example.

这就是对Java线程连接示例的快速汇总。

翻译自: https://www.journaldev.com/1024/java-thread-join-example

java线程池示例

java线程池示例_Java线程连接示例相关推荐

  1. java线程池执行器_Java线程池ThreadPoolExecutor的使用

    Java线程池ThreadPoolExecutor的使用 ThreadPoolExecutor就是我们用来实现线程的一个执行器,它实现了Excutor和ExecutorService接口.Excuto ...

  2. java 线程池数量_java线程池及创建多少线程合适

    java线程池 1.以下是ThreadPoolExecutor参数完备构造方法: public ThreadPoolExecutor(int corePoolSize,int maximumPoolS ...

  3. java线程池概念_Java 线程池概念、原理、简单实现

    线程池的思想概述 我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结東了,这样频繁创建线程就会大大降低系 ...

  4. java线程池功能_Java线程池总结

    一.线程池 线程池适合处理的任务:执行时间短.工作内容较为单一. 合理使用线程池带来的好处: 1)降低资源消耗:重复利用已创建的线程降低线程创建和销毁造成的开销 2)提高响应速度:当任务到达时,任务可 ...

  5. java线程池分类_JAVA线程池有几种类型?

    常用的JAVA线程池有以下几种类型: 1.newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. 这种类型的线程池特点是 ...

  6. java线程池中断处理_Java线程中断机制

    1. 引言 对Java中断没有一个全面的了解,可能会误以为被中断的线程将立马退出运行,但事实并非如此.中断机制是如何工作的?捕获或检测到中断后,是抛出InterruptedException还是重设中 ...

  7. java线程池参数_java线程池参数设置原则,如何设置线程池参数比较合理?

    线程池的参数应该怎样设置呢?相信对于很多的人来说这也是一个比较难的问题,下面就让我们一起来解决一下,究竟应该如何设置线程池的参数才是最合理的吧! 首先在设置参数的时候,有以下的几点是我们需要考虑到的! ...

  8. java 线程池技术_Java线程池技术以及实现

    public class DefaultThreadPool implements ThreadPool{//线程池最大限制数 private static final int MAX_WORKER_ ...

  9. java 线程池扩容_java线程池自动扩容

    线程池构造方法有几个重要参数: public ThreadPoolExecutor(int corePoolSize,//核心线程数 int maximumPoolSize,//最大线程数 long ...

  10. mysql 线程池 下载_java线程池实现批量下载文件

    本文实例为大家分享了java线程池实现批量下载文件的具体代码,供大家参考,具体内容如下 1 创建线程池 package com.cheng.webb.thread; import java.util. ...

最新文章

  1. 你管这破玩意叫 CPU ?
  2. 数字证书及网络加解密原理
  3. c# 多态实现_虚方法
  4. 仪表指针样式_PS教程!教你绘制拟物仪表盘拟物图标
  5. vue 只在父级容器移动_Vue易遗忘的基础复习(二)
  6. http1.0 和 http1.1 主要区别
  7. 网鼎杯2020php反序列化,2020-网鼎杯(青龙组)_Web题目 AreUserialz Writeup
  8. java学习5-jar包的下载以及导入
  9. js 的基础知识变量
  10. Vue 动画的封装
  11. 黑客Windows攻防初级知识点整合
  12. CSRF(跨站请求伪造)
  13. 银耳椰椰——Alpha冲刺Day08
  14. 计算机类英文参考文献,计算机英文参考文献.doc
  15. tampermonkey脚本php,Tampermonkey挂机脚本常用代码片段
  16. 使用 Python 全栈打造淘宝客微信机器人
  17. vcs dump vpd的方法
  18. PDF文件带有密码如何解除?
  19. vue下载Excel,word
  20. java计算机毕业设计Vue框架龙猫宠物交易平台MyBatis+系统+LW文档+源码+调试部署

热门文章

  1. Elasticsearch之settings和mappings(图文详解)
  2. spring学习笔记(六)
  3. C# WinForm 给某动态控件设置 IsBalloon = true的ToolTip 即 气泡状提示
  4. [转载] Python3.X之——卷积计算
  5. 手机编程环境初尝试-用AIDE开发Android应用
  6. 【Linux】解决用vi修改文件,保存文件时,提示“readonly option is set”
  7. redis运行状态图形化监控工具 — RedisLive
  8. less(css)语言快速入门
  9. mybatis~动态SQL(1)
  10. js渐渐入门之懒人框架- laz.js