1、IProducerConsumerCollection (线程安全接口)
  此接口的所有实现必须都启用此接口的所有成员,若要从多个线程同时使用。

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic; namespace ConsoleApp1
{
public class SafeStack<T> : IProducerConsumerCollection<T>
{
// Used for enforcing thread-safety
private object m_lockObject = new object(); // We'll use a regular old Stack for our core operations
private Stack<T> m_sequentialStack = null; //
// Constructors
//
public SafeStack()
{
m_sequentialStack = new Stack<T>();
} public SafeStack(IEnumerable<T> collection)
{
m_sequentialStack = new Stack<T>(collection);
} //
// Safe Push/Pop support
//
public void Push(T item)
{
lock (m_lockObject) m_sequentialStack.Push(item);
} public bool TryPop(out T item)
{
bool rval = true;
lock (m_lockObject)
{
if (m_sequentialStack.Count == ) { item = default(T); rval = false; }
else item = m_sequentialStack.Pop();
}
return rval;
} //
// IProducerConsumerCollection(T) support
//
public bool TryTake(out T item)
{
return TryPop(out item);
} public bool TryAdd(T item)
{
Push(item);
return true; // Push doesn't fail
} public T[] ToArray()
{
T[] rval = null;
lock (m_lockObject) rval = m_sequentialStack.ToArray();
return rval;
} public void CopyTo(T[] array, int index)
{
lock (m_lockObject) m_sequentialStack.CopyTo(array, index);
} //
// Support for IEnumerable(T)
//
public IEnumerator<T> GetEnumerator()
{
// The performance here will be unfortunate for large stacks,
// but thread-safety is effectively implemented.
Stack<T> stackCopy = null;
lock (m_lockObject) stackCopy = new Stack<T>(m_sequentialStack);
return stackCopy.GetEnumerator();
} //
// Support for IEnumerable
//
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
} //
// Support for ICollection
//
public bool IsSynchronized
{
get { return true; }
} public object SyncRoot
{
get { return m_lockObject; }
} public int Count
{
get { return m_sequentialStack.Count; }
} public void CopyTo(Array array, int index)
{
lock (m_lockObject) ((ICollection)m_sequentialStack).CopyTo(array, index);
}
}
}

SafeStack

using System;
using System.Collections.Concurrent; namespace ConsoleApp1
{
class Program
{
static void Main()
{
TestSafeStack(); // Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
} // Test our implementation of IProducerConsumerCollection(T)
// Demonstrates:
// IPCC(T).TryAdd()
// IPCC(T).TryTake()
// IPCC(T).CopyTo()
static void TestSafeStack()
{
SafeStack<int> stack = new SafeStack<int>();
IProducerConsumerCollection<int> ipcc = (IProducerConsumerCollection<int>)stack; // Test Push()/TryAdd()
stack.Push(); Console.WriteLine("Pushed 10");
ipcc.TryAdd(); Console.WriteLine("IPCC.TryAdded 20");
stack.Push(); Console.WriteLine("Pushed 15"); int[] testArray = new int[]; // Try CopyTo() within boundaries
try
{
ipcc.CopyTo(testArray, );
Console.WriteLine("CopyTo() within boundaries worked, as expected");
}
catch (Exception e)
{
Console.WriteLine("CopyTo() within boundaries unexpectedly threw an exception: {0}", e.Message);
} // Try CopyTo() that overflows
try
{
ipcc.CopyTo(testArray, );
Console.WriteLine("CopyTo() with index overflow worked, and it SHOULD NOT HAVE");
}
catch (Exception e)
{
Console.WriteLine("CopyTo() with index overflow threw an exception, as expected: {0}", e.Message);
} // Test enumeration
Console.Write("Enumeration (should be three items): ");
foreach (int item in stack)
Console.Write("{0} ", item);
Console.WriteLine(""); // Test TryPop()
int popped = ;
if (stack.TryPop(out popped))
{
Console.WriteLine("Successfully popped {0}", popped);
}
else Console.WriteLine("FAILED to pop!!"); // Test Count
Console.WriteLine("stack count is {0}, should be 2", stack.Count); // Test TryTake()
if (ipcc.TryTake(out popped))
{
Console.WriteLine("Successfully IPCC-TryTaked {0}", popped);
}
else Console.WriteLine("FAILED to IPCC.TryTake!!");
}
}
}

