并发编程中.net与java的一些对比
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 方法,以释放一个或多个被阻止的线程。
ManualReset和AutoReset的区别:用 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的一些对比的更多相关文章
- Java并发编程(十一)-- Java中的锁详解
上一章我们已经简要的介绍了Java中的一些锁,本章我们就详细的来说说这些锁. synchronized锁 synchronized锁是什么? synchronized是Java的一个关键字,它能够将代 ...
- 【Java并发编程】6、volatile关键字解析&内存模型&并发编程中三概念
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- Java并发编程中的相关注解
引自:http://www.cnblogs.com/phoebus0501/archive/2011/02/21/1960077.html Java并发编程中,用到了一些专门为并发编程准备的 Anno ...
- Java的并发编程中的多线程问题到底是怎么回事儿?
在我之前的一篇<再有人问你Java内存模型是什么,就把这篇文章发给他.>文章中,介绍了Java内存模型,通过这篇文章,大家应该都知道了Java内存模型的概念以及作用,这篇文章中谈到,在Ja ...
- Java并发编程中的设计模式解析(二)一个单例的七种写法
Java单例模式是最常见的设计模式之一,广泛应用于各种框架.中间件和应用开发中.单例模式实现起来比较简单,基本是每个Java工程师都能信手拈来的,本文将结合多线程.类的加载等知识,系统地介绍一下单例模 ...
- Java并发编程中的设计模式解析(一)
Java并发编程,除了被用于各种Web应用.分布式系统和大数据系统,构成高并发系统的核心基础外,其本身也蕴含着大量的设计模式思想在里面.这一系列文章主要是结合Java源码,对并发编程中使用到的.实现的 ...
- Java多线程学习(七)并发编程中一些问题
本节思维导图: 关注微信公众号:"Java面试通关手册" 回复"Java多线程"获取思维导图源文件和思维导图软件. 多线程就一定好吗?快吗?? 并发编程的目的就 ...
- Java并发编程中的若干核心技术,向高手进阶!
来源:http://www.jianshu.com/p/5f499f8212e7 引言 本文试图从一个更高的视角来总结Java语言中的并发编程内容,希望阅读完本文之后,可以收获一些内容,至少应该知道在 ...
- 并发编程(十三)—— Java 线程池 实现原理与源码深度解析 之 Executors(三)
前两篇文章讲了线程池的源码分析,再来看这篇文章就比较简单了, 本文主要讲解 Executors 这个工具类,看看长江创建线程池的几种方法. newFixedThreadPool 生成一个固定大小的线程 ...
随机推荐
- combox 同时写入和获取 text ,value
c# combox 同时写入和获取 text ,value 2007-10-10 16:33:44| 分类: c# 知识|举报|字号 订阅 public class ComboBoxItem ...
- MyBatis学习(四)、MyBatis配置文件
四.MyBatis主配置文件 在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" clas ...
- C#动态webservice调用接口 (JAVA,C#)
C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using ...
- Windows2003 IIS6.0支持32位和64位两种模式的设置方法
IIS 6.0 可支持 32 位和 64 位两种模式.但是,IIS 6.0 不支持在 64 位版本的 Windows 上同时运行这两种模式.ASP.NET 1.1 只在 32 位模式下运行.而 ASP ...
- Asp.Net调试方法备忘
由于种种原因导致vs不能启用Web服务器调试.可用如下方法来执行调试. 1.在vs中选择 调试>启动不调试(ctr+f5), 2.设置你需调试的相关断点,然后选择 调试>进程.选择Aspn ...
- JS 继承总结
ES里面没有真正的继承,但是能通过某些手段达到继承效果,从而让一个类拥有另外一个类的方法 类 =>构造函数 继承描述某语言环境---魔兽世界 哈!其实我没玩过 魔兽世界里面 有Humen类 ...
- css不常用重要属性
超出省略号:display:block;white-space:norwrap;overflow:hidden;text-overflow:ellipsis; white-space:norwrap/ ...
- Flask最佳实践
https://zhuanlan.zhihu.com/p/22774028?refer=python-cn
- Laravel excel安装与使用
在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能 时间 2015-11-17 18:40:56 Laravel学院 原文 http://lar ...
- 数据结构与算法(1)支线任务3——Largest Rectangle in Histogram
题目如下:(https://leetcode.com/problems/largest-rectangle-in-histogram/) Given n non-negative integers r ...