I am trying to create several output text data files based on the data present in a servlet request. The limitations for my servlet are as follows:
- My servlet is waiting for enough requests to reach the threshold (for example, 20 names in a file) before creating the file
- Otherwise, it will expire in a minute and produce a file
The code I wrote is such that:
doGet not syncing
Inside doGet I create a new thread pool (the reason is that the calling application for my servlet does not send the next request until my servlet returns a response, so I check the request and return an instant confirmation to receive new requests)
Transfer all request data to the stream created in the new thread pool
- Call a synchronized function to count streams and print files.
I am using wait(60000) . The problem is that the code creates files with the correct threshold (of names) within a minute, but after a timeout of a minute the files created (very few) exceeded the throughput, for example, the names are more than I determined in capacity.
I think this has something to do with those that, when awakened, cause a problem?
My code
if(!hashmap_dob.containsKey(key)){ request_count=0; hashmap_count.put(key, Integer.toString(request_count)); sb1 = new StringBuilder(); sb2 = new StringBuilder(); sb3 = new StringBuilder(); hashmap_dob.put(key, sb1); hashmap_firstname.put(key, sb2); hashmap_surname.put(key, sb3); } if(hashmap_dob.containsKey(key)){ request_count = Integer.parseInt(hm_count.get(key)); request_count++; hashmap_count.put(key, Integer.toString(request_count)); hashmap_filehasbeenprinted.put(key, Boolean.toString(fileHasBeenPrinted)); } hashmap_dob.get(key).append(dateofbirth + "-"); hashmap_firstname.get(key).append(firstName + "-"); hashmap_surname.get(key).append(surname + "-"); if (hashmap_count.get(key).equals(capacity)){ request_count = 0; dob = hashmap_dob.get(key).toString(); firstname = hashmap_firstname.get(key).toString(); surname = hashmap_surname.get(key).toString(); produceFile(required String parameters for file printing); fileHasBeenPrinted = true; sb1 = new StringBuilder(); sb2 = new StringBuilder(); sb3 = new StringBuilder(); hashmap_dob.put(key, sb1); hashmap_firstname.put(key, sb2); hashmap_surname.put(key, sb3); hashmap_count.put(key, Integer.toString(request_count)); hashmap_filehasbeenprinted.put(key, Boolean.toString(fileHasBeenPrinted)); } try{ wait(Long.parseLong(listenerWaitingTime)); }catch (InterruptedException ie){ System.out.println("Thread interrupted from wait"); } if(hashmap_filehasbeenprinted.get(key).equals("false")){ dob = hashmap_dob.get(key).toString(); firstname = hashmap_firstname.get(key).toString(); surname = hm_surname.get(key).toString(); produceFile(required String parameters for file printing ); sb1 = new StringBuilder(); sb2 = new StringBuilder(); sb3 = new StringBuilder(); hashmap_dob.put(key, sb1); hashmap_firstname.put(key, sb2); hashmap_surname.put(key, sb3); fileHasBeenPrinted= true; request_count =0; hashmap_filehasbeenprinted.put(key, Boolean.toString(fileHasBeenPrinted)); hashmap_count.put(key, Integer.toString(request_count)); }
If you got here, I thank you for reading my question and to consider it in advance if you have any solutions to resolve it!
source share