Java在并发编程中进行使用java.util.concurrent.atomic来处理一些轻量级变量 如AtomicInteger AtomicBoolean等

.Net中则使用Interlocked来实现类似功能

Java中使用object的wait和notify方法来实现线程间的写作

.Net中可以使用Semaphore(信号量)、mutex(互斥)、和EventWaitHandle来实现 但是Semaphore类的构造函数需要指定初始入口数和最大入口数

msdn:

mutex:当两个或多个线程需要同时访问共享的资源时,系统将需要使用同步机制来确保一次只有一个线程使用的资源。 Mutex 是一个同步基元,授予于只有一个线程对共享资源的独占访问权限。 如果一个线程获取互斥体,第二个想要获取该互斥体挂起线程,直到第一个线程释放此斥锁。当互斥体发出信号以指示不拥有时,waitone方法将返回true。反之当超过waitone方法中的等待时间后 waitone将返回false。拥有互斥锁的线程可以请求中的重复调用相同的互斥体 WaitOne 而不会阻止其执行。 但是,调用线程必须 ReleaseMutex 方法以释放互斥体的所属权相同次数。

        private static Mutex mut = new Mutex();
private const int numIterations = ;
private const int numThreads = ;
static void Main(string[] args)
{
for (int i = ; i < numThreads; ++i)
{
Thread newThread = new Thread(new ThreadStart(ThreadProc));
newThread.Name = String.Format("Thread{0}", i+);
newThread.Start();
}
} static void ThreadProc()
{
for (int i = ; i < numIterations; i++)
{
useResource();
}
} static void useResource()
{
Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);
if (mut.WaitOne())
{
Console.WriteLine("{0} has enter the protected area", Thread.CurrentThread.Name);
Thread.Sleep();
Console.WriteLine("{0} is leaving the protected area", Thread.CurrentThread.Name);
mut.ReleaseMutex();
Console.WriteLine("{0} is released the mutex", Thread.CurrentThread.Name);
}
else
{
Console.WriteLine("{0} will not acquire the mutex", Thread.CurrentThread.Name);
}
}

运行结果:

Thread1 is requesting the mutex
Thread1 has enter the protected area
Thread2 is requesting the mutex
Thread3 is requesting the mutex
Thread1 is leaving the protected area
Thread1 is released the mutex
Thread3 has enter the protected area
Thread3 is leaving the protected area
Thread3 is released the mutex
Thread2 has enter the protected area
Thread2 is leaving the protected area
Thread2 is released the mutex

Semaphore:限制可同时访问某一资源或资源池的线程数。Semaphore分为本地信号量和系统信号量。本地信号量只能被所在进程内所有线程使用。系统信号量使用System.Threading.Semaphore.OpenExisting创建或打开

        private static Semaphore _pool;
private static int _padding;
static void Main(string[] args)
{
_pool = new Semaphore(, ); //初始化入口数和最大并发数
for (int i = ; i < ; ++i)
{
Thread newThread = new Thread(new ParameterizedThreadStart(Worker));
newThread.Start(i);
}
Thread.Sleep();
Console.WriteLine("main thread call release(3)");
_pool.Release(); Console.WriteLine("Main Thread exits"); } static void Worker(object obj)
{
Console.WriteLine("Thread {0} begins " + "and waits for the semaphore", obj);
_pool.WaitOne(); int padding = Interlocked.Add(ref _padding, );
Thread.Sleep( + padding); Console.WriteLine("Thread {0} releases the semaphore", obj);
Console.WriteLine("Thread {0} previous semaphore count: {1}",
obj, _pool.Release());
}

执行结果:

Thread 0 begins and waits for the semaphore
Thread 1 begins and waits for the semaphore
Thread 2 begins and waits for the semaphore
Thread 3 begins and waits for the semaphore
Thread 4 begins and waits for the semaphore
main thread call release(3)
Main Thread exits
Thread 3 releases the semaphore
Thread 3 previous semaphore count: 0
Thread 4 releases the semaphore
Thread 4 previous semaphore count: 0
Thread 0 releases the semaphore
Thread 0 previous semaphore count: 0
Thread 2 releases the semaphore
Thread 2 previous semaphore count: 1
Thread 1 releases the semaphore
Thread 1 previous semaphore count: 2

EventWaitHandle:EventWaitHandle 类允许线程通过发信号互相通信。通常,一个或多个线程在 EventWaitHandle 上阻止,直到一个未阻止的线程调用 Set 方法,以释放一个或多个被阻止的线程。

ManualResetAutoReset的区别:用 EventResetMode.AutoReset 标志创建的 EventWaitHandle 在终止时会自动重置。用 EventResetMode.ManualReset 标志创建的 EventWaitHandle 一直保持终止状态

        private static EventWaitHandle ewh;
private static long threadCount = ;
private static EventWaitHandle clearCount =
new EventWaitHandle(false, EventResetMode.AutoReset); static void Main(string[] args)
{
ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
for (int i = ; i <= ; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
t.Start(i);
}
while (Interlocked.Read(ref threadCount) < )
{
Thread.Sleep();
}
while (Interlocked.Read(ref threadCount) > )
{
Console.WriteLine("Press ENTER to release a waiting thread.");
Console.ReadLine();
WaitHandle.SignalAndWait(ewh, clearCount);
}
Console.WriteLine();
ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
for (int i = ; i <= ; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
t.Start(i);
}
while (Interlocked.Read(ref threadCount) < )
{
Thread.Sleep();
}
Console.WriteLine("Press ENTER to release the waiting threads.");
Console.ReadLine();
ewh.Set();
} public static void ThreadProc(Object num)
{
int index = (Int32)num;
Console.WriteLine("Thread{0} blocked", num);
Interlocked.Increment(ref threadCount);
ewh.WaitOne();
Console.WriteLine("Thread {0} exits.", num);
Interlocked.Decrement(ref threadCount);
clearCount.Set();
}

