2012-06-21 43 views

回答

0

在Java中,通常會將通知作爲事件傳遞,這意味着發生了某些操作,例如按鈕已被點擊或作爲異常,意味着出現問題,例如嘗試訪問無效位置數組。

另一種方法是使用volatile變量作爲標誌。

更多的語言不可知的方法是使用標誌,也許某種信號量(即使這些通常更多地用於調節跨競爭線程的訪問)。

你也可以使用套接字,我會假設,但這些通常用於進程間通信。

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 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 
+0

輝煌!!!我想知道如何在使用兩個線程進行通信時使用相同的方法?說我從一個線程讀取一些數據,然後通知寫入線程做一些工作。我經歷了一些博客,其中推薦使用信號量來等待和通知呼叫,這也是有道理的,因爲兩個線程之間的數據需要同步。有沒有其他需要同步的事件?只是爲了通知其他線程? –

+1

如果您不想使用同步,但想要使用wait-notify機制,請使用java.util.concurrent包中的ReerrantLock(),並使用lock() - unlock()以及Condition接口,其中u可以使用await-singal機制,還有一件事...如果你發現這真的很棒,爲什麼我的觀點仍然是0這個答案...哈哈哈.... !! –