zoukankan      html  css  js  c++  java
  • C# 线程锁 Lock ,锁对象string字符串

    string 字符锁

        class ThreadClass 
        {
            string str;
            string path;
            public ThreadClass(string str,string path)
            {
                this.str = str;
                this.path = path;
            }
            public void Thread01()
            {
                lock (str) {
                    for (int i = 0; i < 10;i++ )
                    {
                        Console.WriteLine(path+i);
                    }
                }
            }
        }
        class Program
        {
            static string str01 = "name";
            static string str02 = "name";//这里锁对象是字符串会有不同效果
            static void Main(string[] args)
            {
                ThreadClass tc = new ThreadClass(str01,"这是线程1 ");
                Thread th1 = new Thread(new ThreadStart(tc.Thread01));
                th1.Start();
                //---------------------------------------------------------
                ThreadClass tc2 = new ThreadClass(str02, "这是线程2 ");
                Thread th2 = new Thread(new ThreadStart(tc2.Thread01));
                th2.Start();
    
                Console.ReadLine();
            }
             
        } 

     修改字符串执行效果不一样

        class Program
        {
            static string str01 = "name1";
            static string str02 = "name2";//这里锁对象是字符串会有不同效果
            static void Main(string[] args)
            {
                ThreadClass tc = new ThreadClass(str01,"这是线程1 ");
                Thread th1 = new Thread(new ThreadStart(tc.Thread01));
                th1.Start();
                //---------------------------------------------------------
                ThreadClass tc2 = new ThreadClass(str02, "这是线程2 ");
                Thread th2 = new Thread(new ThreadStart(tc2.Thread01));
                th2.Start();
    
                Console.ReadLine();
            }
             
        } 

  • 相关阅读:
    android购物车的实现
    eclipse配置maven
    Android 高仿微信实时聊天 基于百度云推送
    如何使用Ubuntu online account API创建微博HTML5申请书
    C#创建和初始化类
    一个小的日常实践——距离阵列
    文本框中输入极限
    java阅读器hdfs单纯demo
    错误和问题解决的成本
    选择用户-保存选定的用户
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11046222.html
Copyright © 2011-2022 走看看