在C#并发编程领域,有许多经典问题常常被面试官用来考察应聘者的理解和实际应用能力。以下是一些常见问题及其解析,帮助您更好地准备面试。
1. 什么是线程(Thread)?
线程是程序执行的最小单位,它是操作系统能够进行运算调度的最小单位。在C#中,线程是用于执行并发操作的。
示例代码:
using System;
using System.Threading;
class Program
{
static void Main()
{
Thread t = new Thread(new ThreadStart(Work));
t.Start();
}
static void Work()
{
Console.WriteLine("Thread is working.");
}
}
2. 什么是线程池(ThreadPool)?
线程池是系统维护的一个线程集合,用于执行并发任务。线程池可以减少线程创建和销毁的开销,提高系统性能。
示例代码:
using System;
using System.Threading;
class Program
{
static void Main()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
}
static void Work(object state)
{
Console.WriteLine("Thread is working.");
}
}
3. 什么是锁(Lock)?
锁是用于线程同步的一种机制,它可以确保同一时刻只有一个线程可以访问某个代码段。
示例代码:
using System;
using System.Threading;
class Program
{
private static readonly object _lock = new object();
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work());
}
}
static void Work()
{
lock (_lock)
{
Console.WriteLine("Thread is working.");
}
}
}
4. 什么是Monitor?
Monitor是一种线程同步机制,它可以保证同一时刻只有一个线程可以访问某个对象。
示例代码:
using System;
using System.Threading;
class Program
{
private static readonly object _monitor = new object();
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work());
}
}
static void Work()
{
Monitor.Enter(_monitor);
try
{
Console.WriteLine("Thread is working.");
}
finally
{
Monitor.Exit(_monitor);
}
}
}
5. 什么是Semaphore?
Semaphore是一种信号量,用于控制对特定资源的访问,允许一定数量的线程同时访问资源。
示例代码:
using System;
using System.Threading;
class Program
{
private static Semaphore _semaphore = new Semaphore(3, 3);
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work());
}
}
static void Work()
{
_semaphore.WaitOne();
try
{
Console.WriteLine("Thread is working.");
}
finally
{
_semaphore.Release();
}
}
}
6. 什么是Mutex?
Mutex是互斥锁,它确保同一时刻只有一个线程可以访问某个资源。
示例代码:
using System;
using System.Threading;
class Program
{
private static readonly Mutex _mutex = new Mutex();
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work());
}
}
static void Work()
{
_mutex.WaitOne();
try
{
Console.WriteLine("Thread is working.");
}
finally
{
_mutex.ReleaseMutex();
}
}
}
7. 什么是ManualResetEvent?
ManualResetEvent是一种可以手动重置的信号量,用于线程同步。
示例代码:
using System;
using System.Threading;
class Program
{
private static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work());
}
_manualResetEvent.Set();
}
static void Work()
{
_manualResetEvent.WaitOne();
try
{
Console.WriteLine("Thread is working.");
}
finally
{
_manualResetEvent.Reset();
}
}
}
8. 什么是ConcurrentDictionary?
ConcurrentDictionary是C# 3.5引入的一种线程安全的字典,它支持高并发操作。
示例代码:
using System;
using System.Collections.Concurrent;
using System.Threading;
class Program
{
private static ConcurrentDictionary<int, string> _dict = new ConcurrentDictionary<int, string>();
static void Main()
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(() => Work(i));
}
}
static void Work(int id)
{
_dict.TryAdd(id, "Value" + id);
Console.WriteLine("Thread " + id + " added value.");
}
}
总结
C#并发编程是一个广泛且复杂的主题,上述经典问题只是冰山一角。掌握这些概念和示例代码,将有助于您在面试中展示出对C#并发编程的深入理解。祝您面试顺利!
