并发编程中.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 生成一个固定大小的线程 ...
随机推荐
- 如何通过SerialPort读取和写入设备COM端口数据
SerialPort类用于控制串行端口文件资源.提供同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问.另外,SerialPort的功能可以包装在内部 Stream ...
- ListView addHeaderView 对 position 的影响
1. 在 public View getView(int position, View convertView, ViewGroup parent) 中position 和 是否有headerView ...
- C# 读取和配置IniFile
自定义IniFile操作类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- git 文件重命名
文件重命名 git mv old_name new_name git commit -m 'rename' git push origin master 删除文件 git rm filename
- iOS5.0以上使用新浪微博开放平台OAuth 续(及解决登录无效问题)
新浪微博开放平台为第三方应用提供了简便的合作模式,满足了手机用户和平板电脑用户随时随地分享信息的需求.通过调用平台的api即可实现很多微博上的功能. 本篇主要目的是记录新浪微博移动SDK iOS版本的 ...
- dbms_output.put_line 不显示
再写oracle sql时候,写循环语句,想知道循环对不对,使用dbms_output.put_line()没有打印出任何东西,网上查找发现少了一句. 加上 set serverouput on 就 ...
- Solr整合Ansj中文分词器
Ansj的使用和相关资料下载参考:http://iamyida.iteye.com/blog/2220833 参考 http://www.cnblogs.com/luxh/p/5016894.html ...
- Redis(一) 介绍
先说明下,本人是在windows系统下用的. 简单介绍一下,是nosql数据库,采用key-value存储方式形式.可用于处理高并发日志.维护top表等. 如果把它完全当初数据库使用,当做小型数据库还 ...
- Strint类成员
String& String::operator=(const string& other){ if(this == &other) { return *this; } de ...
- 如何在centos 6.7 上安装oracle 11gR2
1.软件准备: centos6.7(64位); oracle11gR2((Linux x86-64)) 2.执行如下命令安装好相关的包: yum -y install \ binutils \ com ...