Thread.Join & Thread.IsAlive functions

Join blocks the current thread and makes it wait until the thread on which Join method is invoked completes.Join method also has a overload where we can specify the timeout. If we don't specify the timeout the calling thread waits indefinitely(无限期地), until the thread on which Join() is invoked completes. This overloaded Join(int millisecondesTimeout) method returns boolean. True if the thread has terminated(终止) otherwise false.

Join is particularly(特别地) useful when we need to wait and collect result from a thread execution or if we need to do some clean-up after the thread has completed.

IsAlive returns boolean. True if the thread is still executing otherwise false.

public static void Main(string[] args)
{
Console.WriteLine("main start");
Thread t1 = new Thread(ThreadFunction1);
t1.Start();
Thread t2 = new Thread(ThreadFunction2);
t2.Start();
if(t1.Join())
{
Console.WriteLine("threadfunction1 end");
}
else
{
Console.WriteLine("threadfunction1 still working");
}
if(t1.IsAlive)
{
for (int i = ; i < ; i++)
{
Console.WriteLine("threadfunction1 still working...");
Thread.Sleep();
}
}
Console.WriteLine("main end");
} public static void ThreadFunction1()
{
Console.WriteLine("threadfunction1 start");
Thread.Sleep();
Console.WriteLine("threadfunction1 end");
} public static void ThreadFunction2()
{
Console.WriteLine("threadfunction2 start");
}

Part 92 Significance of Thread Join and Thread IsAlive functions的更多相关文章

  1. Java多线程Thread.yield(),thread.join(), Thread.sleep(200),Object.wait(),Object.notify(),Object.notifyAll()的区别

    Thread.yield(),在某个线程里调用Thread.yield(),会使这个线程由正在运行的running状态转变为等待cpu时间片的runable状态.join()是Thread类的一个非s ...

  2. c++并发编程之thread::join()和thread::detach()

    thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...

  3. thread.join 从异步执行变成同步

    Java的线程模型为我们提供了更好的解决方案,这就是join方法.在前面已经讨论过,join的功能就是使用线程 从异步执行变成同步执行 当线程变成同步执行后,就和从普通的方法中得到返回数据没有什么区别 ...

  4. 多线程:head first Thread.join()

    不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...

  5. Thread.join()方法

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

  6. Thread.join简单介绍

    百度了一下,终于明白了.这个解释最简单: 前提:join()方法肯定是被某个线程调用的.   A线程正在执行,突然执行的时候碰到了B.join(), 那么,A线程就必须要等到B线程执行完之后才能执行. ...

  7. Java Thread.join()方法

    一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...

  8. Java Thread join() 的用法

    Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...

  9. Thread .join 的用法一例

    在使用身份证读卡器时,要求 1. 身份证读到身份证 就 停止线程. 2. 关闭界面时会 自动停止调用读身份证的线程.这时候就需要用到 Thead.join 例子如下: Thread thread; p ...

随机推荐

  1. Win7中使用Eclipse连接虚拟机中的Ubuntu中的Hadoop2.4&lt;3&gt;

    经过前几天的学习,基本上能够小试牛刀编写一些小程序玩一玩了,在此之前做几项准备工作 明白我要用hadoop干什么 大体学习一下mapreduce ubuntu重新启动后,再启动hadoop会报连接异常 ...

  2. 用NDK编译lua库

    Android.mk是这样的 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := lua LOCAL_SRC_FILE ...

  3. Linq 学习

    聚合操作符                                                                                              说 ...

  4. Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造

    E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  5. SQL 32位还原位64位出现3154错误

    1:首先检查新建数据库的路径是否正确. 2:勾选覆盖原有数据库. 3:新建查询,选择master.然后新建查询中进行如下直接对bak文件的操作. RESTORE DATABASE TestFROM D ...

  6. 仿Android网易新闻客户端,并增加水平图片滑动,改进阅读体验

    仿网易新闻Android端APP 主要功能展示和代码实现 差不多花了一周的时间,目前实现的了新闻下的包括头条.体育.娱乐的一系列的新闻展示,以及点击后进入的新闻详情展示. 目前效果 目前效果请访问该网 ...

  7. Android图片圆角效果

    一般来说图片加圆角可以使用 Java 的方式来进行, 对图片略加处理即可, 但也可以使用纯XML+Nice-Patch图片来进行, 这样的速度会更快. 如果背景是纯色的情况下建议使用此方法. 原理则是 ...

  8. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  9. 为您的Android,iOS等应用加入声波传输功能

    记得12年左右的时候,美国出现了chirp应用,该应用能够使用声波在iphone手机间传输文本,图片.甚至视频.当时认为非常高大上. 再后来,到13年的时候国内也出现了非常多声波应用.比方支付宝的声波 ...

  10. 之前的Android项目报错,新建Android项目报错,代码中找不到错误解决方案

    打开一年前的东西,结果发现里面的android项目全部有个红叉,也找不到错误.新建一个项目也报错,首先确定自己的环境应该没问题,然后通过查看网上的资料,发现可能是debug的keystore到期啦. ...