Java Object
容器相关: int hashCode(),boolean equals(Object obj)
生命周期相关:protected Object clone(),protected void finalize()
反射相关:Class<?> getClass()
描述相关:String toString()
多线程同步相关 :
void wait()
void notify()
void notifyAll()
Java wait/notify机制
wait():此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。
notify():唤醒在此对象监视器上等待的单个线程。.net Object
public class Object
{ public virtual bool Equals(object obj);
public static bool Equals(object objA, object objB); protected override void Finalize();
public virtual int GetHashCode();
public extern Type GetType(); [MethodImpl(MethodImplOptions.InternalCall)]
protected extern object MemberwiseClone(); public virtual string ToString();
}
为啥在.net Class中没有Java中多线程同步相关的方法,.net是底层机制上是如何实现多线程同步的?
是不是由WaitHandle完成这部分功能的?public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
}; // Define a random number generator for testing.
static Random r = new Random(); static void Main()
{
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);
// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds); // Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
} static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}WaitHandle为啥又没有notify方法?
容器相关: int hashCode(),boolean equals(Object obj)
生命周期相关:protected Object clone(),protected void finalize()
反射相关:Class<?> getClass()
描述相关:String toString()
多线程同步相关 :
void wait()
void notify()
void notifyAll()
Java wait/notify机制
wait():此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。
notify():唤醒在此对象监视器上等待的单个线程。.net Object
public class Object
{ public virtual bool Equals(object obj);
public static bool Equals(object objA, object objB); protected override void Finalize();
public virtual int GetHashCode();
public extern Type GetType(); [MethodImpl(MethodImplOptions.InternalCall)]
protected extern object MemberwiseClone(); public virtual string ToString();
}
为啥在.net Class中没有Java中多线程同步相关的方法,.net是底层机制上是如何实现多线程同步的?
是不是由WaitHandle完成这部分功能的?public sealed class App
{
// Define an array with two AutoResetEvent WaitHandles.
static WaitHandle[] waitHandles = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
}; // Define a random number generator for testing.
static Random r = new Random(); static void Main()
{
// Queue up two tasks on two different threads;
// wait until all tasks are completed.
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);
// The time shown below should match the longest task.
Console.WriteLine("Both tasks are completed (time waited={0})",
(DateTime.Now - dt).TotalMilliseconds); // Queue up two tasks on two different threads;
// wait until any tasks are completed.
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete.");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);
// The time shown below should match the shortest task.
Console.WriteLine("Task {0} finished first (time waited={1}).",
index + 1, (DateTime.Now - dt).TotalMilliseconds);
} static void DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent) state;
int time = 1000 * r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.", time);
Thread.Sleep(time);
are.Set();
}
}WaitHandle为啥又没有notify方法?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货