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

时间:2011-12-26 16:28:46

标签: c#

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.simple示例。

5 个答案:

答案 0 :(得分:1)

您可以查看this

还有其他多篇文章,只是谷歌的'引入线程c#'。

答案 1 :(得分:1)

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

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();

答案 2 :(得分:1)

试试这个:

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

答案 3 :(得分:0)

//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);
    }
}

答案 4 :(得分:0)

这样就可以了,你应该仔细阅读并尝试自己做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);
        }