JSON对象不使用通知和等待方法

发布于 2025-02-13 15:03:12 字数 1245 浏览 0 评论 0原文

我有两个不同的线程(Java):

  • 一个线程以提出发布请求并从REST API服务器中获取答案,以JSON格式获得答案,
  • 以处理此信息并将其保存在文件中。

由于它们是两个不同的线程,而第二个线程仅在第一个线程接收时才开始工作,因此发送我认为是通知()和wait()方法的信息的方法。但是,我得到的JSON对象未发送或收到。我测试了从创建的类发送另一个对象,并且可以正常工作。为什么不能使用通知和等待方法发送JSON对象?

为了简化代码,我将将REST API的响应作为字符串和与Notify和Wait的代码相关联(代码是通过可运行的线程实现的)。

第一个线程获取信息:

JSONObject jobj_gnss=new JSONObject();
String testingProcess="{\"navi\":{\"longitude\":9.015089833333333,\"latitude\":45.514222999999997,\"altitude\":151.9,\"speed\":78.2,\"travelAngle\":244.08,\"horp\":2.52,\"vertp\":5.699999999999999},\"status\":{\"locked\":true,\"quality\":2,\"type\":3,\"gps_time\":\"10h 57m 20s  October 13 2020\",\"satellites_tracked\":12,\"satellites_in_view\":15},\"res\":\"OK\"}";

   JSONParser parser = new JSONParser();      
   try {
        jobj_gnss = (JSONObject) parser.parse(testingProcess);
   } catch (ParseException e) {
        e.printStackTrace();
   }       
   synchronized (jobj_gnss )
   {    
        jobj_gnss.notifyAll();
   }

第二个线程:

   JSONObject jobj_gnss;
   synchronized (jobj_gnss)
   {
        jobj_gnss.wait();
        Thread.sleep(1000);
        System.out.println(jobj_gnss.toString());
   }

I have two different threads (JAVA):

  • one thread in order to make POST requests and get the answer from the REST API server, obtaining the answer in a JSON format
  • another thread with the purpose of processing this information and saving it in a file.

As they are two different threads and the second thread only starts working when the first thread receives, the way to send the information that I thought is notify() and wait() methods. Nevertheless, the JSON Object that I get is not sent nor received. I tested sending another object from a created class and it works. Why the JSON object cannot be sent with notify and wait methods?

To simplify the code, I will put the response from the REST API as a string and the code related to the notify and wait (the code is implemented in thread by runnable).

First thread getting the information:

JSONObject jobj_gnss=new JSONObject();
String testingProcess="{\"navi\":{\"longitude\":9.015089833333333,\"latitude\":45.514222999999997,\"altitude\":151.9,\"speed\":78.2,\"travelAngle\":244.08,\"horp\":2.52,\"vertp\":5.699999999999999},\"status\":{\"locked\":true,\"quality\":2,\"type\":3,\"gps_time\":\"10h 57m 20s  October 13 2020\",\"satellites_tracked\":12,\"satellites_in_view\":15},\"res\":\"OK\"}";

   JSONParser parser = new JSONParser();      
   try {
        jobj_gnss = (JSONObject) parser.parse(testingProcess);
   } catch (ParseException e) {
        e.printStackTrace();
   }       
   synchronized (jobj_gnss )
   {    
        jobj_gnss.notifyAll();
   }

Second thread:

   JSONObject jobj_gnss;
   synchronized (jobj_gnss)
   {
        jobj_gnss.wait();
        Thread.sleep(1000);
        System.out.println(jobj_gnss.toString());
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文