-
Notifications
You must be signed in to change notification settings - Fork 388
[4단계 - Tomcat 구현하기] 카피(김상혁) 미션 제출합니다. #723
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,29 +3,33 @@ | |
| import org.apache.coyote.http11.Http11Processor; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.net.ServerSocket; | ||
| import java.net.Socket; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| public class Connector implements Runnable { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(Connector.class); | ||
|
|
||
| private static final int DEFAULT_PORT = 8080; | ||
| private static final int DEFAULT_ACCEPT_COUNT = 100; | ||
| private static final int DEFAULT_MAX_THREAD = 10; | ||
|
|
||
| private final ServerSocket serverSocket; | ||
| private boolean stopped; | ||
| private final ExecutorService executorService; | ||
|
|
||
| public Connector() { | ||
| this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT); | ||
| this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT, DEFAULT_MAX_THREAD); | ||
| } | ||
|
|
||
| public Connector(final int port, final int acceptCount) { | ||
| public Connector(final int port, final int acceptCount, final int maxThreads) { | ||
| this.serverSocket = createServerSocket(port, acceptCount); | ||
| this.stopped = false; | ||
| executorService = Executors.newFixedThreadPool(maxThreads); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Executors.newFixedThreadPool 은 내부적으로 LinkedBlockingQueue를 대기큐로 사용하여 대기 큐의 크기 제한이 없다고 합니다.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static 메서드를 통해 바로 생성하는 것 대신 new ThreadPoolExecutor()를 직접 생성해서 파라미터로 대기큐의 크기를 설정해서 넘길 수 있다고 합니다! |
||
| } | ||
|
|
||
| private ServerSocket createServerSocket(final int port, final int acceptCount) { | ||
|
|
@@ -67,7 +71,7 @@ private void process(final Socket connection) { | |
| return; | ||
| } | ||
| var processor = new Http11Processor(connection); | ||
| new Thread(processor).start(); | ||
| executorService.submit(processor); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. submit() 과 execute() 는 어떤 차이가 있을까요?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가장 큰 차이점은 submit()은 실행결과를 나타내는 Future를 반환하는 반면, 그래서 결과 값이 필요하거나 예외처리해야하는 경우에 submit()을 사용하고, 그게 아니라면 execute()를 사용해도 무방할 것 같습니다! |
||
| } | ||
|
|
||
| public void stop() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
acceptCount와 maxThreads는 각각 어떤 설정인가요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
acceptCount는 큐에 대기할 수 있는 스레드의 개수를 의미합니다.
maxThreads는 스레드 풀에 생성할 스레드의 개수와 동시에 실행할 수 있는 스레드의 최대 개수를 의미합니다.