Setter and Multithreading java issue

Viewed 42

I have a class(Builder.java) from which I'm invoking a thread by implementing Runnable.(ThreadService.java).

Builder class:

    Class Builder{

    @Autowired
    ThreadService threadservice;

    threadService.setReqJson(reqJson);

    Thread thread = new Thread(threadService);


      try{
           synchronized(thread){

               thread.start();
               thread.wait();

              responseToService = threadService.getResponseToService();

                       }
         }catch(){

       }
    }

Thread class:

class ThreadService implements Runnable {

  
                @Autowired
                private DLI dLI;
         
                @Autowired
                private JsonBuilder jsonBuilder;

                JSONObject reqJson = new JSONObject();
                JSONObject responseToService = new JSONObject();

    public ThreadService() {
            super();
               }

     public void run(){

                //Some code

           }

         public JSONObject getResponseToService() {
                                return this.responseToService;
                      }
                public JSONObject getReqJson() {
                                return reqJson;
                      }
                public void  setReqJson(JSONObject reqJson) {
                                this.reqJson = reqJson;
                      }

When two transaction(T1 & T2) occurs at the same time with milli seconds difference, the setter in Builder class is taking only the T2 transaction(second transaction) details. So only the T2 transaction thread runs.

I want both the transactions to be executed. Please help on it.

I'm using only one thread which is waiting for 14 seconds for a response from a vendor.

0 Answers
Related