是否有使其內部的類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;
}
}
請詳細說明您嘗試實現的內容,並帶有一些示例代碼片段。 – Saket
請幫助我們通過提供更多詳細信息來了解問題,謝謝 – Genjuro
我已經添加了代碼 – coder9