我们如何并行运行两个线程

本文关键字:线程 运行 两个 并行 何并行 我们 | 更新日期: 2023-09-27 18:18:53

public class Program
{
    static void Main(string[] args)
    {
        var myThread = new TestThread();
        Thread t = new Thread(new ThreadStart(myThread.PrintName));
        Thread t1 = new Thread(new ThreadStart(myThread.PrintType));
        t.Start();
        t1.Start();
        Console.Read();
    }
}
public class TestThread
{
    public void PrintName()
    {
        for (int i = 1; i <= 50; i++)
        {
            Console.WriteLine("Name {0}", i);
        }
    }
    public void PrintType()
    {
        for (int i = 100; i <= 180; i++)
        {
            Console.WriteLine("Type {0}", i);
        }
    }
}

这里我如何修复它显示,我可以生成输出顺序,即首先输出Name,然后只输出Type…我还想知道在线程中使用Lock() ?我在哪里可以找到好的例子。我是初学者在线程和需要v.s simple示例。

我们如何并行运行两个线程

试试这个:

var myThread = new TestThread();
var x=Task.Factory.StartNew(() => myThread.PrintName());
x.ContinueWith(p => PrintType());
x.Wait();

你可以看看这个

有很多其他的文章,只要谷歌"介绍线程c#"。

线程的目的是允许事情同时发生。如果你想让事情一个接一个地发生(即顺序地),那么不要使用线程:

var obj = new TestThread();      
obj.PrintName();
obj.PrintType();
Console.Read(); 

或者把PrintName和PrintType放到同一个线程中,以保持UI的响应性:

var myThread = new TestThread();
Thread t = new Thread(new ThreadStart(
    () => { 
        myThread.PrintName(); myThread.PrintType();
    } 
));
t.Start();
// Do things in the UI meanwhile
for (i = 0; i < 100; i++) {
    Console.WriteLine("UI thread {0}", i);
}
Console.Read();
//This will run two operation in sequence.   
public class TestThread
{
    public object obj = new object();
    public void PrintName()
    {
        Monitor.Enter(obj);
        for (int i = 1; i <= 50; i++)
        {
            Console.WriteLine("Name {0}", i);
        }
        Monitor.Exit(obj);
    }
    public void PrintType()
    {
        Monitor.Enter(obj);
        for (int i = 100; i <= 180; i++)
        {
            Console.WriteLine("Type {0}", i);
        }
        Monitor.Exit(obj);
    }
}

这就行了,你应该仔细阅读并尝试自己用3个循环来完成:

private static void SimpleLockTest()
        {
            Task[] myTasks = new Task[2];
            myTasks[0] = Task.Factory.StartNew(() =>
            {
                LockTestThreadOne();
            });
            myTasks[1] = Task.Factory.StartNew(() =>
            {
                LockTestThreadTwo();
            });
            Task.WaitAll(myTasks);
            Console.WriteLine("Done, press ENTER to quit");
            Console.ReadLine();
        }
        private static object locker = new object();
        private static void LockTestThreadOne()
        {
            Monitor.Enter(locker);
            for (int i = 1; i <= 50; i++)
            {
                Console.WriteLine("Name {0}", i);
                Monitor.Pulse(locker);
                Monitor.Wait(locker);                    
            }
            Monitor.Exit(locker);            
        }
        private static void LockTestThreadTwo()
        {
            Monitor.Enter(locker);
            for (int i = 100; i <= 180; i++)
            {
                Console.WriteLine("Type {0}", i);
                Monitor.Pulse(locker);
                Monitor.Wait(locker, 10);                    
            }
            Monitor.Exit(locker);
        }