Program

2、ConcurrentStack类:安全堆栈

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task t = RunProgram();
t.Wait();
} static async Task RunProgram()
{
var taskStack = new ConcurrentStack<CustomTask>();
var cts = new CancellationTokenSource(); var taskSource = Task.Run(() => TaskProducer(taskStack)); Task[] processors = new Task[];
for (int i = ; i <= ; i++)
{
string processorId = i.ToString();
processors[i - ] = Task.Run(
() => TaskProcessor(taskStack, "Processor " + processorId, cts.Token));
} await taskSource;
cts.CancelAfter(TimeSpan.FromSeconds()); await Task.WhenAll(processors);
} static async Task TaskProducer(ConcurrentStack<CustomTask> stack)
{
for (int i = ; i <= ; i++)
{
await Task.Delay();
var workItem = new CustomTask { Id = i };
stack.Push(workItem);
Console.WriteLine("Task {0} has been posted", workItem.Id);
}
} static async Task TaskProcessor(
ConcurrentStack<CustomTask> stack, string name, CancellationToken token)
{
await GetRandomDelay();
do
{
CustomTask workItem;
bool popSuccesful = stack.TryPop(out workItem);
if (popSuccesful)
{
Console.WriteLine("Task {0} has been processed by {1}", workItem.Id, name);
} await GetRandomDelay();
}
while (!token.IsCancellationRequested);
} static Task GetRandomDelay()
{
int delay = new Random(DateTime.Now.Millisecond).Next(, );
return Task.Delay(delay);
} class CustomTask
{
public int Id { get; set; }
}
}
}

Program

3、ConcurrentQueue类:安全队列

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task t = RunProgram();
t.Wait();
} static async Task RunProgram()
{
var taskQueue = new ConcurrentQueue<CustomTask>();
var cts = new CancellationTokenSource(); var taskSource = Task.Run(() => TaskProducer(taskQueue)); Task[] processors = new Task[];
for (int i = ; i <= ; i++)
{
string processorId = i.ToString();
processors[i - ] = Task.Run(
() => TaskProcessor(taskQueue, "Processor " + processorId, cts.Token));
} await taskSource;
cts.CancelAfter(TimeSpan.FromSeconds()); await Task.WhenAll(processors);
} static async Task TaskProducer(ConcurrentQueue<CustomTask> queue)
{
for (int i = ; i <= ; i++)
{
await Task.Delay();
var workItem = new CustomTask { Id = i };
queue.Enqueue(workItem);
Console.WriteLine("插入Task {0} has been posted ThreadID={1}", workItem.Id, Thread.CurrentThread.ManagedThreadId);
}
} static async Task TaskProcessor(
ConcurrentQueue<CustomTask> queue, string name, CancellationToken token)
{
CustomTask workItem;
bool dequeueSuccesful = false; await GetRandomDelay();
do
{
dequeueSuccesful = queue.TryDequeue(out workItem);
if (dequeueSuccesful)
{
Console.WriteLine("读取Task {0} has been processed by {1} ThreadID={2}",
workItem.Id, name, Thread.CurrentThread.ManagedThreadId);
} await GetRandomDelay();
}
while (!token.IsCancellationRequested);
} static Task GetRandomDelay()
{
int delay = new Random(DateTime.Now.Millisecond).Next(, );
return Task.Delay(delay);
} class CustomTask
{
public int Id { get; set; }
}
}
}

Program

