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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 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);
           
        }

    }

}