Re: [問題] synchronized 和 multi thread
※ 引述《jehovah (Lucius)》之銘言:
: 大家好, 小弟對multi thread還不熟悉, 想請教一個問題
: 目前我有A, B兩條thread, 以及一個公用的arraylist
: A thread會做以下的工作:
: arraylist.remove(old_index);
: ...一些運算
: arraylist.add(new_index, obj);
: B thread則會對arraylist作get
: 因此A在作add前, B有機會IndexOutOfBounds
: 我查了synchronized修飾字,
: 將A的工作用synchronized包起來, 可是沒有幫助
: synchronized(arraylist){
: ...
: }
: 我是希望鎖住arraylist這個物件, 而不是操作這物件的Method
: 請問一般來說, 這種狀況如何處理較恰當?
: 可否給我點建議, 或是該往哪個方向去查..謝謝:)
雖然看不很懂你想做的是什麼,不過看來你是誤會了
synchronized (或其他locking 方法)的用意。
(看來其他人也沒有提及)
你不止要把 A 的工作 synchronized ,B 也需要。
synchronized 可以想成是一個協同方法而已,並
不是你把 A 的工作利用 synchronized 包起來就
行,B 做的動作 (比如你說的 get ) 也要
synchronize,B 才會乖乖等 A 相關的動作做完
才執行。
比如:
A:
synchronized(arrayList) {
arrayList.remove(something);
// do something else
arrayList.add(something);
}
B:
synchronized(arrayList) {
arrayList.get(index);
}
這樣才行。
搞清楚這裡,就可以再下一步了:
利用 concurrent package 的 lock 的概念也類似,
只是 lock & unlock 要explictly 做,而做 lock
的目標也不是 arrayList 本身,而是一個 “代表”
arrayList 的 lock obj:
A:
arrayListLock.lock();
try {
arrayList.remove(something);
// do something else
arrayList.add(something);
} finally {
arrayListLock.unlock();
}
B:
arrayListLock.lock();
try {
arrayList.get(index);
} finally {
arrayListLock.unlock();
}
這步搞得通嗎?
搞得通的話,再下一步:
你寫的東西,如果將來會常有多 thread 一起讀 (B),
偶然才會 update (A),那麼用 reader writer lock
是一個好選擇:
A:
// ReadWriteLock arrayListLock
arrayListLock.writeLock().lock();
try {
arrayList.remove(something);
// do something else
arrayList.add(something);
} finally {
arrayListLock.writeLock()unlock();
}
B:
arrayListLock.readLock().lock();
try {
arrayList.get(index);
} finally {
arrayListLock.readLock().unlock();
}
這一步還可以嗎?
然後到最後一步。
雖然到處都自己 lock 是可以跑,但 maintain
起來可不是一件好事。視乎你的設計,你可以
考慮大家不是直接操作 arrayList, 而是把相
關的 business logic 包起來。比如,arrayList
放的是學生資料,那麼,倒不如弄一個 StudentRepository.
各 thread 是操作 StudentRepository:
interface StudentRepository {
void updateStudent(Student student);
Student getStudent(int index);
}
class StudentRepositoryImpl implements StudentRepository {
List<Student> students;
ReadWriteLock repoLock;
public void updateStudent(Student student) {
// 就是本來在 A 裡面操作 arrayList 的邏輯
repoLock.writeLock().lock();
try {
students.remove(something);
// do something else
students.add(something);
} finally {
repoLock.writeLock().unlock();
}
}
public Student getStudent(int index) {
// 本來在 thread B 裡操作arraylist 的部份
repoLock.readLock().lock();
try {
return students.get(index);
} finally {
repoLock.readLock.unlock();
}
}
}
A:
repo.updateStudent(student);
//其他有的沒的
B:
student = repo.getStudent(i);
// 對 student 作其他操作 etc.
這樣既可把 arrayList 的操作包起來,你也可以隨便
選用/改用 synchronization 的策略 (直接用 synchronized,
或用 ReentrantLock, 或用 Reader Writer Lock etc)
。整體設計更是整齊許多.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 183.179.61.91
推
10/05 14:17, , 1F
10/05 14:17, 1F
推
10/06 10:20, , 2F
10/06 10:20, 2F
推
10/06 12:31, , 3F
10/06 12:31, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):