0

有谁能告诉我如何在 Java 中处理信号?我有一种情况,我需要从应用程序 main() 向线程发出事件/信号。

4

3 回答 3

0

在 Java 中,通知通常作为事件传递,这意味着发生了某些操作,例如单击了按钮,或者作为异常传递,意味着出现了问题,例如尝试访问数组中的无效位置。

另一种方法是使用volatile变量作为标志。

一种更与语言无关的方法是使用标志,可能是某种信号量(尽管这些通常更多地用于规范跨竞争线程的访问)。

我想你也可以使用套接字,但这些通常用于进程间通信。

于 2012-06-21T06:45:22.680 回答
0

在 android 上,您可以使用 Handler 类跨线程发送消息。 这是 Handler 类描述代码示例

于 2012-06-21T06:58:56.477 回答
0

我会尝试解释它...

活动: 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 UnChecked Exceptions

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
于 2012-06-21T07:11:34.183 回答