전체 글 38

executorservice 사용 예시

for (int i = 0; i { System.out.println("작업 " + taskId + " 시작"); try { Thread.sleep(2000); // 2초 대기 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("작업 " + taskId + " 완료"); });}비동기 처리ExecutorService 사용:executor.submit(() -> {...})를 사용하여 작업을 제출하고 있습니다. ExecutorService는 비동기적으로 작업을 실행할 수 있는 기능을 제공합니다. 제출된 작업은 스..

카테고리 없음 2024.07.24

메인 스레드 와 서브 스레드

public class SyncToAsync {public static void main(String[] args) {Runnable task = () -> {// 동기적으로 수행할 작업System.out.println("작업 시작");try {Thread.sleep(2000); // 2초 대기} catch (InterruptedException e) {e.printStackTrace();}System.out.println("작업 완료");};// 새로운 스레드에서 작업 실행Thread thread = new Thread(task);thread.start();// 메인 스레드는 다른 작업을 계속 수행할 수 있음System.out.println("메인 스레드에서 다른 작업 수행");}} 메인 스레드와 서브..

카테고리 없음 2024.07.24

스레드 풀 관련 의문점

Thread thread = new Thread(task);는 task를 스레드 풀에 넣는 것이 아닙니다. 이 코드의 의미를 좀 더 자세히 설명드리겠습니다.설명새 스레드 생성:new Thread(task)는 task를 실행하기 위한 새로운 스레드를 생성합니다. 여기서 task는 Runnable 인터페이스를 구현한 객체여야 합니다. 이 객체의 run() 메서드가 스레드에서 실행됩니다.javaRunnable task = new MyTask(); // MyTask는 Runnable을 구현한 클래스Thread thread = new Thread(task); // 새로운 스레드 생성스레드 풀 아님:위의 코드에서는 스레드 풀에 작업을 추가하지 않고, 단순히 새로운 스레드를 생성합니다. 생성된 스레드는 start()..

카테고리 없음 2024.07.24