for*_*yez 63 c# multithreading
我在网上看到它说myThread.Join();当我想阻止我的线程直到另一个线程完成时我使用它.(如果我有多个线程,那么我没有得到的一件事就是如何).
但一般来说,我只是在我使用时.Join()或者对它有用的条件时才会得到.任何人都可以向我解释一下,就像我是四年级学生一样吗?理解的简单解释将得到我的回答投票.
J.D*_*.D. 68
假设你想要启动一些工作线程来执行某种计算,然后用所有结果做一些事情.
List<Thread> workerThreads = new List<Thread>();
List<int> results = new List<int>();
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(() => {
Thread.Sleep(new Random().Next(1000, 5000));
lock (results) {
results.Add(new Random().Next(1, 10));
}
});
workerThreads.Add(thread);
thread.Start();
}
// Wait for all the threads to finish so that the results list is populated.
// If a thread is already finished when Join is called, Join will return immediately.
foreach (Thread thread in workerThreads) {
thread.Join();
}
Debug.WriteLine("Sum of results: " + results.Sum());
Run Code Online (Sandbox Code Playgroud)
哦,是的,不要像那样使用Random,我只是想写一个简单易懂的例子.如果你在时间上创建新的Random实例,它最终不会是随机的,因为种子是基于时钟的.
Mit*_*eat 19
在下面的代码片段中,主线程调用Join(),使其等待所有生成的线程完成:
static void Main()
{
Thread regularThread = new Thread(ThreadMethod);
regularThread.Start();
Thread regularThread2 = new Thread(ThreadMethod2);
regularThread2.Start();
// Wait for spawned threads to end.
regularThread.Join();
Console.WriteLine("regularThread returned.");
regularThread2.Join();
Console.WriteLine("regularThread2 returned.");
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您还从线程池中旋转了一个线程(例如,使用QueueUserWorkItem),则Join不会等待该后台线程.您需要实现一些其他机制,例如使用AutoResetEvent.
对于线程的优秀介绍,我建议在C#中阅读Joe Albahari的免费线程
Els*_*han 12
这是一个非常简单的程序,用于演示Thread的用法Join.请按照我的意见进行更好的理解.按原样编写这个程序.
using System;
using System.Threading;
namespace ThreadSample
{
class Program
{
static Thread thread1, thread2;
static int sum=0;
static void Main(string[] args)
{
start();
Console.ReadKey();
}
private static void Sample() { sum = sum + 1; }
private static void Sample2() { sum = sum + 10; }
private static void start()
{
thread1 = new Thread(new ThreadStart(Sample));
thread2 = new Thread(new ThreadStart(Sample2));
thread1.Start();
thread2.Start();
// thread1.Join();
// thread2.Join();
Console.WriteLine(sum);
Console.WriteLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
1.第一次按原样运行(带注释):然后结果将为0(初始值)或1(当线程1完成时)或10(或线程完成)
2.运行删除注释thread1.Join():结果应始终大于1.因为thread1.Join()在获得总和之前,应该完成触发和线程1.
3.运行去除所有内容:结果应始终为11
Lor*_*nzo 10
Join主要用于在您使用代码进行处理之前需要等待一个线程(或一堆线程)终止的情况.
因此,当您需要从线程执行中收集结果时,这也特别有用.
根据下面的Arafangion评论,如果您在创建线程后需要执行一些清洁/内务代码,则加入线程也很重要.
| 归档时间: |
|
| 查看次数: |
70782 次 |
| 最近记录: |