database
作業系統學到的同步化知識,Java 內建就有,以下讓我們窺其一二。
建立執行緒
public class SomeRunnable implements Runnable {
@Override
public void run() { /* ... */ }
}
public class SomeThread extends Thread {
@Override
public void run() { /* ... */ }
}
var runnableThread = new Thread(new SomeRunnable());
runnableThread.start();
var myThread = new SomeThread();
myThread.start();
新執行緒的 Function Call 會在新的 Stack 上運作,與當前 Stack 分離。
有別於 Fork Process,Heap 上的記憶體是各個 Thread 共享的(Share Memory),故會有 Race Condition。
Java 提供兩種 Race Condition 的解決方法。
以下兩者等價。
public class MonitorCounter {
private int shared_var;
public synchronized void set(int val) {
shared_var = val;
}
public synchronized int get() {
return shared_var;
}
}
this
放入就等於一次只能有一個 Thread 呼叫這倆要同步的方法。
public class SyncCounter {
private int shared_var;
public void set(int val) {
synchronized(this) {
shared_var = val;
}
}
public synchronized int get() {
synchronized(this) {
return shared_var;
}
}
}
不過最重要的是,要清楚 Critical Section 真正的範圍。以下就算用上面方法上同步化的物件也沒用。
counter = new SyncCounter(); // shared_var = 0
// Thread 1
int c = counter.get();
++c;
counter.set(c);
// Thread 2
int c = counter.get();
--c;
counter.set(c);
// The result of shared_var can be 0, 1, -1...
// because te real critical section is the whole get-and-set process!
解決方法有二,一是使用者把使用者的操作也納入 Critical Section,二是物件提供同步化的 get-and-set 的方法。
wait
和 notify
Java 的所有物件都內建 semaphore 的邏輯!!!
如果某 Thread 得不到需要的物件資源,可以主動呼叫該物件的 Object::wait
方法將執行權讓給其他 Thread;當某 Thread 釋放此物件資源時可以呼叫 Object::notifyAll
方法喚醒「所有」在等待此資源的 Thread,當中的「某一個」物件將會獲得此物件的使用權。注意這裡說「物件的使用權」是自訂義的,Java 並不會限制物件的使用。
另注意,Object::wait
方法總是習慣被包在回圈中,因為只有「某一個」想使用物件的 Thread 可以真的獲得之。
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.
Do you want to remove this version name and description?
Syncing