JsonPath returned wrong value in multiple threads

  Kiến thức lập trình

I did sample code below to check JsonPath version 2.9.0 thread safe or not, I realized it did not.
Is that the issue of JsonPath or there is any way to cope it? Or any alternative solution? Please help

@Test
  public void testJsonPath() throws IOException {

    String jsonString = "{"test":1,"person":{"name":"John","age":50}}";
    String jsonString2 ="{"test":1,"person":{"name":"Babara","age":48}}";
    Thread thread1 = new Thread(createRunnable(jsonString));
    Thread thread2 = new Thread(createRunnable(jsonString2));
    thread1.start();
    thread2.start();
    try {
      thread1.join();
      thread2.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private Runnable createRunnable(String json) {
    return () -> {
      int count = 10;
      try {
        while (count > 0) {
          count--;
          Thread.sleep(1000);
          String value = JsonPath.read(json, "concat($.person.name, $.person.age)");
          System.out.printf("Thread-[%d], Value: [%s]%n", Thread.currentThread().getId(), value);

        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    };
  }

The result showed that JsonPath return wrong values, such as: Babara50, John48

Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [**Babara50**]
Thread-[31], Value: [**John48**]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [**Babara50**]
Thread-[30], Value: [John50]
Thread-[31], Value: [**John48**]
Thread-[31], Value: [**John48**]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]
Thread-[31], Value: [Babara48]
Thread-[30], Value: [John50]

LEAVE A COMMENT