惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - buru

django begining Update 两个表之间更新数据问题 用户控件与应用页面的事件顺序 linq小问题总结 “尝试读取或写入受保护的内存”错误处理 firefox浏览器的默认事件 配置rails运行环境 通过c#和ironruby学习 ruby语言 SQL 语句查询与性能 操作Access数据库碰到的几个问题 asp.net的CodeDom zz天涯-关于职业生涯的 一个线程管理器 zz两个存储过程 Mediator 中介者模式 让firefox支持IE的一些属性 IE与firefox取得事件对象的函数(zz) proxy模式 c#通过url获取文件
看《C#线程参考手册》
buru · 2007-10-29 · via 博客园 - buru

看到第二章,暂时知道的有:

一个个线程在寄存器中是以队列的形式排列的,但是执行的时候并不是按顺序,而是由操作系统非配一个固定的时间片段来个每个线程,一个线程在一个时间段执行完以后就排到队列最后,cpu执行下一个线程。

以前经常用的Thread.Sleep方法其实是使当前线程睡眠掉。

有时候多线程执行效率较高,但cpu非配线程也会消耗一定资源。

ThreadStart委托的函数是没有参数的,所以有参数的情况下需要建立一个代理的类。

如果把工作放到队列中,并且应该使用多线程,就可以考虑使用线程池。
但是不管任何情况下,尽量使线程数目最少。这样会减少开销,增加用来处理线程中指令的时间量,减少应用程序的内存。
---------------------
Threading.ReadWriteLock类:
在类中的一个ReadWriteLock实例成员,可以获取读取或写的锁,读取字段值的时候就不能写入,写入字段值的时候就不能读取。读取可以几个线程一起,写只能一个线程获得锁。

--------------几个用于手动同步的类---------------
同步就是操作线程执行指令的顺序。
AutoResetEvent :WaitOne()后会改变状态。
ManualResetEvent : WaitOne不会改变状态.与AutoReSetEvent一样用来使线程处于等待状态,通过Set()方法某事件将它置于有信号(即waitone值为true)。Reset()将使之无信号。
Mutex:类似Monitor类,有ReleaseMutex来释放锁。
然后waitone()执行结果true,继续运行。否则会抛出异常。

using System;
using System.Threading;
namespace Threads
{   
    
class WroxMutex
    
{
        
static Mutex myMutex;

        
public static void Main()
        
{
            myMutex 
= new Mutex(true,"WROX");
            WroxMutex nm 
= new WroxMutex();
            Thread t 
= new Thread(new ThreadStart(nm.Run));
            t.Start();
            Console.WriteLine(
"Thread Sleep for 5 sec");
            Thread.Sleep(
5000);
            Console.WriteLine(
"Thread Woke Up");
            myMutex.ReleaseMutex();
            Console.WriteLine(
"Befor WaitOne");
            myMutex.WaitOne( );
            Console.WriteLine(
"Lock owned by Main Thread");

        }

        
public void Run()
        
{
            Console.WriteLine(
"In Run");
            myMutex.WaitOne();
            Console.WriteLine(
"Thread sleeping for 10 secs");
            Thread.Sleep(
10000);
            Console.WriteLine(
"End of Run() Method");
            myMutex.ReleaseMutex();
        }

    }

}

Interlocked:同步访问一个由许多线程共享的变量。

namespace Threads
{
    
class WorxInterLocked
    
{
        
public ManualResetEvent a = new ManualResetEvent(false);

        
private int i = 5;
        
public void Run(object s)
        
{
            Interlocked.Increment(
ref i);//递增 1.
            
//i++;
            Console.WriteLine("{0} {1}", Thread.CurrentThread.GetHashCode(), i);

        }

    }

    
public class MainAPP
    
{
        
public static void Main()
        
{
            ManualResetEvent mR 
= new ManualResetEvent(false);
            WorxInterLocked wL 
= new WorxInterLocked();
            
for (int i = 1; i <= 10; i++)
            
{
                ThreadPool.QueueUserWorkItem(
new WaitCallback(wL.Run), 1);

            }

            mR.WaitOne(
10000,true);
           
        }

    }

}