c#線程順序執行


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace 線程同步
{
    class Program
    {
        static int num = 1;
        static void Main(string[] args)
        {
            test2();        
        }

        //線程順序執行方法1

        static void test1()
        {
            Action act = () =>
            {
                num++;
                Console.WriteLine(num);
            };



            Task.Factory.StartNew(act).ContinueWith(o => act()).ContinueWith(o=>act());

            Console.ReadLine();
        }


        //線程順序執行方法2
        static void test2()
        {
            Action act = () =>
            {
                Console.WriteLine("this is 1");
            
            };


            Action act2 = () =>
            {
               
                Console.WriteLine("this is 2");
            };


            Action act3 = () =>
            {
                
                Console.WriteLine("this is 3");
            };

            Thread t1 = new Thread(new ThreadStart(act));
            Thread t2 = new Thread(new ThreadStart(act2));
            Thread t3 = new Thread(new ThreadStart(act3));

            t1.Start();
            t2.Start();
            t2.Join(); //主線程停止,執行t2
            t3.Start();
            t3.Join(); //主線程停止,執行t3

            Console.ReadLine();
        }

    }




}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM