-
多线程
编辑
-
中文名
- 多线程 外文名
- multithreading
-
用 途
- 实现多个线程并发执行的技术 对 象
- 计算机
目录
-
线程切换能够在一个 CPU 周期内完成(有些硬件甚至没有开销,上个周期在运行线程A,下个周期就已在运行线程B)。
-
每个线程看起来就像是独自运行的,即没有与其他线程共享硬件资源。对操作系统来说,通常每个线程都被视做独占一个处理器,这样将简化系统软件的设计(尤其是对于支持多线程的操作系统)。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
usingSystem;
usingSystem.Threading;
namespaceSynchronizationThreadsExample
{
classSynchronizationThreadsExample
{
privateintcounter=0;
staticvoidMain()
{
SynchronizationThreadsExampleSTE=newSynchronizationThreadsExample();
STE.ThreadFunction();
}
publicvoidThreadFunction()
{
ThreadDummyThread=newThread(newThreadStart(SomeFunction));
DummyThread.IsBackground=
true
;
DummyThread.Start();
Console.WriteLine(
"Startedthread"
);
ThreadDummyPriorityThread=newThread(newThreadStart(SomeFunction));
DummyPriorityThread.IsBackground=
true
;
//="SecondThread";
DummyPriorityThread.Start();
Console.WriteLine(
"Startedthread"
);
DummyThread.Join();
DummyPriorityThread.Join();
}
publicvoidSomeFunction()
{
try
{
while
(counter<10)
{
inttempCounter=counter;
tempCounter++;
Thread.Sleep(1);
counter=tempCounter;
Console.WriteLine(
"Thread.SomeFunction:"
+Thread.+counter);
}
}
catch
(ThreadInterruptedExceptionEx)
{
Console.WriteLine(
"Exceptioninthread"
+Thread.);
}
finally
{
Console.WriteLine(
"ThreadExiting."
+Thread);
}
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
classMyThreadextendsThread{
publicvoidrun(){
//这里写上线程的内容
}
publicstaticvoidmain(String[]args){
//使用这个方法启动一个线程
(newMyThread()).start();
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
classMyThreadimplementsRunnable{
publicvoidrun(){
//这里写上线程的内容
}
publicstaticvoidmain(String[]args){
//使用这个方法启动一个线程
(newThread(newMyThread())).start();
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include<thread>
usingnamespacestd;
voidthreadFunc(){
//这里写上线程的内容
}
intmain(){
threadt(threadFunc);
//启动线程
t.join();
//等待线程运行完毕
return0;
}
|
| 概论 |
|
|---|
| 方式 |
|
|---|
| 理论 |
|
|---|
| 元素 |
|
|---|
| 协调 |
|
|---|
| 编程 |
|
|---|
| APIs |
|
|---|
| 问题 |
|
|---|
-
参考资料

7648



被折叠的 条评论
为什么被折叠?



