zoukankan      html  css  js  c++  java
  • 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();
            }

        }




    }

  • 相关阅读:
    深入理解MyBatis中的一级缓存与二级缓存
    Spring-mvc文件的上传和下载
    Spring-mvc的拦截器和异常通知
    各种配置文件
    设计模式---代理模式
    dom4j读取xml和dtd的使用方式
    几种不同的路径
    常用正则表达式
    请求转发和重定向的对比
    跨浏览器检测某个节点是不是另一个节点的后代
  • 原文地址:https://www.cnblogs.com/tiancai/p/6904178.html
Copyright © 2011-2022 走看看