C#中的Actor模型與普通線程在多個方面存在顯著差異。以下是它們之間的主要區別:
Thread
類或Task
類來創建和管理線程,需要手動處理并發控制和同步問題。public interface IActor
{
bool AddMsg(object message);
Task Start();
bool Stop(int WaitingTimeout = 100);
}
public abstract class Actor : IDisposable, IActor
{
public Actor(string name)
{
Name = name;
MailBox = new BlockingCollection<object>();
}
public string Name { get; set; }
public bool Active { get; private set; }
public bool LongRunning { get; set; } = true;
public BlockingCollection<object> MailBox { get; set; }
private Task _task;
public virtual Task Start()
{
if (Active) return _task;
Active = true;
// 啟動異步
if (_task == null)
{
lock (this)
{
if (_task == null)
{
_task = Task.Factory.StartNew(DoActorWork, LongRunning ? TaskCreationOptions.LongRunning : TaskCreationOptions.None);
}
}
}
return _task;
}
public virtual bool Stop(int WaitingTimeout = 100)
{
MailBox?.CompleteAdding();
Active = false;
if (WaitingTimeout == 0 || _task == null) return true;
return _task.Wait(WaitingTimeout);
}
public virtual bool AddMsg(object message)
{
// 自動開始
if (!Active) { Start(); }
if (!Active) { return false; }
MailBox.Add(message);
return true;
}
private void DoActorWork()
{
while (true)
{
object message = MailBox.Take();
if (message is null) break;
ProcessMessage(message);
}
}
protected virtual void ProcessMessage(object message)
{
// 處理消息的邏輯
}
}
public class MultiThreadExample implements Runnable
{
private string threadName;
public MultiThreadExample(string name)
{
this.threadName = name;
}
public void run()
{
System.out.println("Thread " + threadName + " starting.");
for (int i = 0; i < 5; i++)
{
System.out.println("Thread " + threadName + " running. Count: " + i);
try
{
Thread.Sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Thread " + threadName + " interrupted.");
}
}
System.out.println("Thread " + threadName + " exiting.");
}
public static void main(string[] args)
{
System.out.println("Main thread starting.");
MultiThreadExample thread1 = new MultiThreadExample("Thread 1");
MultiThreadExample thread2 = new MultiThreadExample("Thread 2");
Thread t1 = new Thread(thread1);
Thread t2 = new Thread(thread2);
t1.start();
t2.start();
System.out.println("Main thread exiting.");
}
}
通過這些對比,可以看出C# Actor模型在并發編程中提供了更高的抽象級別、更好的隔離性和效率,適用于高并發場景。而普通線程則更適用于需要共享內存和資源的場景,但需要更精細的并發控制。