Part 92 Significance of Thread Join and Thread IsAlive functions
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的更多相关文章
- Java多线程Thread.yield(),thread.join(), Thread.sleep(200),Object.wait(),Object.notify(),Object.notifyAll()的区别
Thread.yield(),在某个线程里调用Thread.yield(),会使这个线程由正在运行的running状态转变为等待cpu时间片的runable状态.join()是Thread类的一个非s ...
- c++并发编程之thread::join()和thread::detach()
thread::join(): 阻塞当前线程,直至 *this 所标识的线程完成其执行.*this 所标识的线程的完成同步于从 join() 的成功返回. 该方法简单暴力,主线程等待子进程期间什么都不 ...
- thread.join 从异步执行变成同步
Java的线程模型为我们提供了更好的解决方案,这就是join方法.在前面已经讨论过,join的功能就是使用线程 从异步执行变成同步执行 当线程变成同步执行后,就和从普通的方法中得到返回数据没有什么区别 ...
- 多线程:head first Thread.join()
不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...
- Thread.join()方法
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B.t.join() ...
- Thread.join简单介绍
百度了一下,终于明白了.这个解释最简单: 前提:join()方法肯定是被某个线程调用的. A线程正在执行,突然执行的时候碰到了B.join(), 那么,A线程就必须要等到B线程执行完之后才能执行. ...
- Java Thread.join()方法
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- Java Thread join() 的用法
Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...
- Thread .join 的用法一例
在使用身份证读卡器时,要求 1. 身份证读到身份证 就 停止线程. 2. 关闭界面时会 自动停止调用读身份证的线程.这时候就需要用到 Thead.join 例子如下: Thread thread; p ...
随机推荐
- TMS3705A PCF7991AT 线路图
- EWARM STM32 向量表偏移设置
system_stm32f2xx.c #ifdef VECT_TAB_SRAM SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table ...
- 简短介绍 C# 6 的新特性 .net 6都要来了?
几周前我在不同的地方读到了有关C#6的一些新特性.我就决定把它们都收集到一起,如果你还没有读过,就可以一次性把它们都过一遍.它们中的一些可能不会如预期那样神奇,但那也只是目前的更新. 你可以通过下载V ...
- 【转】Delphi多线程学习(9):多线程数据库查询(ADO)
原文:http://www.cnblogs.com/djcsch2001/articles/2382559.html ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用 ...
- [原创]-CMD命令设置IP地址
问题描述 在实际工作中,尤其是像我们这种BI分析人员,在做项目的时候,时常都需要因客户的不同随时切换不同的网络环境,有时可能需要在公司和客户之间来回的穿梭.交替.问题也就随之而来:每次客户那里都需要设 ...
- Win7 IE故障:APPCRASH,d3d9.dll,c0000005
问题: 今天使用使用IE登录某网址,发现总是报错,如下图,无法浏览. 解决方案: 主要讲IE的呈现方案修改即可,如下步骤: 在IE的[Intern ...
- spring 学习总结
前几天,一直在学spring,然后又学习spring mvc ,再回过头去看spring,本来不是特别熟悉,竟然几乎全部忘记了.于是,立刻写这篇博客来总结一下.这是我写的其中一个程序,大概的逻辑流程. ...
- 下拉选择框加listview删除
package com.downselect; import java.util.ArrayList; import android.R.array; import android.app.Activ ...
- [Java] MAP、LIST、SET集合解析
在JAVA的util包中有两个所有集合的父接口Collection和Map,它们的父子关系: java.util +Collection 这个接口extends自 --java.lang ...
- 7. Android框架和工具之 android-percent-support-lib-sample(百分比支持)
1. android-percent-support-lib-sample介绍: 谷歌最新的百分比布局库的示例项目.其实LinearLayout的layout_weight也能实现百分比效果,不过这个 ...