C#中的多线程 [英] Multithreading in C#

查看:127
本文介绍了C#中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下示例。



I am using following example .

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}





就是这样的。



输出:



It works like this.

output :

Before start thread
Hello world 0
Hello world 0
Hello world 1
Hello world 1
Hello world 2
Hello world 2
Hello world 3
Hello world 3
Hello world 4
Hello world 4
Hello world 5
Hello world 5
Hello world 6
Hello world 6
Hello world 7
Hello world 7
Hello world 8
Hello world 8
Hello world 9
Hello world 9





从第一个打开i循环并睡眠1秒转到第二个用于循环。它打印i从第二个循环睡眠1秒并移动到第一个。我不希望它在每次打印后都进行上下文切换。我想在打印一些值后的一段时间之后这样想,睡觉在那个时间段内的某个时间从第二个循环打印i的值。在该过期之后再次移回第一个循环.Ex:它假设从第一个循环打印从1到2-10并且睡眠移动到秒,它可以从第二个打印1到2-10循环。并移动到第一个循环。来自第一个循环的i的值可能不同于第二个。



我尝试过:





It print i from first for loop and sleep for 1 sec move to second for loop.It prints i from second for loop sleep for 1 sec and move to first.I don't want it to context switch after every i print.I want like this after certain time after printing some value of i,sleep for some time within that period print value of i from second loop.After that expire again move back to first loop.Ex: it suppose to print from 1 to 2-10 from first for loop and sleep move to second,it may print 1to 2-10 from second for loop.and move to first loop.Value of i from from first loop may not be same for second.

What I have tried:

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

推荐答案

首先,睡眠(1)不是睡一秒钟 - 它是睡眠一毫秒 - 千分之一秒。



其次,两个线程执行相同的代码,但它们不共享变量:

First off, Sleep(1) is not "sleep for one second" - it's "sleep for one millisecond" - a thousandth of a second.

Secondly, the two thread execute the same code, but they don't share variables:
for (int i = 0; i < 10; i++)

创建的新实例i 在每个线程中,所以它们将具有不同的值并且不会相互影响。

您无法保证在任何特定时间运行哪个线程 - 这完全取决于操作系统,这将是只要核心变得可用,就会随机选择不等待某些东西的线程(如睡眠定时器) - 这意味着如果你有足够的内核并且系统不是太忙,它们实际上会同时毁掉它们:如果你想控制在多线程系统中发生的事情,你有责任明确地实现它,最常见的是使用semapho res。



如果你想让第二个线程等待第一个线程,你需要让它等到第一个线程为它做好准备 - 这意味着它们不能共享相同的代码,或者线程1将最终等待自己完成!



并且使其变得更糟......主要方法没有被执行这些线程,所以当它到达最后你的应用程序将关闭,你将看不到任何东西...



我建议你回到你的课程笔记并再次阅读 - 这是一个复杂的主题,你似乎还没有正确掌握它。

creates a new instance of i in each thread, so they will have different values and will not affect each other.
You cannot guarantee which thread will run at any specific time - that is totally up to the operating system, which will pick threads which aren't waiting for something (like the Sleep timer) effectively at random as soon as a core becomes available - which means they can actually ruin at the same time if you have sufficient cores and the system isn't too busy: if you want to control what happens when in a multithreaded system, you are responsible for explicitly make it happen, most often with semaphores.

If you want thread two to wait for thread one, you need to make it wait until thread one is ready for it - and that means they can't share the same code, or thread 1 will end up waiting for itself to finish!

And to make it worse ... the main method is not being executed on either of those threads, so when it gets to the end your app will close and you won't see anything...

I suggest you go back to your course notes and read them again - this is a complicated subject and you don't seem to have grasped it properly yet.


这篇关于C#中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