4、ConcurrentDictionary类
  ConcurrentDictionary类写操作比使用锁的通常字典(Dictionary)要慢的多,而读操作则要快些。因此对字典要大量的线程安全的读操作,ConcurrentDictionary类是最好的选择
  ConcurrentDictionary类的实现使用了细粒度锁(fine-grained locking)技术,这在多线程写入方面比使用锁的通常的字典(也被称为粗粒度锁)

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var concurrentDictionary = new ConcurrentDictionary<int, string>();
var dictionary = new Dictionary<int, string>(); var sw = new Stopwatch(); sw.Start();
for (int i = ; i < ; i++)
{
lock (dictionary)
{
dictionary[i] = Item;
}
}
sw.Stop();
Console.WriteLine("Writing to dictionary with a lock: {0}", sw.Elapsed); sw.Restart();
for (int i = ; i < ; i++)
{
concurrentDictionary[i] = Item;
}
sw.Stop();
Console.WriteLine("Writing to a concurrent dictionary: {0}", sw.Elapsed); sw.Restart();
for (int i = ; i < ; i++)
{
lock (dictionary)
{
CurrentItem = dictionary[i];
}
}
sw.Stop();
Console.WriteLine("Reading from dictionary with a lock: {0}", sw.Elapsed); sw.Restart();
for (int i = ; i < ; i++)
{
CurrentItem = concurrentDictionary[i];
}
sw.Stop();
Console.WriteLine("Reading from a concurrent dictionary: {0}", sw.Elapsed);
} const string Item = "Dictionary item";
public static string CurrentItem;
}
}

Program

5、ConcurrentBag类

namespace ConsoleApp1
{
class CrawlingTask
{
public string UrlToCrawl { get; set; } public string ProducerName { get; set; }
}
}

CrawlingTask

using System.Collections.Generic;

namespace ConsoleApp1
{
static class Module
{
public static Dictionary<string, string[]> _contentEmulation = new Dictionary<string, string[]>(); public static void CreateLinks()
{
_contentEmulation["http://microsoft.com/"] = new[] { "http://microsoft.com/a.html", "http://microsoft.com/b.html" };
_contentEmulation["http://microsoft.com/a.html"] = new[] { "http://microsoft.com/c.html", "http://microsoft.com/d.html" };
_contentEmulation["http://microsoft.com/b.html"] = new[] { "http://microsoft.com/e.html" }; _contentEmulation["http://google.com/"] = new[] { "http://google.com/a.html", "http://google.com/b.html" };
_contentEmulation["http://google.com/a.html"] = new[] { "http://google.com/c.html", "http://google.com/d.html" };
_contentEmulation["http://google.com/b.html"] = new[] { "http://google.com/e.html", "http://google.com/f.html" };
_contentEmulation["http://google.com/c.html"] = new[] { "http://google.com/h.html", "http://google.com/i.html" }; _contentEmulation["http://facebook.com/"] = new[] { "http://facebook.com/a.html", "http://facebook.com/b.html" };
_contentEmulation["http://facebook.com/a.html"] = new[] { "http://facebook.com/c.html", "http://facebook.com/d.html" };
_contentEmulation["http://facebook.com/b.html"] = new[] { "http://facebook.com/e.html" }; _contentEmulation["http://twitter.com/"] = new[] { "http://twitter.com/a.html", "http://twitter.com/b.html" };
_contentEmulation["http://twitter.com/a.html"] = new[] { "http://twitter.com/c.html", "http://twitter.com/d.html" };
_contentEmulation["http://twitter.com/b.html"] = new[] { "http://twitter.com/e.html" };
_contentEmulation["http://twitter.com/c.html"] = new[] { "http://twitter.com/f.html", "http://twitter.com/g.html" };
_contentEmulation["http://twitter.com/d.html"] = new[] { "http://twitter.com/h.html" };
_contentEmulation["http://twitter.com/e.html"] = new[] { "http://twitter.com/i.html" };
}
}
}

