繁体   English   中英

在以下情况下如何唤醒客户端线程?

[英]How to wake up the Client Thread in Below Situation?

class Server
{
   while(true)
   new ClientThread(Socket.accept()).start();
}
class ClientThread extends Thread
{
   public void run()
    {
       ppl.chat(Socket s);//a defined protocol object in my program
     }
}
class Protocol
{
   public Protocol(Socket s)
    {
           socket=s;
     }
    public synchronized void chat()
    {
       //here i defined socket input output streams and serverinput is a string given     by server
       if(ServerInput=="wait")
        wait();
       if(ServerInput=="cont")
        notify()
        .....................sending infomation-------
      }
}    

在这里,我无法使用wait()块,之后便无法通过此套接字发送信息。 我测试了它是否成功退出了等待块。 通过从其他客户端线程提供“续”通知它。 有人解决我的问题吗? 感谢前进。

您的伪代码是一个非常令人困惑的恕我直言,但我要做的是。

仅将客户端线程用于读取,而不用于写入。

当您想进行写操作时,请获得写锁(不要锁定整个对象)或在一次写操作中发送消息(做同样的事情)

这样,读取线程仅在有需要读取的内容时才需要唤醒。

这意味着您无需等待或通知我建议您在任何情况下都使用。 如果需要它们,则应使用Java 5.0中添加的高级并发库(但在这种情况下不使用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM