using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadStart thr_start_func = new ThreadStart(First_Thread);
            Console.WriteLine("Creating the first thread ");
            Thread fThread = new Thread(thr_start_func);
            fThread.Name = "first_thread";
            fThread.Start(); //starting the thread
            Thread sThread = new Thread(new ThreadStart(Second_Thread));
            sThread.Name = "second_thread";
            sThread.Start();
        }        public static void First_Thread()
        {
            Console.WriteLine("First thread created");
            Thread current_thread = Thread.CurrentThread;
            string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
            Console.WriteLine("The details of the thread are :" + thread_details);
            Console.WriteLine("first thread terminated");
        }        public static void Second_Thread()
        {
            Console.WriteLine("Second thread created");
            Thread current_thread = Thread.CurrentThread;
            string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
            Console.WriteLine("The details of the thread are :" + thread_details);
            Console.WriteLine("second thread terminated");
        }
    }
}
//如果不是,谁能提供一个简单的、多线明确的源代码示例?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace WZDM.Test
    {
        /// <summary>
        /// 限制并发线程数例程
        /// </summary>
        public class TestThread 
        {
            int _currentThreads = 0;
            int _maxThreads = 10;        public void Test()
            {
                while (true)
                {
                    //_maxThreads = new Random().Next(1, 10);
                    for (int i=0; i<100; i++)
                    {
                        // 在此进行数量限制
                        if (_currentThreads >= _maxThreads)
                            break;                    // 开启线程
                        lock (typeof(TestThread))
                        {
                            _currentThreads++;
                            if (_currentThreads > _maxThreads)
                                _currentThreads = _maxThreads;                        string currentInfo = string.Format("thread {0}/{1}", _currentThreads, _maxThreads);
                            Console.WriteLine(currentInfo + " start");
                            Thread thread = new Thread(new ParameterizedThreadStart(Run));
                            thread.Start(currentInfo);
                        }
                    }                System.Threading.Thread.Sleep(2000);
                }
            }        // 线程任务
            void Run(object obj)
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine("{0}:{1}", obj, i);
                    Thread.Sleep(100);
                }
                Console.WriteLine("{0} finished", obj);
                lock (typeof(TestThread))
                {
                    _currentThreads--;
                    if (_currentThreads < 0)
                        _currentThreads = 0;
                }
            }
        }
    }
      

  2.   


    分3个线程你的在主线程执行 这个现成会关心另外2个执行 First_Thread,Second_Thread() 执行的怎么样了,主线程序会继续执行后面的. Second_Thread是不关心 First_Thread是否执行完就会开始执行        
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;namespace thread
    {
        class Program
        {
            static void Main(string[] args)
            {
                Thread T1 = new Thread(new ThreadStart(proces.pro1));
                Thread T2 = new Thread(new ThreadStart(proces.pro2));
                T1.Start();
                T2.Start();
                T1.Join();
                T2.Join();
                Console.WriteLine("主程序运行完毕");
            }
        }    public static class proces
        {
            public static void pro1()
            {
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(1);
                    Console.WriteLine("线程1运行中...");
                }
                Console.WriteLine("线程1运行完毕...");
            }        public static void pro2()
            {
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(1);
                    Console.WriteLine("线程2运行中...");
                }
                Console.WriteLine("线程2运行完毕...");
            }
        }
    }
    两个任务同时进行,主线程等两个子任务完成再结束整个程序。
      

  4.   

    问题继续:线程的并发,我的理解就是线程的同步问题。我在网上看到的资料说,线程同步容易产生的两个问题:1、死锁;2、脏数据;我的理解是这两个问题的解决,应该是通过管理解决的。听有经验的人讲,多线程中,存在主线程。那在帖子的这段代码中,启动first和second的main是不是主线程?另外,解决脏数据的方法是对公共数据操作部分加锁,如果要实现加锁,是不是也应该在主线程中做?