Module

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Module.CreateLinks();
Task t = RunProgram();
t.Wait();
} static async Task RunProgram()
{
var bag = new ConcurrentBag<CrawlingTask>(); string[] urls = new[] { "http://microsoft.com/", "http://google.com/", "http://facebook.com/", "http://twitter.com/" }; var crawlers = new Task[];
for (int i = ; i <= ; i++)
{
string crawlerName = "Crawler " + i.ToString();
bag.Add(new CrawlingTask { UrlToCrawl = urls[i - ], ProducerName = "root" });
crawlers[i - ] = Task.Run(() => Crawl(bag, crawlerName));
} await Task.WhenAll(crawlers);
} static async Task Crawl(ConcurrentBag<CrawlingTask> bag, string crawlerName)
{
CrawlingTask task;
//尝试从bag中取出对象
while (bag.TryTake(out task))
{
IEnumerable<string> urls = await GetLinksFromContent(task);
if (urls != null)
{
foreach (var url in urls)
{
var t = new CrawlingTask
{
UrlToCrawl = url,
ProducerName = crawlerName
};
//将子集插入到bag中
bag.Add(t);
}
}
Console.WriteLine("Indexing url {0} posted by {1} is completed by {2}!",
task.UrlToCrawl, task.ProducerName, crawlerName);
}
} static async Task<IEnumerable<string>> GetLinksFromContent(CrawlingTask task)
{
await GetRandomDelay(); if (Module._contentEmulation.ContainsKey(task.UrlToCrawl)) return Module._contentEmulation[task.UrlToCrawl]; return null;
} static Task GetRandomDelay()
{
int delay = new Random(DateTime.Now.Millisecond).Next(, );
return Task.Delay(delay);
} }
}

Program

6、BlockingCollection类
  BlockingCollection类: 我们能够改变任务存储在阻塞集合中的方式。默认情况下它使用的是ConcurrentQueue容器,但是我们能够使用任何实现了IProducerConsumerCollection泛型接口的集合。

namespace ConsoleApp1
{
class CustomTask
{
public int Id { get; set; }
}
}

CustomTask

using System;
using System.Threading.Tasks; namespace ConsoleApp1
{
static class Module
{
public static Task GetRandomDelay()
{
int delay = new Random(DateTime.Now.Millisecond).Next(, );
return Task.Delay(delay);
}
}
}

Module

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Using a Queue inside of BlockingCollection");
Console.WriteLine();
Task t = RunProgram();
t.Wait(); //Console.WriteLine();
//Console.WriteLine("Using a Stack inside of BlockingCollection");
//Console.WriteLine();
//Task t = RunProgram(new ConcurrentStack<CustomTask>());
//t.Wait();
} static async Task RunProgram(IProducerConsumerCollection<CustomTask> collection = null)
{
var taskCollection = new BlockingCollection<CustomTask>();
if (collection != null)
taskCollection = new BlockingCollection<CustomTask>(collection);
//初始化collection中的数据
var taskSource = Task.Run(() => TaskProducer(taskCollection)); Task[] processors = new Task[];
for (int i = ; i <= ; i++)
{
string processorId = "Processor " + i;
processors[i - ] = Task.Run(
() => TaskProcessor(taskCollection, processorId));
} await taskSource; await Task.WhenAll(processors);
}
/// <summary>
/// 初始化collection中的数据
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
static async Task TaskProducer(BlockingCollection<CustomTask> collection)
{
for (int i = ; i <= ; i++)
{
await Task.Delay();
var workItem = new CustomTask { Id = i };
collection.Add(workItem);
Console.WriteLine("Task {0} has been posted", workItem.Id);
}
collection.CompleteAdding();
}
/// <summary>
/// 打印collection中的数据
/// </summary>
/// <param name="collection"></param>
/// <param name="name"></param>
/// <returns></returns>
static async Task TaskProcessor(
BlockingCollection<CustomTask> collection, string name)
{
await Module.GetRandomDelay();
foreach (CustomTask item in collection.GetConsumingEnumerable())
{
Console.WriteLine("Task {0} has been processed by {1}", item.Id, name);
await Module.GetRandomDelay();
}
}
}
}

Program

7、使用ThreadStatic特性
  ThreadStatic特性是最简单的TLS使用,且只支持静态字段,只需要在字段上标记这个特性就可以了

