t*****a 发帖数: 106 | 1 加mutex是对的,不好意思,看错了。开始想着用while+wait能按顺序输出HHO,发现不
行。写了个code,贴在这
public class H2O {
private Lock lock;
private Semaphore semH;
private Semaphore semO;
H2O()
{
lock=new ReentrantLock();
semH=new Semaphore(0);
semO=new Semaphore(0);
}
public void H() throws InterruptedException
{
semH.release();
semO.acquire();
System.out.println("H");
}
public void O() throws InterruptedException
{
lock.lock();
semH.acquire();
semH.acquire();
lock.unlock();
semO.release();
semO.release();
System.out.println("O");
}
... 阅读全帖 |
|
h**u 发帖数: 144 | 2 用了两个blocking queue, 还请大侠指教!
import java.util.concurrent.LinkedBlockingQueue;
public class H2O {
LinkedBlockingQueue hQueue = new LinkedBlockingQueue();
LinkedBlockingQueue oQueue = new LinkedBlockingQueue();
Object o = new Object();
public void h() throws InterruptedException {
hQueue.put(Thread.currentThread());
synchronized (o){
System.out.println(Thread.currentThread().getName() + ".h,
notify");
o.notify()... 阅读全帖 |
|
h**u 发帖数: 144 | 3 用了两个blocking queue, 还请大侠指教!
import java.util.concurrent.LinkedBlockingQueue;
public class H2O {
LinkedBlockingQueue hQueue = new LinkedBlockingQueue();
LinkedBlockingQueue oQueue = new LinkedBlockingQueue();
Object o = new Object();
public void h() throws InterruptedException {
hQueue.put(Thread.currentThread());
synchronized (o){
System.out.println(Thread.currentThread().getName() + ".h,
notify");
o.notify()... 阅读全帖 |
|
z****e 发帖数: 54598 | 4 因为wait的monitor在l上,而不是在ti上
这个问题可以单独剥离出来看,如果写成一堆的话
那就非常不容易阅读了,这是代码
List l = new ArrayList();
public void h() throws InterruptedException {
ThreadInfo ti =null;
synchronized(l){
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){
hThrea... 阅读全帖 |
|
z****e 发帖数: 54598 | 5 因为wait的monitor在l上,而不是在ti上
这个问题可以单独剥离出来看,如果写成一堆的话
那就非常不容易阅读了,这是代码
List l = new ArrayList();
public void h() throws InterruptedException {
ThreadInfo ti =null;
synchronized(l){
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){
hThrea... 阅读全帖 |
|
z*******3 发帖数: 13709 | 6 List l = new ArrayList();
public synchronized void h() throws InterruptedException {
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){hThread.notify();}
synchronized(oThread){oThread.notify();}
l.remove(hThread);
l.remove(oThread);
ret... 阅读全帖 |
|
z*******3 发帖数: 13709 | 7 List l = new ArrayList();
public synchronized void h() throws InterruptedException {
if (l.size() >= 2) {
ThreadInfo hThread = l.get(0);
ThreadInfo oThread = l.get(l.size() - 1);
if (hThread.isH() && oThread.isO()) {
synchronized(hThread){hThread.notify();}
synchronized(oThread){oThread.notify();}
l.remove(hThread);
l.remove(oThread);
ret... 阅读全帖 |
|
z*******3 发帖数: 13709 | 8 原题:
3: 实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
我随手写了一个,不想用synchronized关键字
这个要搞起来的确比较麻烦,想上java.util.concurrent
研究了一下copyonwritearraylist,还是不行,两个方法都得上synchronized
List l = new ArrayList();
public synchronized void h(){
if(l.size()>=2){
Thread hThread = l.get(0);
Thread oThread = l.get(l.size()-1);
if(hThread.isH() && oThread.isO()){
hThread.notify();
oThread.notify();
return;
}
}
Thread currentThre... 阅读全帖 |
|
|
z*******3 发帖数: 13709 | 9 或者找个map寄存
不过增加的map自然增加了查找的时间
效率上不如包装类
List l= new ArrayList();
Map m= new HashMap();
public synchronized void h(){
if(l.size()>=2){
Thread hThread = l.get(0);
Thread oThread = l.get(l.size()-1);
if(m.get(hThread) && !m.get(oThread)){
hThread.notify();
oThread.notify();
return;
}
}
Thread currentThread = Thread.getCurrentThread();
l.add(0,currentThread);
m.put(currentThead, true);
currentThread.wait();
}
public synchronized void o(){
if(l.size()>=... 阅读全帖 |
|
z*******3 发帖数: 13709 | 10 汗,实验了一下,有三个小问题,看来多线程白板还是难以bug free,需要多练习
getCurrentThread() -> currentThread()
currentThread.wait() -> wait()
hThread.notify() -> synchronized(hThread){ hThread.notify();} |
|
z*******3 发帖数: 13709 | 11 原题:
3: 实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
我随手写了一个,不想用synchronized关键字
这个要搞起来的确比较麻烦,想上java.util.concurrent
研究了一下copyonwritearraylist,还是不行,两个方法都得上synchronized
List l = new ArrayList();
public synchronized void h(){
if(l.size()>=2){
Thread hThread = l.get(0);
Thread oThread = l.get(l.size()-1);
if(hThread.isH() && oThread.isO()){
hThread.notify();
oThread.notify();
return;
}
}
Thread currentThre... 阅读全帖 |
|
z*******3 发帖数: 13709 | 12 或者找个map寄存
不过增加的map自然增加了查找的时间
效率上不如包装类
List l= new ArrayList();
Map m= new HashMap();
public synchronized void h(){
if(l.size()>=2){
Thread hThread = l.get(0);
Thread oThread = l.get(l.size()-1);
if(m.get(hThread) && !m.get(oThread)){
hThread.notify();
oThread.notify();
return;
}
}
Thread currentThread = Thread.getCurrentThread();
l.add(0,currentThread);
m.put(currentThead, true);
currentThread.wait();
}
public synchronized void o(){
if(l.size()>=... 阅读全帖 |
|
z*******3 发帖数: 13709 | 13 汗,实验了一下,有三个小问题,看来多线程白板还是难以bug free,需要多练习
getCurrentThread() -> currentThread()
currentThread.wait() -> wait()
hThread.notify() -> synchronized(hThread){ hThread.notify();} |
|
e***n 发帖数: 286 | 14 我有这样一段程序:
// Define 10 subproblems of the same content
DWORD WINAPI sub1(LPVOID lpPara);
DWORD WINAPI sub2(LPVOID lpPara);
...
DWORD WINAPI sub10(LPVOID lpPara);
// Define function pointer array
DWORD (WINAPI *PtrSub[10])(LPVOID = {sub1, sub2, ..., sub10};
// Start 10 multithreads
HANDLE hThread[10];
for(int ix = 0; ix < 10; ++ix)
hThread[ix] = CreateThread(NULL, 0, PtrSub[ix], NULL, 0, NULL);
for(int ix = 0; ix < 10; ++ix)
CloseHandle(hThread[ix]);
//...
while(SOLVED) Sleep(50);
//...
。。。 |
|
O*******d 发帖数: 20343 | 15 HANDLE hThread = (HANDLE)_beginthreadex(.....);
// run thread
WaitForSingelObject(hThread, someTimeOut);
CloseHandle(hThread ); |
|
F****n 发帖数: 3271 | 16 你知道你的hThread.notify() notify不到 wait()把??? |
|
F****n 发帖数: 3271 | 17 你就解释一下为啥hThread.notify()能影响wait()吧 |
|
F****n 发帖数: 3271 | 18 你知道你的hThread.notify() notify不到 wait()把??? |
|
F****n 发帖数: 3271 | 19 你就解释一下为啥hThread.notify()能影响wait()吧 |
|