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 ...
随机推荐
- .cmd文件不小心管理记事本打开的恢复
比如不小心将.cmd文件关联成用记事本打开了,此时须要删除注冊表: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explor ...
- C语言统计一个字符串中单词的个数
假定每一个单词用空格隔开. 样例: 输入:how are you! 输出:3 两种方法: 一: #include <stdio.h> #include <string.h> # ...
- Android---3种方式限制EditView输入字数(转载)
方法一:利用TextWatcher editText.addTextChangedListener(new TextWatcher() { private CharSequence temp; pri ...
- 为您的Android,iOS等应用加入声波传输功能
记得12年左右的时候,美国出现了chirp应用,该应用能够使用声波在iphone手机间传输文本,图片.甚至视频.当时认为非常高大上. 再后来,到13年的时候国内也出现了非常多声波应用.比方支付宝的声波 ...
- WebService究竟是什么?
一.序言 大家或多或少都听过WebService(Web服务),有一段时间非常多计算机期刊.书籍和站点都大肆的提及和宣传WebService技术,当中不乏非常多吹嘘和做广告的成分.可是不得不承认的是W ...
- [Oracle] - 性能优化工具(2) - ASH
ASH和AWR的关系 ASH以V$SESSION为基础,每秒採样一次,记录活动会话等待的事件.不活动的会话不会採样,採样工作由新引入的后台进程MMNL来完毕. ASH内存记录数据始终是有限的,为了保存 ...
- docker + swarm 集群
docker + swarm 集群 导读 Swarm是Docker公司在2014年12月初新发布的容器管理工具.和Swarm一起发布的Docker管理工具还有Machine以及Compose.Swar ...
- 编译linux内核以及depmod的使用
转载:http://blog.lmtw.com/b/18215/archives/2010/71074.html depmod(depend module) 功能说明:分析可载入模块的相依性. 语 法 ...
- 来自 Github 的图形化 Git 使用教程
转载:http://www.linuxeden.com/html/news/20120628/126451.html 这是来自 Github 上对 Git 常用操作进行简短介绍以及可视化图形操作说明的 ...
- TCP/IP协议原理与应用笔记22:静态和动态路由选择
1. 静态路由选择: Static routing 管理员手工设置 管理员手动更新 不能保证路由的一致性和及时性 管理性强 没有路由开销 小型.变化缓慢网络 2. 动态路由选择 Dynamic rou ...