Java wątki, ExecutorService

0

Chciałbym aby jednocześnie pracowały 2 wątki ze specialService wykonujące te samą funkcję jednak nie rozumiem czemu nie działa


main(){
ExecutorService specialService = Executors.newFixedThreadPool(2);

        specialService.submit(() -> {
            while (!isEndOfDayWork || anyTaskLeft) {
                Employee employee = removeFromQueue();
                if (employee != null) {
                    handleSpecialStatusPriorityClient(employee);
                    }}});
}

private void handleSpecialStatusPriorityClient(Employee employee){
        Client specialStatusClient = specialStatusClients.poll();
        handleTheQuestion(specialStatusClient, employee);

        employeesLock.lock();
        try{
            employees.add(employee);
        }finally {
            employeesLock.unlock();
        }
    }


    private void handleTheQuestion(Client handledClient, Employee employee){
        if(handledClient!=null) {
            final long time = handledClient.getQuestion().getAmountOfTimeToHandleQuestion();
            try {
                Thread.sleep(time * 1000);
                System.out.println(employee.getID() + " just handled "+ handledClient.getQuestion() + " task!");
            } catch (InterruptedException e) {
                System.out.println("Problem during thread sleeping");
            }
        }
    }

Problemem jest, że działa tylko 1 a w executorze utworzyłem 2.

Gdy rozbije na dwa tak jak niżej i dam takie same submity to jednak działa, więc pytanie jest moje czemu ta wersja u góry nie działa

ExecutorService specialService = Executors.newFixedThreadPool(2);


ExecutorService specialService1 = Executors.newFixedThreadPool(1);
ExecutorService specialService2 = Executors.newFixedThreadPool(1);

1

A robiłeś 2 submity do tego executora?
Executor robi zadania. Wrzucane submitem. Każde wrzucone zadanie dostaje wątek z puli.

0

aaa czyli musze do każdego dawać submit, myślałem że jak mam service i daje submit a mam więcej wątków to submit zadziała jak jakieś submit all, a to dziekuje, teraz działa i sam executorService wydaje się być bardziej użytecny :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1