Position:Home>net development> The LockSupport in Java kind the implementation in C#
The LockSupport in Java kind the implementation in C#
From;  Author:Stand originally

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.
Previous12 Next