RSS
热门关键字:
当前位置 :| 主页>net development>

The LockSupport in Java kind the implementation in C#

来源: 作者:Stand originally 时间:2008-12-21 Tag: 点击:

Outstanding intercurrent library Util.concurrent is offerred after Java 5, .netLack in similarFunction. Because hardware system produced change, much nucleus times comes, the lack in.NET erupts simultaneously kind of library is apparent malapropos. Alleviate of this one contradiction among them a method is going to what Java transplants in C# namelyUtil.concurrent.

The Util.concurrent in Java offerred in the bag kind of LockSupport, util.concurrent wraps a lot of keys to realize need to call LockSupport. If need the Util.concurrent Bao Qian of a Java to move,arrive in C# , lockSupport kind migratory it is inevitable question.

In Java, lockSupport kind be like lower part law:

It is to cite below extract:
Public Static Void Park(Object Blocker) {
Thread T = Thread.currentThread();
SetBlocker(t, blocker);
Unsafe.park(false, 0L);
SetBlocker(t, null);
}

After calling LockSupport.park when Cheng of a line, line Cheng can stop download, be similar to Object.wait, or the System.Threading.Monitor.Wait in.NET. But the problem is the Monitor.wait in the Object.wait in Java and.NET, need a WaitObject, this problem once perplexed me, turned over JDK 6 to realize a source for this, going to what discover finally to settle way is very simple however, the ground floor that also need not understand JDK realizes a source.

It is to cite below extract:
Public Class LockSupport
{
Private Static LocalDataStoreSlot Slot = Thread.GetNamedDataSlot("LockSupport.Park" );
Public Static Void Park(Object Blocker)
{
Thread Thread = Thread.CurrentThread;
Thread.SetData(slot, blocker);
Lock (thread)
{
Monitor.Wait(thread);
}
}
Public Static Void Unpark(Thread Thread)
{
If (thread==Null) Return;
Lock (thread)
{
Monitor.Pulse(thread);
}
}
}

Among them Slot need not need, but the LockSupport code of referenced JDK Util.concurrent is pleasant to the eye, the ThreadLocal) of use Slot(java is OK and convenient dog debug.

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册