C# 线程 线程池 线程同步 异步委托 后台线程 实例
1. [代码][C#]代码
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Text; |
04 |
using System.Threading; |
05 |
06 |
namespace 线程Thread |
07 |
{ |
08 |
class Program |
09 |
{ |
10 |
11 |
public class MyThread |
12 |
{ |
13 |
private string data; |
14 |
public MyThread(string data) |
15 |
{ |
16 |
this.data = data; |
17 |
} |
18 |
public void ThreadMain() |
19 |
{ |
20 |
Console.WriteLine("Running in a thread,data :{0}", data); |
21 |
} |
22 |
} |
23 |
24 |
static void Main(string[] args) |
25 |
{ |
26 |
/* |
27 |
* 第一种方法 |
28 |
//ThreadStart委托定义了一个返回类型为void 的无参数方法.在创建了Thread对象后,就可以使用start()方法启动线程 |
29 |
var t1 = new Thread(ThreadMain); |
30 |
t1.Start(); |
31 |
Console.WriteLine("This is the main thread!"); |
32 |
*/ |
33 |
/* |
34 |
//第二种方法 |
35 |
var t1 = new Thread(() => |
36 |
Console.WriteLine("running in a thread ,id :{0}", Thread.CurrentThread.ManagedThreadId)); |
37 |
t1.Start(); |
38 |
Console.WriteLine("This is the main thread ,id :{0}", Thread.CurrentThread.ManagedThreadId); |
39 |
*/ |
40 |
41 |
42 |
43 |
//给线程传递数据 方法1 |
44 |
/* |
45 |
var d = new Data { Message = "Info" }; |
46 |
var t2 = new Thread(ThreadMainWithParameters); |
47 |
|
48 |
t2.Start(d); |
49 |
*/ |
50 |
//给线程传递数据 方法2 |
51 |
/* |
52 |
var obj = new MyThread("info"); |
53 |
var t3 = new Thread(obj.ThreadMain); |
54 |
t3.Start(); |
55 |
*/ |
56 |
57 |
//后台线程 测试后台线程请去掉 Console.Read(); |
58 |
var t1 = new Thread(ThreadMain) { Name = "MyNewThreaad", IsBackground =true }; |
59 |
t1.Start(); |
60 |
Console.WriteLine("Main thread ending now."); |
61 |
62 |
63 |
64 |
65 |
|
66 |
67 |
// Console.Read(); |
68 |
} |
69 |
public struct Data |
70 |
{ |
71 |
public string Message; |
72 |
} |
73 |
static void ThreadMainWithParameters(object o) |
74 |
{ |
75 |
Data d = (Data)o; |
76 |
Console.WriteLine("Running in a thread,received {0}", d.Message); |
77 |
} |
78 |
79 |
80 |
static void ThreadMain() |
81 |
{ |
82 |
Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name); |
83 |
Thread.Sleep(3000); |
84 |
Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name); |
85 |
//Console.WriteLine("Running in a thread."); |
86 |
} |
87 |
88 |
89 |
90 |
} |
91 |
} |
2. [代码][C#]代码
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Text; |
04 |
using System.Threading; |
05 |
06 |
namespace 线程池 |
07 |
{ |
08 |
class Program |
09 |
{ |
10 |
static void Main(string[] args) |
11 |
{ |
12 |
/* |
13 |
1、线程池中所有线程都是后台线程,且不能更改 |
14 |
2、不能给入池的线程设置优先级或名称 |
15 |
3、对于COM对象 入池的线程都是多线程单元 (multithreaded apartment ,MTA)线程 |
16 |
* 许多COM对象需要单线程单元(single-threaded apartment,STA)线程 |
17 |
4、入池的线程只能用于较短的任务 |
18 |
*/ |
19 |
20 |
int nWorkerThreads; |
21 |
int nCompletionPortThreads; |
22 |
ThreadPool.GetMaxThreads(out nWorkerThreads, out nCompletionPortThreads); |
23 |
Console.WriteLine("Max worker thread:{0} ," + |
24 |
"I/O completion threads:{1}", |
25 |
nWorkerThreads, nCompletionPortThreads); |
26 |
for (int i = 0; i < 5; i++) |
27 |
{ |
28 |
ThreadPool.QueueUserWorkItem(JobForAThread); |
29 |
} |
30 |
Console.Read(); |
31 |
} |
32 |
static void JobForAThread(object state) |
33 |
{ |
34 |
for (int i = 0; i < 3; i++) |
35 |
{ |
36 |
Console.WriteLine("loop {0} ,running inside pooled thread {1} ", i, Thread.CurrentThread.ManagedThreadId); |
37 |
Thread.Sleep(50); |
38 |
} |
39 |
} |
40 |
} |
41 |
} |
3. [代码][C#]代码
01 |
using System; |
02 |
using System.Collections.Generic; |
03 |
using System.Text; |
04 |
using System.Threading; |
05 |
06 |
namespace 线程同步 |
07 |
{ |
08 |
class Program |
09 |
{ |
10 |
11 |
12 |
//lock 语句是设置锁定和解除锁定的一种简单方式 |
13 |
//用lock语句定义的对象表示,要等待指定对象的锁定解除 只能传送引用类型 因为锁定值类型只是锁定了一个副本 |
14 |
public class SharedState |
15 |
{ |
16 |
private int state = 0; |
17 |
private object syncRoot = new object(); |
18 |
19 |
public int State |
20 |
{ |
21 |
get |
22 |
{ |
23 |
lock (syncRoot) |
24 |
{ |
25 |
return state; |
26 |
} |
27 |
} |
28 |
set |
29 |
{ |
30 |
lock (syncRoot) |
31 |
{ |
32 |
state = value; |
33 |
} |
34 |
} |
35 |
} |
36 |
37 |
} |
38 |
public class Task |
39 |
{ |
40 |
41 |
SharedState sharedstate; |
42 |
public Task( SharedState sharedstate) |
43 |
{ |
44 |
this.sharedstate = sharedstate; |
45 |
} |
46 |
public void DoTheTask() |
47 |
{ |
48 |
for (int i = 0; i < 50000; i++) |
49 |
{ |
50 |
lock (sharedstate) |
51 |
{ |
52 |
sharedstate.State += 1; |
53 |
} |
54 |
} |
55 |
} |
56 |
} |
57 |
58 |
static void Main1(string[] args) |
59 |
{ |
60 |
int numThreads = 20; |
61 |
SharedState sharedState = new SharedState(); |
62 |
Thread[] threads = new Thread[numThreads]; |
63 |
for (int i = 0; i < numThreads; i++) |
64 |
{ |
65 |
threads[i] = new Thread(new Task(sharedState).DoTheTask); |
66 |
threads[i].Start(); |
67 |
} |
68 |
for (int i = 0; i < numThreads; i++) |
69 |
{ |
70 |
threads[i].Join(); |
71 |
} |
72 |
Console.WriteLine("summarized {0} ", sharedState.State); |
73 |
Console.Read(); |
74 |
} |
75 |
76 |
} |
77 |
} |
4. [代码][C#]代码 跳至 [1] [2] [3] [4] [全屏预览]
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Text; |
004 |
using System.Threading; |
005 |
006 |
007 |
008 |
009 |
namespace 线程同步 |
010 |
{ |
011 |
class tbInterlocked |
012 |
{ |
013 |
//Interlocked 类用于使变量的简单语句原子化 |
014 |
/* |
015 |
* Increment() 方法递增一个变量,把结果存储到一个原子操作中 |
016 |
* Decrement() 方法递减一个变量,并存储结果 |
017 |
* Exchange() Exchange()将一个变量设置为指定的值,并返回变量的初始值 |
018 |
* CompareExchange() 对两个变量进行相等比较,如果他们相等,就设置指定的值,返回初始值 |
019 |
* Add() Add()对 两个值进行相加操纵 |
020 |
* Read() 用于在一个原子操作中从内存读取64位值。在32为操作系统中,读取64位不是原子化的,而需要从两个内存地址中读取 |
021 |
*/ |
022 |
public class SharedState |
023 |
{ |
024 |
private int state = 0; |
025 |
public int State |
026 |
{ |
027 |
get |
028 |
{ |
029 |
return Interlocked.Increment(ref state); |
030 |
} |
031 |
} |
032 |
033 |
|
034 |
private int decrement = 0; |
035 |
/// <summary> |
036 |
/// 这里是Decrment的实例 |
037 |
/// </summary> |
038 |
public int Decrement |
039 |
{ |
040 |
get |
041 |
{ |
042 |
Console.WriteLine("decrement 的初始值为 {0}", decrement); |
043 |
Interlocked.Decrement(ref decrement); |
044 |
Console.WriteLine("调用interlocked.Decrement后的结果为{0}", decrement); |
045 |
return decrement; |
046 |
} |
047 |
|
048 |
} |
049 |
private int exchange = 0; |
050 |
/// <summary> |
051 |
/// 这里是Exchange的实例 |
052 |
/// </summary> |
053 |
public int Exchange |
054 |
{ |
055 |
get |
056 |
{ |
057 |
Console.WriteLine("exchange 的初始值为 {0}", exchange); |
058 |
Interlocked.Exchange(ref exchange, 10); |
059 |
Console.WriteLine("调用interlocked.Exchange后的结果为{0}", exchange); |
060 |
return exchange; |
061 |
} |
062 |
} |
063 |
} |
064 |
public class Task |
065 |
{ |
066 |
SharedState sharedstate; |
067 |
public Task(SharedState sharedstate) |
068 |
{ |
069 |
this.sharedstate = sharedstate; |
070 |
} |
071 |
public void ToTheTask() |
072 |
{ |
073 |
for (int i = 0; i < 50000; i++) |
074 |
{ |
075 |
|
076 |
int j= sharedstate.State; |
077 |
} |
078 |
} |
079 |
} |
080 |
081 |
static void Main2() |
082 |
{ |
083 |
084 |
int numThreads = 20; |
085 |
SharedState state = new SharedState(); |
086 |
Thread[] threads = new Thread[numThreads]; |
087 |
088 |
for (int i = 0; i < numThreads; i++) |
089 |
{ |
090 |
threads[i] = new Thread(new Task(state).ToTheTask); |
091 |
threads[i].Start(); |
092 |
} |
093 |
for (int i = 0; i < numThreads; i++) |
094 |
{ |
095 |
threads[i].Join(); |
096 |
} |
097 |
Console.WriteLine("结果 summarized {0}", state.State - 1); |
098 |
Console.WriteLine("结果 +1 summarized {0}", state.State); |
099 |
Console.WriteLine("调用2次后结果summarized {0}", state.State); |
100 |
101 |
102 |
int iTest= state.Decrement; |
103 |
104 |
iTest = state.Exchange; |
105 |
106 |
Console.Read(); |
107 |
} |
108 |
} |
109 |
} |

浙公网安备 33010602011771号