zoukankan      html  css  js  c++  java
  • 线程同步lock and monitor

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication1
    {
        class Print
        {
            public void printNumbers()
            {
                Monitor.Enter(this);
                try
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.Write("{0}\t", i);
                    }
                    Console.WriteLine();
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }

            public void printNumberslock()
            {
                lock (this)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.Write("{0}\t", i);
                    }
                    Console.WriteLine();
                }
            }
        }

        
        class Program
        {
            static void Main(string[] args)
            {
                Print p = new Print();
                Thread[] threads = new Thread[10];

                for (int i = 0; i < 10; i++)
                {
                    threads[i] = new Thread(new ThreadStart(p.printNumbers));
                    threads[i].Name = string.Format("thread#{0}", i);
                }
                    foreach (Thread t in threads)
                    {
                        Console.WriteLine(t.Name );
                        t.Start();
                    }
            }
        }
    }

     

  • 相关阅读:
    前端三剑客:html,css,JavaScript
    Python的开发之路
    python-win32操作excel的一些特殊功能
    pillow处理图片横向与纵向的合并
    部署django+uwsgi+Virtualenv+nginx+supervisor详细步骤
    python实现下载文件路径自动添加(1)的递增路径
    cx_Oracle连接oracle数据库
    pillow模块常用方法
    python内建集合模块collections功能,计数,有序,双向队列
    paramiko批量上传下载sftp,解决访问windows系列sftp乱码问题
  • 原文地址:https://www.cnblogs.com/qixue/p/1587812.html
Copyright © 2011-2022 走看看