zoukankan      html  css  js  c++  java
  • C#多线程,线程锁

    /*
     * User: Administrator
     * Email:798033502@qq.com
     * Date: 2013-7-18
     * Time: 22:54
     */
    using System;
    using System.Threading;

    namespace ThreadDome
    {
        class Program
        {
            //创建一个线程锁标识
            private static object threadLock = new object();
            public static void Main(string[] args)
            {
                //使10个线程全部指向同一个方法
                Thread [] threads = new Thread[10];
                for (int i = 0; i < 10; i++) {
                    threads[i]= new Thread(new ThreadStart(PrintNumbers));
                    threads[i].Name = string.Format("Worker thread{0}",i);
                    //设置为后台线程
                    threads[i].IsBackground =true;
                }
                //启动线程
                foreach (Thread t  in threads) {
                    t.Start();
                }
                
                Console.ReadKey();
            }
            
            public static  void PrintNumbers()
            {
                lock(threadLock)
                {
                    Console.WriteLine("线程——>{0}正在执行 ",Thread.CurrentThread.Name);
                    
                    for (int i = 0; i < 100; i++) {
                        Random r = new Random ();
                        Thread.Sleep(60*r.Next(6));
                        Console.WriteLine("{0}",i);
                    }
                    Console.WriteLine();
                }
            }
        }
    }

  • 相关阅读:
    搭建 Linux 下 GitLab 服务器(转)
    sql语法:inner join on, left join on, right join on具体用法
    Android Studio之同一应用创建多个Activity(一)
    java环境变量配置
    老鸟的Python新手教程
    域名注冊以及域名解析设置
    Android在WebView上构建Web应用程序
    利用JasperReport+iReport进行Web报表开发
    android App Widgets
    多数据库下activiti的流程定义缓存问题
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3201273.html
Copyright © 2011-2022 走看看