using System;
using System.Threading; namespace ConsoleApp1
{
class Program
{
//TLS中的str变量
//可以看到,str静态字段在两个线程中都是独立存储的,互相不会被修改。
[ThreadStatic]
static string str = "hehe"; static void Main(string[] args)
{
//另一个线程只会修改自己TLS中的hehe
Thread th = new Thread(() => { str = "Mgen"; Display(); });
th.Start();
th.Join();
Display();
}
static void Display()
{
Console.WriteLine("{0} {1}", Thread.CurrentThread.ManagedThreadId, str);
} }
}

Program

8、使用命名的LocalDataStoreSlot类型
  显然ThreadStatic特性只支持静态字段太受限制了。.NET线程类型中的LocalDataStoreSlot提供更好的TLS支持。我们先来看看命名的LocalDataStoreSlot类型,可以通过Thread.AllocateNamedDataSlot来分配一个命名的空间,通过Thread.FreeNamedDataSlot来销毁一个命名的空间。空间数据的获取和设置则通过Thread类型的GetData方法和SetData方法。

using System;
using System.Threading; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建Slot
LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot("slot"); //设置TLS中的值
Thread.SetData(slot, "hehe"); //修改TLS的线程
Thread th = new Thread(() =>
{
Thread.SetData(slot, "Mgen");
Display();
}); th.Start();
th.Join();
Display(); //清除Slot
Thread.FreeNamedDataSlot("slot");
} //显示TLS中Slot值
static void Display()
{
LocalDataStoreSlot dataslot = Thread.GetNamedDataSlot("slot");
Console.WriteLine("{0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.GetData(dataslot));
} }
}

Program

9、使用未命名的LocalDataStoreSlot类型
  线程同样支持未命名的LocalDataStoreSlot,未命名的LocalDataStoreSlot不需要手动清除,分配则需要Thread.AllocateDataSlot方法。注意由于未命名的LocalDataStoreSlot没有名称,因此无法使用Thread.GetNamedDataSlot方法,只能在多个线程中引用同一个LocalDataStoreSlot才可以对TLS空间进行操作,将上面的命名的LocalDataStoreSlot代码改成未命名的LocalDataStoreSlot执行

using System;
using System.Threading; namespace ConsoleApp1
{
class Program
{
//静态LocalDataStoreSlot变量
static LocalDataStoreSlot slot; static void Main(string[] args)
{
//创建Slot
slot = Thread.AllocateDataSlot(); //设置TLS中的值
Thread.SetData(slot, "hehe"); //修改TLS的线程
Thread th = new Thread(() =>
{
Thread.SetData(slot, "Mgen");
Display(); }); th.Start();
th.Join();
Display();
} //显示TLS中Slot值
static void Display()
{
Console.WriteLine("{0} {1}", Thread.CurrentThread.ManagedThreadId, Thread.GetData(slot));
} }
}

Program

10、使用.NET 4.0的ThreadLocal<T>类型
  .NET 4.0在线程方面加入了很多东西,其中就包括ThreadLocal<T>类型,他的出现更大的简化了TLS的操作。ThreadLocal<T>类型和Lazy<T>惊人相似,构造函数参数是Func<T>用来创建对象(当然也可以理解成对象的默认值),然后用Value属性来得到或者设置这个对象。
  ThreadLocal的操作或多或少有点像上面的未命名的LocalDataStoreSlot,但ThreadLocal感觉更简洁更好理解。

using System;
using System.Threading; namespace ConsoleApp1
{
class Program
{
static ThreadLocal<string> local; static void Main(string[] args)
{
//创建ThreadLocal并提供默认值
local = new ThreadLocal<string>(() => "hehe"); //修改TLS的线程
Thread th = new Thread(() =>
{ local.Value = "Mgen";
Display();
}); th.Start();
th.Join();
Display();
} //显示TLS中数据值
static void Display()
{
Console.WriteLine("{0} {1}", Thread.CurrentThread.ManagedThreadId, local.Value);
} }
}

