多线程/在C#中同时运行2个线程 [英] Multithreading / Running 2 threads simultaneously in C#

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

问题描述

我对多线程有一些困惑,关于它如何运行。



这是我的代码。



使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading;

namespace MultiThreading
{
class program
{
static void Main(string [] args)
{
Operations op = new Operations();
线程Th1 =新线程(新的ThreadStart(op.Op1));
线程Th2 =新线程(新的ThreadStart(op.Op2));
//线程Th3 =新线程(新的ThreadStart(op.Op3));
Th1.Start();
Th2.Start();
//Th3.Start();
Console.ReadLine();
}
}
class操作
{
public void Op1()
{
Console.WriteLine(线程1操作A) ;
Thread.Sleep(2000);
Op3();
Console.WriteLine(线程1操作B);
Thread.Sleep(2000);
Op3();
Console.WriteLine(线程1操作C);
}
public void Op2()
{
Console.WriteLine(Thread 2 operation A);
Thread.Sleep(2000);
Console.WriteLine(线程2操作B);
Thread.Sleep(2000);
Console.WriteLine(线程2操作C);
}
public void Op3()
{
Console.WriteLine(Do Nothing);
}
}
}







我得到的输出是

线程1操作A 
线程2操作A
线程2操作B
什么都不做
线程1操作B
什么都不做
线程1操作C
线程2操作C





然而,下一次我跑,我得到了不同的输出。任何人都可以解释为什么会发生这种情况



编辑:

如果是正常的并且调度算法决定运行哪个,那么以何种方式我可以控制线程使输出以我想要的格式显示?

解决方案

它是normal.it是调度算法谁决定运行

I am having some confusions regarding multithreading, and regarding how it runs.

This is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MultiThreading
{
    class Program
    {
        static void Main(string[] args)
        {
            Operations op = new Operations();
            Thread Th1 = new Thread(new ThreadStart(op.Op1));
            Thread Th2 = new Thread(new ThreadStart(op.Op2));
            //Thread Th3 = new Thread(new ThreadStart(op.Op3));
            Th1.Start();
            Th2.Start();
            //Th3.Start();
            Console.ReadLine();
        }
    }
    class Operations
    {
        public void Op1()
        {
            Console.WriteLine("Thread 1 operation A");
            Thread.Sleep(2000);
            Op3();
            Console.WriteLine("Thread 1 operation B");
            Thread.Sleep(2000);
            Op3();
            Console.WriteLine("Thread 1 operation C");
        }
        public void Op2()
        {
            Console.WriteLine("Thread 2 operation A");
            Thread.Sleep(2000);
            Console.WriteLine("Thread 2 operation B");
            Thread.Sleep(2000);
            Console.WriteLine("Thread 2 operation C");
        }
        public void Op3()
        {
            Console.WriteLine("Do Nothing");
        }
    }
}




The output I get is

Thread 1 operation A
Thread 2 operation A
Thread 2 operation B
Do Nothing
Thread 1 operation B
Do Nothing
Thread 1 operation C
Thread 2 operation C



However, the very next time I run, I get a varying outputs. Can anyone explain why this happens

EDIT:
If it is normal and it is the Scheduling Algorithm who decides which to run, is there any way I can control the threads to make the output appear in a format that I want ?

解决方案

it is normal.it is the Scheduling Algorithm who decides which to run.


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

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