2011-10-09 64 views
1

是否有使其內部的類A調用notify(),以便從線程喚醒wait()中的線程在後臺運行創建一個類,以便從Java中的另一個類中調用它的notify()

謝謝。

編輯:

請忽略不必要的代碼。我想告訴你我是如何真正實現它的。

Messenger是一個序列化對象。

線程「ServerHandler」將調用 bufferImpObject.put(<Messenger Obj>, <ServerHandler instance>, <socket instance>)

public class BufferImp { 
    private Messenger[] requests; 
    public static int req_size; 
    public static int req_count; 


    Socket s; 
    Messenger msg; 
    InputStream is; 
    ObjectInputStream ois; 
    OutputStream os; 
    ObjectOutputStream oos; 



    public BufferImp(int size) { 
     this.req_size = size; 
     this.req_count = 0; 
     requests = new Messenger[size]; 

    } 



    class CheckWaitThread extends Thread{ 
     Thread t; 
     String s; 
     Socket incoming_socket; 



     CheckWaitThread(String s, Thread t, Socket incoming_socket){ 
      this.t = t; 
      this.s = s; 
      this.incoming_socket = incoming_socket; 


     } 
     @Override 
     public void run(){ 
      try { 
       // I want to keep on checking till slots are FREE 
       // IF FREE, call notify() of BufferImp to get back WAITING (inactive) ServerHandler thread 
       if(t.getState().equals(State.WAITING)){ 
           os = incoming_socket.getOutputStream(); 
           AppendableObjectOutputStream oosa = new AppendableObjectOutputStream(os); 
           oosa.writeObject(new Messenger("bufferFull", null, "true")); 


       } 
       t.join(); 
      } catch (IOException ex) { 
       Logger.getLogger(BufferImp.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(BufferImp.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

    // insert incoming Message objects into "requests" queue 
    public synchronized boolean put(Messenger o, Thread t, Socket s) throws InterruptedException { 


     while(req_size == req_count) { 
      CheckWaitThread cw = new CheckWaitThread("<----------CheckWaitThread----------->", t, s); 
      cw.start(); 
      wait(); 


     } 

     // these parts need to be executed when notify() is called. 
     requests[cur_req_in] = o; 
     cur_req_in = (cur_req_in + 1) % req_size; 
     req_count++; 
     notifyAll(); 
     Messenger msg = (Messenger) o; 


     return true; 
    } 




} 
+3

請詳細說明您嘗試實現的內容,並帶有一些示例代碼片段。 – Saket

+0

請幫助我們通過提供更多詳細信息來了解問題,謝謝 – Genjuro

+0

我已經添加了代碼 – coder9

回答

5

簡短的回答你的問題是

你的問題很清楚,但我覺得這是你問:

  • A類延伸ThreadRunnable並且線程正在運行一些A作爲目標的對象。叫它AThread。

  • 該線程在A對象上擁有一個鎖,並且其他線程(OThread)正在等待它。

  • 你想讓其他線程(BgThread)能夠喚醒等待的線程。

重要的一點是,通知可以只能從線程擁有鎖調用。所以如果後臺線程不擁有鎖,那麼它就不能調用它的通知。

編輯:

看着你添加的代碼,看來你是想建立一個等待機制BufferImp:如果requests滿額,則CheckWaitThread會寫一個bufferFull消息傳入插座,所以大概客戶會停止發送更多的請求。不知何故(因爲其他線程彈出和處理請求)在未來某個時間機器將再次開始旋轉。

沒有必要這麼辛苦

  • 爲什麼你需要發送響應,並等待requests集合在一個單獨的線程釋放?爲什麼你不能從這個線程本身發送它?

  • 而不是自己管理requests array,請看BlockingQueue這正是爲此目的而創建的。

+0

好的答案,我想他有點澄清了他的想法。 – SHiRKiT

+0

+1感謝您的支持。這是一個必須使用** wait(),notify()或notifyAll()**,因爲這是一個學術項目。 – coder9

+0

使用單獨線程等待請求的原因是,沒有整個GUI被凍結,我沒有在EDT上調用wait()。我通過在wait()之前調用Thread.currentThread.getName()來確認它。任何線索? – coder9

0

所有代碼運行在一個線程中,背景和前景之間的區別是名義上的。

要調用notify(),只需鎖定通知的對象。

相關問題