Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadPool은 thread에게 어떻게 작업을 배분하는가? #9

Open
sawooook opened this issue Mar 12, 2023 · 1 comment
Open

Comments

@sawooook
Copy link
Member

sawooook commented Mar 12, 2023

Q. ThreadPool은 thread에게 어떻게 작업을 배분하는가?

@sawooook sawooook changed the title ThreadPool은 thread를 어떻게 배분하는가? ThreadPool은 thread에게 어떻게 작업을 배분하는가? Mar 12, 2023
@sawooook
Copy link
Member Author

쓰레드풀이 어떻게 쓰레드에게 작업을 할당 해주는지 찾아보았습니다..!
결론은 쓰레드풀이 알아서 잘 업무를 할당해주고 있었는데요..!

현재 일하고 있는 쓰레드와 할당된 쓰레드의 갯수를 비교하여 풀의 사이즈를 넘지 않으면 쓰레드를 가져와 업무를 배분하고, 아니면 workQueue 에 집어넣고 다른 쓰레드가 일이 끝날때까지 기다립니다. 그 후 쓰레드가 일이 끝나면 업무를 배분합니다.

    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();

        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

참고해서 보면 좋을 다른 메서드들
boolean addWorker(Runnable firstTask, boolean core), void runWorker(Worker w)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant