由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - multi-threading guru们
相关主题
微软面试题WebClient for multi-connections?
狗狗家面筋我在美国混不下去了怎么办? 读CS master啊!
请问一个多线程与volatile关键字的问题。round function in math
请教一个c语言实现多线程的问题关于Dword 和 word
相关话题的讨论汇总
话题: method话题: mutex话题: event话题: pthread话题: wait
进入JobHunting版参与讨论
1 (共1页)
I**A
发帖数: 2345
1
java里
Class C{
method 1{}
method 2{}
}
How can we guarantee that method 1 can only be called after method 2 is
called and finished?
Thanks a lot~
h**********d
发帖数: 4313
2
by putting them inside a synchronized block?
x*****p
发帖数: 1707
3
Yes.
Suppose you define two Runnable objects r1 and r2. They share the same
object of C. r1 calls method1 and r2 calls method2 in their run method.
Then you can use
ExecutoreService es = Executors.newSingleThreadExecutor();
es.execute(r2);
es.execute(r1);
Then it is guaranteed that r2 finishes running method2 first and r1 runs
method1 second.
a****n
发帖数: 1887
4
in C++
create event
thread A------------------------
wait for event
invoke method 1
thread B------------------------
invoke method 2
set event
x*****p
发帖数: 1707
5
synchronized block can not guarantee the order.

【在 h**********d 的大作中提到】
: by putting them inside a synchronized block?
a*****e
发帖数: 21
6
wait and notify可以吗

【在 x*****p 的大作中提到】
: synchronized block can not guarantee the order.
y*******o
发帖数: 6632
7
private AtomicBolean method2finished=new AtomicBoolean( false );
// private volatile method2finished=false; will also work and may be better
method1(){
while(!method2finished)
{ sleep(2000)}
do methods;
return;
}
method2(){
do methods;
method2finished=true;
return;
}

【在 I**A 的大作中提到】
: java里
: Class C{
: method 1{}
: method 2{}
: }
: How can we guarantee that method 1 can only be called after method 2 is
: called and finished?
: Thanks a lot~

l*******o
发帖数: 791
8
多线程的情况下好像不能保证想要的顺序,因为这是有操作系统的调度来决定。一个笨
办法好像是强制让一个thread睡眠一段时间,利用这段时间完成另一端,从而保证了按
照一个既定顺序。
x*****p
发帖数: 1707
9
In Java, you have two waits to control.
1. Use wait-notify pairs to control the order of tasks
2. In concurrent package, use scheduled thread pool.

【在 l*******o 的大作中提到】
: 多线程的情况下好像不能保证想要的顺序,因为这是有操作系统的调度来决定。一个笨
: 办法好像是强制让一个thread睡眠一段时间,利用这段时间完成另一端,从而保证了按
: 照一个既定顺序。

a****n
发帖数: 1887
10
用event或者signal!!!
不知道java里面有没有实现, C++跨平台的实现
void create_event()
{
#ifdef _MSC_VER
m_event = CreateEvent(NULL,FALSE,FALSE,L"SESSION");
#else
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init (&m_event, NULL);
#endif
}
bool wait_event()
{
#ifdef _MSC_VER
DWORD ret = WaitForSingleObject(m_event,10000);
return WAIT_OBJECT_0 == ret;
#else
timespec ts;
ts.tv_sec = time(NULL) + 5;
ts.tv_nsec = 0;
pthread_mutex_lock(&m_mutex);
int ret = pthread_cond_timedwait(&m_event, &m_mutex, &ts);
//int ret = pthread_cond_wait(&m_event, &m_mutex);
pthread_mutex_unlock(&m_mutex);
return ret == 0;
#endif
}
void set_event()
{
#ifdef _MSC_VER
SetEvent(m_event);
#else
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_event);
pthread_mutex_unlock(&m_mutex);
#endif
}
linux下还可以用 signal 做
相关主题
我在美国混不下去了怎么办? 读CS master啊!微软面试题
round function in math狗狗家面筋
关于Dword 和 word请问一个多线程与volatile关键字的问题。
进入JobHunting版参与讨论
I**A
发帖数: 2345
11
不行的话,我就试试这个笨法子。。。
我想了一下,要解决我的这个问题,貌似必须创建独立的thread,而且调控要it们,太
麻烦了

【在 l*******o 的大作中提到】
: 多线程的情况下好像不能保证想要的顺序,因为这是有操作系统的调度来决定。一个笨
: 办法好像是强制让一个thread睡眠一段时间,利用这段时间完成另一端,从而保证了按
: 照一个既定顺序。

T*******i
发帖数: 4992
12
java monitor
wait/notify/notifyAll

【在 I**A 的大作中提到】
: 不行的话,我就试试这个笨法子。。。
: 我想了一下,要解决我的这个问题,貌似必须创建独立的thread,而且调控要it们,太
: 麻烦了

j*****u
发帖数: 1133
13
it depends on the behavior you want
do you want the caller to be blocked in m2 and notified when m1 is done?
or do you want m2 to return to caller immediately?
both are pretty easy to implement with some thread sync approeach.
blocking is bad, usually.

【在 I**A 的大作中提到】
: java里
: Class C{
: method 1{}
: method 2{}
: }
: How can we guarantee that method 1 can only be called after method 2 is
: called and finished?
: Thanks a lot~

I**A
发帖数: 2345
14
就是要求m2在m1finish之前绝对不能被call
否则就deadlock瞭。。。

【在 j*****u 的大作中提到】
: it depends on the behavior you want
: do you want the caller to be blocked in m2 and notified when m1 is done?
: or do you want m2 to return to caller immediately?
: both are pretty easy to implement with some thread sync approeach.
: blocking is bad, usually.

j*****u
发帖数: 1133
15

如果m2是public,caller可以在任何时候call它,你没有办法阻止
你要确定是当m2被call(而m1还没有finish)的时候,你希望m2的behavior是什么?

【在 I**A 的大作中提到】
: 就是要求m2在m1finish之前绝对不能被call
: 否则就deadlock瞭。。。

y*******o
发帖数: 6632
16
i do not think that is possible if both of your method expose to the client.
If that is the case, you should not expose both method to your client.
you need to have to full control to the method

【在 I**A 的大作中提到】
: 就是要求m2在m1finish之前绝对不能被call
: 否则就deadlock瞭。。。

c*****h
发帖数: 166
17
两个thread用一个对象吗?
b****n
发帖数: 84
18
c++的话用boost::thread::join()
h******3
发帖数: 351
19
恕我弩钝, 请问这题和career cup 上的那个题目有啥区别啊?
class F{
public A(); public B(); public C();
}
Can you design a mechanism to make sure that B is executed after A, C is
executed after B?
And the solution is using two semaphore synchronize between A&B, B&C. I
think the question is talking about multithreads share the same objects.
在Java里就是用wait and notifyall.
谢谢

【在 I**A 的大作中提到】
: java里
: Class C{
: method 1{}
: method 2{}
: }
: How can we guarantee that method 1 can only be called after method 2 is
: called and finished?
: Thanks a lot~

l******9
发帖数: 7
20
Mutex
1 (共1页)
进入JobHunting版参与讨论
相关主题
round function in math请问一个多线程与volatile关键字的问题。
关于Dword 和 word请教一个c语言实现多线程的问题
微软面试题WebClient for multi-connections?
狗狗家面筋我在美国混不下去了怎么办? 读CS master啊!
相关话题的讨论汇总
话题: method话题: mutex话题: event话题: pthread话题: wait