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. 集群工具ansible使用方法

    ansible简介 ansible是与puppet.saltstack类似的集群管理工具,其优点是仅需要ssh和Python即可使用,而不像puppet.saltstack那样都需要客户端.与pupp ...

  2. JVM1.6 GC详解

    前言  JVM GC是JVM的内存回收算法,调整JVM GC(Garbage Collection),可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java程序的工作效率. ...

  3. C# webbrowser实现真正意义上的F5刷新

    关于webbrowser的刷新在C#中有提供方便的方法: webbrowser.refresh(); 但是有时候会发现,不给力啊 那怎么办? 还有一招: webBrowser1.Document.Ex ...

  4. 关于MySql 关键字与字段名冲突 的问题

    我在用mysql创建数据表时,其中一个表怎么创建都提示失败,最终我把语句翻来覆去折腾了许多遍之后发现原来我的一个字段值的名称为order的字段出了问题,把它去了就好了,最后结论就是设置字段值名称时不要 ...

  5. volley_缓存介绍

    离线缓存就是在网络畅通的情况下将从服务器收到的数据保存到本地,当网络断开之后直接读取本地文件中的数据.如Json 数据缓存到本地,在断网的状态下启动APP时读取本地缓存数据显示在界面上,常用的APP( ...

  6. Redis数据库入门教程

    [使用redis客户端] 我们直接看一个例子: 复制代码 代码如下: //这样来启动redis客户端了 $ ./redis-cli //用set指令来设置key.value 127.0.0.1:637 ...

  7. 解决类似 Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)的问题

    源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found的问题.这 ...

  8. php 二维数组排序,多维数组排序

    对2维数组或者多维数组排序是常见的问题,在php中我们有个专门的多维数组排序函数,下面简单介绍下: array_multisort(array1,sorting order, sorting type ...

  9. 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure

    相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...

  10. jQuery MiniUI开发系列之:创建组件对象

    jQuery MiniUI可以使用Javascript和Html两种方式来创建对象. 1)Javascript创建对象 使用JavaScript创建对象,是最基本的方式,有如下几个要点: 1)使用ne ...