建立兩條執行續,每一條都對count+1=>執行1萬次,當每條執行續執行完成後,預計Count結果會是2萬
因此我們可以透過一些管控機制來使得Count會是預期的結果2萬次,這樣的行為就可以稱為同步化
SampleCode:
public static int count = 0;
void Main()
{
var t1 =new Thread(() => {AddCount();});
var t2 =new Thread(() => {AddCount();});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine($"最終Count 數字為 = {count}");
}
public void AddCount()
{
for (int i = 0; i < 10000; i++)
{
count++;
}
}
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing