繁体   English   中英

如何立即杀死所有线程

[英]How to kill all threads immediately

我编写了一个小示例程序,该程序使用两个线程对数字进行计数。 它还在计数旁边打印第二个数字,因此我可以看到哪个线程打印了哪个数字。

现在,我的目标是使两个线程中的一个计数为7后立即停止。我不知道该如何处理。 我考虑过将线程数组作为参数传递给Counter ,然后使用foreach循环中止两个线程。 问题是t0可能正在执行它并调用t0.Abort() ,因此t1.Abort()调用t0.Abort()

public static int count = 0;
private static object lockObject = new object();

static void Main(string[] args) {
    Thread t0 = new Thread(() => Counter(1, 10));
    Thread t1 = new Thread(() => Counter(10, 20));

    t0.Start();
    t1.Start();

    t0.Join();
    t1.Join();

    Console.ReadLine();
}

public static void Counter(int k, int m) {
    for(int i = k; i < m; i++) {
        lock (lockObject) {
            count++;
            Console.WriteLine(count + " " + i);
            if (i == 7) {
                /* 
                 * Code that will kill thread t0 and t1
                 */
            }
        }
    }
}

输出应该是这样的

1 1
2 2
3 10
4 11
5 12
6 13
7 14

有人建议如何同时杀死t0t1吗?

使用一个标志,在这种情况下为“运行”。

public static int count = 0;
private static object lockObject = new object();
static bool run = true;

static void Main(string[] args) {
    Thread t0 = new Thread(() => Counter(1, 10));
    Thread t1 = new Thread(() => Counter(10, 20));        

    t0.Start();
    t1.Start();

    t0.Join();
    t1.Join();

    Console.ReadLine();
}

public static void Counter(int k, int m) {
    for(int i = k; i < m && run; i++) {
        lock (lockObject) {
            count++;
            Console.WriteLine(count + " " + i);
            if (i == 7) {
                run = false;
            }
        }
    }
}

看看ManualResetEvent。 您应该创建一个方法,并在Counter-method的for循环内检查事件是否已设置,如果已设置,则中断for循环并返回。 如果达到7,则只需设置事件。 该事件是线程安全的。

顺便说一句,这似乎是大学的功课;)下次,请自己尝试。

public static int count = 0;
private static object lockObject = new object();
private ManualResetEvent finish = new ManualResetEvent(false);

static void Main(string[] args) {
    Thread t0 = new Thread(() => Counter(1, 10));
    Thread t1 = new Thread(() => Counter(10, 20));

    t0.Start();
    t1.Start();

    t0.Join();
    t1.Join();

    Console.ReadLine();
}

public static void Counter(int k, int m) {
    for(int i = k; i < m; i++) {

        lock (lockObject) {
            if (finish.waitOne(0))
                break;

            count++;
            Console.WriteLine(count + " " + i);
            if (i == 7)
                finish.set()
        }
    }
}

MSDN不知道什么是ManualResetEvent.isset(),但是Matthias的答案看起来是正确的。 如果将isset()替换为WaitOne(0) ,则代码可以正常工作。

private static int count = 0;
private static ManualResetEvent finish = new ManualResetEvent(false);
private static int stopValue = 7;
private static int syncInterval = 0; //0ms
private static object lockObject = new object();
static void Main(string[] args)
{
    Thread t0 = new Thread(() => Counter(1, 10));
    Thread t1 = new Thread(() => Counter(10, 20));
    t0.Name = "Thread 1";
    t1.Name = "Thread 2";

    t0.Start();
    t1.Start();

    t0.Join();
    t1.Join();

    Console.ReadKey();
}
public static void Counter(int k, int m)
{
    for (int i = k; i < m; i++)
    {
        lock (lockObject)
        {
            if (finish.WaitOne(syncInterval))
                break;
            count++;
            Console.WriteLine($"{count} {i}");
            if (count == stopValue)
            {
                finish.Set();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM