创建一个类以从Java中的另一个类调用其notify()

cod*_*er9 1 java multithreading notify wait

有没有反正,使A级调用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;
    }




}
Run Code Online (Sandbox Code Playgroud)

Mis*_*ble 5

您的问题的简短回答是否定的.

你的问题很不清楚,但我认为这就是你要问的问题:

  • A类扩展Thread,Runnable并且一个线程正在运行,某个A对象作为目标.称之为AThread.

  • 该线程拥有A对象的锁,而其他线程(OThread)正在等待它.

  • 你想要一些其他线程(BgThread)能够唤醒等待的线程.

重要的一点是,只能从拥有锁线程中调用notify .因此,如果后台线程不拥有锁,那么它就不能在其上调用notify.

编辑:

查看您添加的代码,似乎您正在尝试构建一个等待机制BufferImp:如果requests填充容量,则会CheckWaitThread将bufferFull消息写入传入套接字,因此可能客户端将停止发送更多请求.并且在某种程度上(因为其他线程弹出和处理请求)将来某个时候机器将再次开始旋转.

没有必要这么努力工作

  • 为什么需要发送响应并等待requests集合在单独的线程中释放?为什么你不能从这个线程本身发送它?

  • 而不是管理requests array自己,看看BlockingQueue,它正是为了这个目的而创造的.