Java C# 同步 互斥

tyjhField 2011-06-13 11:45:32
1. 在C#中有 ManualResetEvent 可用在线程间同步,那在JAVA中有类似的类吗?

2. 在C#中有 MethodInvoker, 它是可以绑定一个事件处理函数, 还是没有限制?

MethodInvoker mi += handle1();
mi += handle2();


if(mi != null)mi();

*********************************
是handle1() or handle2() or together 响应?
*********************************

3. JAVA中没有MethodInvoker, 我自造了一个(这样代吗的修改可以少些)
interface MethodInvoker{
public void onMethodInvoker();
}

And also write function "setOnMethodInvokerListener(MethodInvoker onMethodInvoker)" in the event original source class.
But the code in C# used as this:
CommandLogon(userName, pwd, delegete{faile=0;}, delegete{faile=1;}, delegete{faile=2;});
The construction method : CommandLogon(string , string , MethodInvoker , MethodInvoker , MethodInvoker );

***************************************************************************************************
What can I do as to make a common MethodInvoker , as you see, my current MethodInvoker no parameters?
***************************************************************************************************

4. As to support the situation that multiple Handlers can be added to one MethodInvoker , I create a MethodInvokerGroup .
Just use a Set<MethodInvoker> onMethodInvokers to achieve that by
for(MethodInvoker mi : onMethodInvokers){mi.onMethodInvoker();}
At the same time , I have use List<> for another Event-Handler .
*********************************
Which is better ? List or Set ? (ArrayList/HashSet)
*********************************
...全文
95 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyjhField 2011-06-17
  • 打赏
  • 举报
回复
上一个提问中一个打字错误,

难道说 接收线程 发送线程 是连个线程,Condition不支持在两个线程间操作?

---》》

难道说 接收线程 发送线程 是两个线程,Condition不支持在两个线程间操作?

先谢谢大家的帮助
tyjhField 2011-06-17
  • 打赏
  • 举报
回复
1.
我在接收Socket message 线程中,
Runnable recieveThread = new Runnable(){
if(!con.isConnected()){
//使用了从一个lock得到的Condition conCond的await方法;
conCond.await();
}
.....
}
在与服务器链接成功监听事件中,使用conCond.single();试图唤醒
在发送消息线程中判断链接状态,如果断,则试图连接
Runnable sendThread = new Runnable(){
if(!conConnected()){
connect2Server();
}

}

public void connect2Server(){
...
//链接成功
conCond.single();
}

接收线程 发送线程 connect2Server 全在一个叫Session的类中
为何运行后会有 Illegel... ?
据说这个是由于 非锁拥有者试图释放锁
难道说 接收线程 发送线程 是连个线程,Condition不支持在两个线程间操作?

2
我在做一个Android程序,昨天还出现一个很怪的问题
我有一个 负责登陆的 CommandLogon类,在其他使用处,尽然发生了 ClassNotFoundException 让我很郁闷?明明在那里,import都没问题?
shine333 2011-06-15
  • 打赏
  • 举报
回复
List/Set,倾向于List。不过,即使是Set,也绝不是HashSet,而是LinkedHashSet,因为处理事件的listener是有顺序的。因为event可以被“消费掉”,也就是说前面(更高层,更接近于业务逻辑)的Listener可以把消息传递的过程中断,消息不会再被传递到后面(更底层,更接近物理系统)的Listener。

比如输入框内只能输入数字,你添加的Listener在判断刚才的输入字符不是数字后,可以把它“消费掉”,阻止这一消息继续向更底层的事件处理传递,避免这个字符被显示在输入框中。
shine333 2011-06-15
  • 打赏
  • 举报
回复
一般都是定义一个事件处理接口+事件对象,然后再在发出事件的类中添加一个List<XXXListener>的,
然后就是add/removeXXXListener,事件发生时,循环调用相应事件处理。
shine333 2011-06-15
  • 打赏
  • 举报
回复
see Object.wait() 挂起当前线程 notify()/notifyAll() 通知
tyjhField 2011-06-15
  • 打赏
  • 举报
回复
4楼有点愤青,我显然查了,没查出来,所来来向高手们请教请教
tyjhField 2011-06-15
  • 打赏
  • 举报
回复
1楼 3楼的答非所问
tyjhField 2011-06-15
  • 打赏
  • 举报
回复
针对第一个问题:
c# ManualResetEvent,在java中我使用Condition来实现
就是说有lock得到的Condition

我个人觉得这和ManualResetEvent有相似之处,都是可以wait,然后有一个事件(条件)来激活等待的线程。

关于后面的三个问题,咋没人回答呢?
tyjhField 2011-06-14
  • 打赏
  • 举报
回复
Thread A
if(!con.isConnectioned()){ //wait for a event }

Thread B
con.Connection();
//Notify A to awake up

How ?
kurama_mail 2011-06-14
  • 打赏
  • 举报
回复
自己查阅Lock的互斥锁
uastation 2011-06-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tkd03072010 的回复:]

synchronized 在你的方法上加这个关键字 就能锁上 实现同步

Java code

public synchronized void test() {

}
[/Quote]

在方法上面加同步,效果不大的,而且也影响性能,最好就能用对像去同步,如下面这样:

private Object object = null;
public void method1(){
if(object==null){
synchronized(object){
if(object==null){
// 实现你的代码~
}
}
}
}
TKD03072010 2011-06-13
  • 打赏
  • 举报
回复
synchronized 在你的方法上加这个关键字 就能锁上 实现同步


public synchronized void test() {

}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