【发布时间】:2012-06-21 06:39:29
【问题描述】:
谁能告诉我在 Java 中是如何处理信号的?我有一种情况,我需要从 application main () 向线程发送事件/信号。
【问题讨论】:
标签: java android signal-handling
谁能告诉我在 Java 中是如何处理信号的?我有一种情况,我需要从 application main () 向线程发送事件/信号。
【问题讨论】:
标签: java android signal-handling
在 android 上,您可以使用 Handler 类跨线程发送消息。 Here is the Handler class description 和 code sample
【讨论】:
在 Java 中,通知通常作为事件传递,这意味着某些操作已经发生,例如按钮已被单击,或者作为异常传递,这意味着出现问题,例如尝试访问一个无效位置数组。
另一种方法是使用volatile 变量作为标志。
一种更与语言无关的方法是使用标志,可能是某种信号量(尽管这些通常更多用于管理竞争线程之间的访问)。
我想你也可以使用套接字,但这些通常用于进程间通信。
【讨论】:
我会试着解释一下……
活动: When some action take place.
事件处理: To do certain actions on the basis of the Event.
Eg:
Button - Button is an Event Source
Action - Button Pressed
Pressing of button generates an Event, which is needed to be Handled..
例外:When Something unexpectedly take place.
Throwable --> Exception --> IOException & InterruptedException --> Checked and UnCheckedExceptions
Eg:
Making a Socket Connection to the Server
If the connection to the Server fails it leads to
UnknowHostException which in need to be Handled..
this is called Exception Handling..
We handle the exception using try/catch blocks
【讨论】: