【问题标题】:Make a class to call its notify() from another Class in Java使一个类从 Java 中的另一个类调用它的 notify()
【发布时间】:2011-10-09 12:42:31
【问题描述】:

是否有反正可以在其中调用A类notify(),在wait()上为该对象的锁唤醒线程,从后台运行的线程 ?

谢谢。

编辑:

请忽略不必要的代码。我想向你展示我是如何真正实现它的。

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;
    }




}

【问题讨论】:

  • 请详细说明您要实现的目标,并附上一些示例 sn-ps。
  • 请提供更多细节帮助我们理解问题,谢谢
  • 代码太多了 :) 您必须尽可能将其缩短为尽可能小的样本——这使其他人更容易查看它,但通常也可以帮助您识别核心问题/概念.

标签: java multithreading wait notify


【解决方案1】:

您的问题的简短回答是“否”。

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

  • A 类扩展了ThreadRunnable,并且一个线程正在以一些Aobject 作为目标运行。称之为 AThread。

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

  • 您希望其他线程 (BgThread) 能够唤醒等待的线程。

重要的是只能从拥有锁线程调用notify。因此,如果后台线程不拥有锁,则它无法对其调用通知。

编辑:

查看您添加的代码,您似乎正在尝试在BufferImp 中构建等待机制:如果requests 已满,那么CheckWaitThread 将向传入的套接字写入bufferFull 消息,所以大概客户端将停止发送更多请求。并且不知何故(因为其他线程弹出和处理请求)在未来某个时间机器将再次开始旋转。

没必要这么辛苦

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

  • 与其自己管理requests array,不如查看BlockingQueue,它正是创建了这个目的。

【讨论】:

  • +1 感谢您的支持。必须使用 wait()、notify() 或 notifyAll(),因为这是针对学术项目的。
  • 使用那个单独的线程来等待请求的原因是,如果没有整个 GUI 被冻结,我不会在 EDT 上调用 wait()。我通过在 wait() 之前调用 Thread.currentThread.getName() 来确认它。有什么线索吗?
  • 不阻塞 GUI 是好的,但在这种情况下,put 本质上是阻塞的,而 requests 数组已满。但现在我很困惑。这是否意味着您的 GUI 线程正在从套接字读取请求?这似乎不对。
【解决方案2】:

所有代码都在一个线程中运行,后台和前台之间的区别是名义上的。

要调用 notify(),您只需锁定通知的对象。

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多