0

Guys I'm facing a similar situation like This junit case on another thread though i don't have a junit case. I tried everything that i know of.. including suggestion on that link page, keeping a countdown and thread sleep but the results don't change. if i run through debug and give it some time it shows me all the results from all the thread but if i run it normally it invariably gives me less results. My code is as belows

`

    AtomicInteger atomicInteger = new AtomicInteger(employeeids.size());
    CountDownLatch latch = new CountDownLatch(employeeids.size());
    Iterable<List<String>> batchList = createBatches(employeeids, batchSize);

     Set<Future<List<GradeSearchDTO>>> set = new HashSet<Future<List<GradeSearchDTO>>>();


    for(List<String> employeeidsList: batchList) {

        Callable<List<GradeSearchDTO>> callable = new ScheduleCallable( employeetype, employeedetails,  employeeidsList,  dept, seeker, atomicInteger,latch );
        Future<List<GradeSearchDTO>> future = pool.submit(callable);
        set.add(future);
      try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


    try {
        latch.await(getTimeOutInMillis(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        throw new EmployeeException("Building of Schedules didn't finish in time of ["+getTimeOutInMillis()+"] millis. ");

    }

    long timeLeft = getTimeOutInMillis();
    boolean check=true;
    while (check){
        logger.debug("Waiting for building asset. countdown value is[" + timeLeft+"]");
        try {
            Thread.sleep(TIME_TO_PAUSE);
            timeLeft = timeLeft - TIME_TO_PAUSE;
            if(timeLeft == 0 || timeLeft < 0){
                throw new EmployeeException("Building of Schedules didn't finish in time of ["+getTimeOutInMillis()+"] millis. ");
            }
            for (Future<List<GradeSearchDTO>> future : set) {
                if(!future.isDone()){
                    check=true;
                    break;
                }
                else{check=false;}
              }

        } catch (InterruptedException e) {
            logger.error("Error waiting for asset to build to bulid");
        }
    }

    for (Future<List<GradeSearchDTO>> future : set) {
        try {
            EmployeeScheduleList.addAll(future.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }



 public static class ScheduleCallable implements Callable 

{ 
    private String employeetype;
    private List<Employee>  employeedetails;
    private List<String> employeeidsList;
    private String dept;
    private EmployeeSeekerHelper seeker;
    private AtomicInteger atomicInteger;
    private CountDownLatch latch;

     public ScheduleCallable(String employeetype,List<Employee> employeedetails, 
     list<String> employeeidsList, String dept,EmployeeSeekerHelper seeker,AtomicInteger
    atomicInteger,CountDownLatch latch )

    {

    this.employeetype = employeetype;
    this.employeedetails = employeedetails;
    this.employeeidsList = employeeidsList;
    this.dept = dept;
    this.seeker = seeker;
    this.atomicInteger=atomicInteger;
    this.latch=latch;
    }

    public List<GradeSearchDTO> call() 
    {
    List<GradeSearchDTO> EmployeeScheduleList =  new ArrayList<GradeSearchDTO>(0) ;

    int counter=1;

    for(String scheduleId : employeeidsList)

    {

     latch.countDown();

    EmployeeScheduleList.addAll(searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch));
    System.out.println("Thread COUNTER "+counter);
    atomicInteger.decrementAndGet();
    counter++;
    // latch.countDown();

    }
return EmployeeScheduleList;

    }
}

`

Community
  • 1
  • 1
  • 4
    TL; DR could you pin-point the problem you're having and creating a small piece of code that reproduces it? – maasg May 15 '14 at 17:40
  • +1 to the comment, and it'd also help if you specified _what_ fails. Are you getting an exception, and if so, what is? Are you getting unexpected results somewhere, and if so, what are they and what do you expect? – yshavit May 15 '14 at 17:55
  • So let's say i have my batchList size=5 with each object containing 10 employeeids. so the batchlist loop should run 5 times and i would except results to be from those 5 threads.. but what i see is i get randomly sometimes results from 1 thread sometimes 2 threads. The way my cord is trying to work is in the call() method for each batchlist object(employeeid) another loop is run to call the method which actually does the business logic. – user3641298 May 15 '14 at 19:21

1 Answers1

0

So the above code is perfectly fine... nothing wrong at all. The problem that i faced with the random results was because the method searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch) which does the business logic under the call() was internally calling a rules engine which was not returning proper results because of the usage of same instance of a class instead of a new instance for each thread.