`

java 并发(1) 理解Thread.join()

    博客分类:
  • Java
 
阅读更多

thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。

t.join();      //当前线程中加入线程t,当t执行完成后,当前线程才继续进行。
t.join(1000);  //等待 t 线程,等待时间是1000毫秒,无论t是否执行完,主线程1000s后都将会执行。

 

应用一 : 实现多个线程的串行化调度。代码如下:

 

package com.xx.concurrent.commonUse;

public class SerialThread {
	
	public static void main(String[] args) throws InterruptedException {
		Thread[] threads = new Thread[100];
		
		for(int i=0; i< 100; ++i){
			if (i == 0 ){
				threads[i] = new Thread(new MyThread(i,null));
			}else{
				threads[i] = new Thread(new MyThread(i,threads[i-1]));
			}
			threads[i].start();	
		}
	}
}

class MyThread implements Runnable {
	
	private int id ;
	
	private Thread priorThread ;
	
	public MyThread(int id,Thread priorThread ){
		this.id = id ;
		this.priorThread = priorThread;
	}
	
	@Override
	public void run() {
		if (priorThread != null){
			try {
				priorThread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Thread-" + this.id + " is running over .....");
	}
}

 输出结果:

 

Thread-0 is running over .....
Thread-1 is running over .....
Thread-2 is running over .....
Thread-3 is running over .....
Thread-4 is running over .....
Thread-5 is running over .....
Thread-6 is running over .....
Thread-7 is running over .....
Thread-8 is running over .....
Thread-9 is running over .....
Thread-10 is running over .....
Thread-11 is running over .....
Thread-12 is running over .....
Thread-13 is running over .....
Thread-14 is running over .....
Thread-15 is running over .....
Thread-16 is running over .....
Thread-17 is running over .....
Thread-18 is running over .....
Thread-19 is running over .....
Thread-20 is running over .....
Thread-21 is running over .....
Thread-22 is running over .....
Thread-23 is running over .....
Thread-24 is running over .....
Thread-25 is running over .....
Thread-26 is running over .....
Thread-27 is running over .....
Thread-28 is running over .....
Thread-29 is running over .....
Thread-30 is running over .....
Thread-31 is running over .....
Thread-32 is running over .....
Thread-33 is running over .....
Thread-34 is running over .....
Thread-35 is running over .....
Thread-36 is running over .....
Thread-37 is running over .....
Thread-38 is running over .....
Thread-39 is running over .....
Thread-40 is running over .....
Thread-41 is running over .....
Thread-42 is running over .....
Thread-43 is running over .....
Thread-44 is running over .....
Thread-45 is running over .....
Thread-46 is running over .....
Thread-47 is running over .....
Thread-48 is running over .....
Thread-49 is running over .....
Thread-50 is running over .....
Thread-51 is running over .....
Thread-52 is running over .....
Thread-53 is running over .....
Thread-54 is running over .....
Thread-55 is running over .....
Thread-56 is running over .....
Thread-57 is running over .....
Thread-58 is running over .....
Thread-59 is running over .....
Thread-60 is running over .....
Thread-61 is running over .....
Thread-62 is running over .....
Thread-63 is running over .....
Thread-64 is running over .....
Thread-65 is running over .....
Thread-66 is running over .....
Thread-67 is running over .....
Thread-68 is running over .....
Thread-69 is running over .....
Thread-70 is running over .....
Thread-71 is running over .....
Thread-72 is running over .....
Thread-73 is running over .....
Thread-74 is running over .....
Thread-75 is running over .....
Thread-76 is running over .....
Thread-77 is running over .....
Thread-78 is running over .....
Thread-79 is running over .....
Thread-80 is running over .....
Thread-81 is running over .....
Thread-82 is running over .....
Thread-83 is running over .....
Thread-84 is running over .....
Thread-85 is running over .....
Thread-86 is running over .....
Thread-87 is running over .....
Thread-88 is running over .....
Thread-89 is running over .....
Thread-90 is running over .....
Thread-91 is running over .....
Thread-92 is running over .....
Thread-93 is running over .....
Thread-94 is running over .....
Thread-95 is running over .....
Thread-96 is running over .....
Thread-97 is running over .....
Thread-98 is running over .....
Thread-99 is running over .....

 Thread中的join源码如下:

/**
     * Waits at most <code>millis</code> milliseconds for this thread to 
     * die. A timeout of <code>0</code> means to wait forever. 
     *
     * @param      millis   the time to wait in milliseconds.
     * @exception  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 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方法首先判断线程t的状态,线程状态为已启动状态下,才有效,因此t.join()一般在t.start()之后。主线程t.join时候,会获得线程对象t的锁(wait 意味着拿到该对象的锁),或者调用该对象的wait(等待时间),直到该对象唤醒主线程。可以看出t线程的运行结束即可唤醒主线程。

分享到:
评论

相关推荐

    13-Java并发编程学习宝典.zip

    Java并发编程是软件开发中的重要领域,特别是在大型系统和高并发场景中不可或缺。"13-Java并发编程学习宝典.zip" 包含了一系列关于Java并发编程的学习资源,旨在帮助开发者掌握多线程编程的核心技术和最佳实践。以下...

    Java并发编程学习笔记.

    2. **并发基础**:Java并发编程的基础包括`Thread.start()`启动线程,`Thread.join()`等待线程结束,以及`Thread.sleep()`让当前线程暂停一段时间。 3. **同步机制**:Java提供了多种同步机制,如`synchronized`...

    ( Java并发程序设计教程.zip )高清版 PDF

    《Java并发程序设计教程》是一本深入探讨Java平台上的并发编程技术的专业书籍。并发和多线程是现代软件开发中的核心概念,特别是在Java这样的多线程支持强大的编程语言中。这本书详细介绍了如何在Java环境中有效地...

    《Java 并发编程实战》.zip

    《Java 并发编程实战》是一本专注于Java并发编程领域的权威书籍,旨在帮助开发者深入理解和掌握在多线程环境中编写高效、安全且可维护的代码。这本书涵盖了Java并发编程的基础概念,高级特性以及最佳实践,是Java...

    Java并发编程全景图.pdf

    为了深入理解Java并发编程,有必要了解其核心技术点和相关实现原理,以下将详细介绍文件中提及的关键知识点。 1. Java同步原语 Java提供了多种同步原语来保证线程安全,主要包括关键字volatile、synchronized以及...

    java 中Thread.join()的使用方法

    在Java编程语言中,`Thread.join()`方法是一个非常重要的同步工具,它允许一个线程(调用者)等待另一个线程(被调用者)执行完成。`Thread.join()`的使用能够有效地解决多线程环境中的顺序执行问题,确保线程间的...

    Java 多线程与并发-Java并发知识体系详解.pdf

    总的来说,Java并发编程是一个复杂但重要的主题,需要深入理解线程基础、同步机制、并发工具类等知识,才能编写出高效、稳定的多线程程序。在实际开发中,应根据具体场景选择合适的并发控制手段,合理利用Java提供的...

    JAVA并发编程实战.pdf

    根据提供的文件信息:“JAVA并发编程实战.pdf”,我们可以深入探讨与Java并发编程相关的多个核心知识点。...希望通过对上述知识点的学习和实践,能够加深对Java并发编程的理解,并能在实际工作中灵活运用。

    javathread.part05.rar

    线程池是Java并发编程中的重要工具,`java.util.concurrent`包下的`ExecutorService`和`ThreadPoolExecutor`提供了线程池的实现。通过线程池可以有效地管理线程,避免频繁创建和销毁线程带来的开销。`...

    Java 多线程与并发(2-26)Java 并发 - 线程基础.pdf

    Java多线程与并发是Java编程中至关重要的一个领域,特别是在构建高性能、高并发的应用时。线程基础是理解并发编程的关键,它涉及到线程的状态转换、创建与使用方式、同步与协作机制等方面。 线程有五种基本状态: 1...

    java并发实战中文文档

    《Java并发实战》是一本深度剖析Java并发编程的权威指南,旨在帮助开发者高效地理解和解决多线程环境下的编程挑战。文档清晰明了,配备有详细的目录,使得学习过程更为顺畅,避免了在查找信息上浪费时间。标签“Java...

    javathread.part03.rar

    `javathread.part03.rar`这个压缩包文件很可能包含了关于Java线程深入理解和实践的资源,可能是代码示例、教程文档或者课件。在这个部分,我们将探讨Java线程的一些关键知识点。 1. **线程创建**: Java提供了两种...

    JAVA并发编程实践.pdf

    根据提供的信息,“JAVA并发编程实践.pdf”这一文档主要聚焦于Java并发编程的实践与应用,这对于希望深入了解并行处理和多线程技术的...希望通过对以上知识点的学习,能够帮助开发者更好地理解和运用Java并发编程技术。

    javathread.part104.rar

    以上只是部分Java线程编程的核心知识点,`javathread.part104.rar`中可能还会包含更多高级主题,如线程池的配置与优化、线程通信(例如`join()`, `Thread.sleep()`, `yield()`)以及并发工具类的深入探讨。...

    javathread.part02.rar

    总之,`javathread.part02.rar`可能涵盖了以上所述的Java线程相关知识点,对于学习和理解Java并发编程具有很高的价值。通过深入研究这个压缩包中的内容,开发者可以更好地掌握Java线程的使用,提高多线程环境下的...

    并发编程面试专题.pdf

    ### 并发编程面试专题知识点解析 #### 一、线程执行顺序控制 **知识点:** 1. **问题描述:** 如何确保线程T2在T1执行完毕后开始执行,T3在T2执行完毕后开始执行? 2. **解决方案:** 可以通过`Thread.join()`方法...

    java并发编程书籍

    Java并发编程是软件开发中的一个关键领域,尤其是在大型企业级应用和分布式系统中。通过学习相关的书籍,开发者可以深入理解如何有效地设计和实现高效的多线程应用程序,避免并发问题,如竞态条件、死锁、活锁等。...

    java虚拟机并发编程.pdf

    1. **线程基础**:书中会讲解如何创建和管理Java线程,包括Thread类和Runnable接口的使用,以及线程的生命周期状态(新建、就绪、运行、阻塞和死亡)。此外,还会介绍守护线程和线程组的概念。 2. **同步机制**:...

    Java 并发.pdf_电子版pdf版

    Java并发编程是Java开发中不可或缺的一部分,特别是在处理高并发、多线程的互联网应用时尤为重要。本篇将详细解析Java并发中的线程状态转换、线程的使用方式以及相关概念。 一、线程状态转换 Java线程在执行过程中...

Global site tag (gtag.js) - Google Analytics