对比:

mutex类强制线程标示,只能由获得他的线程释放互斥体,而Semaphore并不强制线程标示,可以跨应用程序域边界传递互斥体

并发编程中.net与java的一些对比的更多相关文章

  1. Java并发编程(十一)-- Java中的锁详解

    上一章我们已经简要的介绍了Java中的一些锁,本章我们就详细的来说说这些锁. synchronized锁 synchronized锁是什么? synchronized是Java的一个关键字,它能够将代 ...

  2. 【Java并发编程】6、volatile关键字解析&内存模型&并发编程中三概念

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  3. Java并发编程中的相关注解

    引自:http://www.cnblogs.com/phoebus0501/archive/2011/02/21/1960077.html Java并发编程中,用到了一些专门为并发编程准备的 Anno ...

  4. Java的并发编程中的多线程问题到底是怎么回事儿?

    在我之前的一篇<再有人问你Java内存模型是什么,就把这篇文章发给他.>文章中,介绍了Java内存模型,通过这篇文章,大家应该都知道了Java内存模型的概念以及作用,这篇文章中谈到,在Ja ...

  5. Java并发编程中的设计模式解析(二)一个单例的七种写法

    Java单例模式是最常见的设计模式之一,广泛应用于各种框架.中间件和应用开发中.单例模式实现起来比较简单,基本是每个Java工程师都能信手拈来的,本文将结合多线程.类的加载等知识,系统地介绍一下单例模 ...

  6. Java并发编程中的设计模式解析(一)

    Java并发编程,除了被用于各种Web应用.分布式系统和大数据系统,构成高并发系统的核心基础外,其本身也蕴含着大量的设计模式思想在里面.这一系列文章主要是结合Java源码,对并发编程中使用到的.实现的 ...

  7. Java多线程学习(七)并发编程中一些问题

    本节思维导图: 关注微信公众号:"Java面试通关手册" 回复"Java多线程"获取思维导图源文件和思维导图软件. 多线程就一定好吗?快吗?? 并发编程的目的就 ...

  8. Java并发编程中的若干核心技术,向高手进阶!

    来源:http://www.jianshu.com/p/5f499f8212e7 引言 本文试图从一个更高的视角来总结Java语言中的并发编程内容,希望阅读完本文之后,可以收获一些内容,至少应该知道在 ...

  9. 并发编程(十三)—— Java 线程池 实现原理与源码深度解析 之 Executors(三)

    前两篇文章讲了线程池的源码分析,再来看这篇文章就比较简单了, 本文主要讲解 Executors 这个工具类,看看长江创建线程池的几种方法. newFixedThreadPool 生成一个固定大小的线程 ...

随机推荐

  1. Javascript技巧

    Javascript数组转换为CSV格式 首先考虑如下的应用场景,有一个Javscript的字符型(或者数值型)数组,现在需要转换为以逗号分割的CSV格式文件.则我们可以使用如下的小技巧,代码如下: ...

  2. nginx下rewrite参数超过9个的解决方法

    nginx 在处理多于9个参数的时候,是采用重命名的方法来实现的: /?m?([0-9,]*)h?(\d*)a?([0-9,]*)c?(\d*)s?(x?f?(?P<f>[0-9,]*)/ ...

  3. Windows 10 LTSB

    没有商店,没有EDGE,没有助手. 有平板模式,但没有通用应用,内置便笺也没有.可以“固定到开始屏幕”,但没有动态磁贴.有虚拟键盘,但没有Windows Ink. 只有安全补丁更新,据说是10年. 安 ...

  4. My Tornado Particle Effect

    These animations are more able to demostrate this plugin than the following static images. :) test 1 ...

  5. js嵌套对象相等比较的一种方法 (原创)

    做前端开发经常会遇到比较js对象是否相等的情况, 或者说其它问题往往会归结到这个问题上来:比如对象数组的去重复. 网上看到过很多例子, 但是基本上都是那种比较简单的对象结构, 而复杂的对象结构,比如对 ...

  6. phpmyadmin的root密码忘记了怎么办?

    管理mysql数据库的工具有网页版的phpmyadmin(开源),也有诸如客户端Navicat for MySQL(商业). 好多环境集成包,比如XAMPP.WampServer等集成了phpmyad ...

  7. SQL时间相关

    SQL --本周第一天 SELECT DATEADD(Day,-(DATEPART(Weekday,getdate())+@@DATEFIRST-)%,getdate()) --or ,getdate ...

  8. mysql中的模糊查询

    转载自:http://www.letuknowit.com/archives/90/ MySQL中实现模糊查询有2种方式:一是用LIKE/NOT LIKE,二是用REGEXP/NOT REGEXP(或 ...

  9. 尝试打开或创建物理文件 REATE FILE 遇到操作系统错误 5(拒绝访问)

    尝试打开或创建物理文件 'E:\Library.mdf' 时,CREATE FILE 遇到操作系统错误 5(拒绝访问.). 最佳回答: 这是因为SQL Server的启动帐户(一般是system或某个 ...

  10. 使用 Google Web Fonts

    Google Fonts 的介绍:Google Fonts 并不是简单的字体下载站 Google Fonts 地址:https://www.google.com/fonts 左上角可以输入查找的字体名 ...