Program

C#线程安全类型的更多相关文章

  1. EsRejectedExecutionException排错与线程池类型

    1.EsRejectedExecutionException异常示例 java.util.concurrent.ExecutionException: RemoteTransportException ...

  2. ElasticSearch 线程池类型分析之SizeBlockingQueue

    ElasticSearch 线程池类型分析之SizeBlockingQueue 尽管前面写好几篇ES线程池分析的文章(见文末参考链接),但都不太满意.但从ES的线程池中了解到了不少JAVA线程池的使用 ...

  3. ElasticSearch 线程池类型分析之 ExecutorScalingQueue

    ElasticSearch 线程池类型分析之 ExecutorScalingQueue 在ElasticSearch 线程池类型分析之SizeBlockingQueue这篇文章中分析了ES的fixed ...

  4. ElasticSearch 线程池类型分析之 ResizableBlockingQueue

    ElasticSearch 线程池类型分析之 ResizableBlockingQueue 在上一篇文章 ElasticSearch 线程池类型分析之 ExecutorScalingQueue的末尾, ...

  5. Java并发:五种线程安全类型、线程安全的实现、枚举类型

    1. Java中的线程安全 Java线程安全:狭义地认为是多线程之间共享数据的访问. Java语言中各种操作共享的数据有5种类型:不可变.绝对线程安全.相对线程安全.线程兼容.线程独立 ① 不可变 不 ...

  6. 进程和线程及Linux下的编程

    一.概述 进程和线程网络上有一堆解释,我不喜欢抄袭,也不喜欢套用太教科书的说法.就以我自己的理解来说说进程和线程吧,当然自己的理解肯定不是很严谨,但是理解起来应该会比教科书快一点.进程和线程都可以认为 ...

  7. C#中的线程(二) 线程同步基础

    1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具:                       简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...

  8. mina2线程详解

    1主要流程 read  in  data: IO读入(IoProcessor)---日志记录.解码.threadPool(IoFilter)---业务逻辑处理(IoHandler) write  ou ...

  9. COM中的线程模式

      Choosing the threading model for an object depends on the object's function. An object that does e ...

随机推荐

  1. retrofit+rxjava封装

    public class RetrofitHelper { private static OkHttpClient okHttpClient; private static ServiceAPI se ...

  2. 【Solidity】学习(1)

    string string类型不可以直接通过length读取字符串长度,也不可以直接通过下标直接访问数据元素 使用的方法是:强制类型转换为bytes 其中," " 和‘ ’都可以表 ...

  3. 设置npm的registry

    .原npm地址 npm config set registry http://registry.npmjs.org .设置国内镜像 a.通过config命令 npm config set regist ...

  4. unigui ShowModal、MessageDlg

    procedure Calback(Sender: TComponent; AResult: Integer);procedure TForm.Calback(Sender: TComponent; ...

  5. Java-异常初步练习

    案例一: package com.esandinfo; /** * 自定义一个Exception类 */ class MyCustomException extends RuntimeExceptio ...

  6. 弹性盒子模型属性之flex-grow

    在学习弹性盒子模型的时候,有几个属性常常让同学们感觉头痛, 不知到最后得到的效果数值到底是怎样计算得来的,那么不要慌,稳住,我们能赢 !!!今天就让我们先来看看flex-grow这个属性 flex-g ...

  7. 40个Java多线程面试问题

    1. 多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓"知其然知其所以然","会用"只 ...

  8. Android Studio 常见问题及解决方法

    一.Error:All flavors must now belong to a named flavor dimension 问题描述: Error:All flavors must now bel ...

  9. Javascript高级编程学习笔记(68)—— 事件(12)设备事件

    设备事件 随着智能手机与平板电脑的普及,为了更好地让用户与这些设备进行交互 浏览器引入了一种新的方式,而一类新的事件也应运而生,这就是设备事件 W3C从2011年开始制定关于设备事件的草案 下面将会介 ...

  10. Java 线程池的原理及实现

    1.线程池简介: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线 ...