topics

全部话题 - 话题: hthread
(共0页)
t*****a
发帖数: 106
1
来自主题: JobHunting版 - 请教L家生成H2O水分子的题。
加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
来自主题: JobHunting版 - LinkedIn 面经
用了两个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
来自主题: JobHunting版 - LinkedIn 面经
用了两个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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
因为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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
因为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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
原题:
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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
或者找个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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
汗,实验了一下,有三个小问题,看来多线程白板还是难以bug free,需要多练习
getCurrentThread() -> currentThread()
currentThread.wait() -> wait()
hThread.notify() -> synchronized(hThread){ hThread.notify();}
z*******3
发帖数: 13709
11
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
原题:
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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
或者找个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
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
汗,实验了一下,有三个小问题,看来多线程白板还是难以bug free,需要多练习
getCurrentThread() -> currentThread()
currentThread.wait() -> wait()
hThread.notify() -> synchronized(hThread){ hThread.notify();}
e***n
发帖数: 286
14
来自主题: Programming版 - C 多线程的一个问题
我有这样一段程序:
// 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
来自主题: Programming版 - 请教一个c语言实现多线程的问题
HANDLE hThread = (HANDLE)_beginthreadex(.....);
// run thread
WaitForSingelObject(hThread, someTimeOut);
CloseHandle(hThread );
F****n
发帖数: 3271
16
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
你知道你的hThread.notify() notify不到 wait()把???
F****n
发帖数: 3271
17
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
你就解释一下为啥hThread.notify()能影响wait()吧
F****n
发帖数: 3271
18
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
你知道你的hThread.notify() notify不到 wait()把???
F****n
发帖数: 3271
19
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
你就解释一下为啥hThread.notify()能影响wait()吧
(共0页)