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 ...
随机推荐
- until与till的用法归纳
until与till的用法归纳 崔荣斌 until和till两者都可作介词.连词,一般情况下可以互换使用.用于肯定句时,主句的动词只用延续性的,它所表示的动作一直延续到till或until表示的时间为 ...
- UDT: Breaking the Data Transfer Bottleneck
http://udt.sourceforge.net/ DT is a reliable UDP based application level data transport protocol for ...
- [MongoDB] Insert, find -- 1
MongoDB is JSON Document: How to start MongoDB client: mongod //start the server mongo // start the ...
- 给指针malloc分配空间后就等于数组吗?
首先回答这个的问题:严格的说不等于数组,但是可以认为它是个数组一样的使用而不产生任何问题.不过既然这样,那它应该算是个数组吧.所以,一般我们都用“动态数组”这种名字来称呼这种东西. 要讲清楚这个东西, ...
- 终端I/O之行控制函数
下列4个函数提供了终端设备的行控制能力.其中,filedes引用一个终端设备,否则出错返回,errno设置为ENOTTY. #include <termios.h> int tcdrain ...
- 【转】winform退出代码:Application.Exit和Environment.Exit(0)
Application.Exit和Environment.Exit(0)有什么退出方面的区别吗? Application.Exit:通知winform消息循环退出.会在所有前台线程退出后,退出应用 强 ...
- Jquery实现文本框获取焦点清空内容,失去焦点重新获得内容的公共函数
最近在做一个同城交友网站你给我吧(www.niyuewo.com)时写的一个函数与大家分享: //清除input中的内容 $(document).ready(function(){ $("i ...
- linxu,window系统
window下:net start VMwareHostdVMAuthdServiceVMUSBArbService"VMware NAT Service"VMnetDHCP #启 ...
- QQ上传大文件为什么这么快
今天和同事在群里讨论“QQ上传大文件/QQ群发送大文件时,可以在极短的时间内完成”是如何做到的. 有时候我们通过QQ上传一个几百M的文件,竟然只用了几秒钟,从带宽上限制可以得出,实际上传文件是不可能的 ...
- 1.7.4 Query Syntax and Parsing
1. 查询语法和解析 这部分主要说明了如何指定被使用的查询解析器.同样描述了主查询解析器的支持的语法和功能.同时还描述了在特定环境下使用的其他查询解析器.这里有一些普通查询解析器都能使用的参数,将会在 ...