[JAVA]67.스레드

손영민's avatar
Mar 09, 2025
[JAVA]67.스레드
Contents
5.6.
 
package ex19; public class Th01 { public static void main(String[] args) { //CPU -> 메인 쓰레드 Thread t1 = new Thread(() -> { //thread의 생명주기는 타겟이라 부른다 //분신1 for (int i = 0; i < 10; i++) { System.out.println("분신1 : " + i); try { Thread.sleep(500); //0.5초 } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t1.start(); //운영체제(os)한테 남는 쓰레드를 run을 요청 Thread t2 = new Thread(() -> { //분신2 for (int i = 0; i < 10; i++) { System.out.println("분신2 : " + i); try { Thread.sleep(500); //0.5초 } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t2.start(); Thread t3 = new Thread(() -> { for (int i = 0; i < 10; i++) { System.out.println("분신3 : " + i); try { Thread.sleep(500); //0.5초 } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t3.start(); System.out.println("메인쓰레드 종료"); } }
 
 
 
 
 
 
 
 

5.

package ex19; //콜백 class Store implements Runnable { int qty; @Override public void run() { // 통신 -> 다운로드 try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } qty = 5; } } /* 스레드에서 받은 데이터를 리턴 받아서 응용하고 싶을때!! 1. 타이밍 맞추기 (임시방편 - 그래도 쓰는 사람 많음) 2. 리스너 (부하가 너무 큼) 3. 콜백 (제일 좋음) 사용이유: i/o가 일어날때, 동시에 일하고 싶을때 */ public class Th06 { public static void main(String[] args) { int totalQty = 10; Store store = new Store(); Thread t1 = new Thread(store); t1.start(); try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("재고수량 :" + (store.qty + totalQty)); } }
 
package ex19; //콜백 class Store implements Runnable { Integer qty; @Override public void run() { // 통신 -> 다운로드 try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } qty = 5; } } /* 스레드에서 받은 데이터를 리턴 받아서 응용하고 싶을때!! 1. 타이밍 맞추기 (임시방편 - 그래도 쓰는 사람 많음) 2. 리스너 (부하가 너무 큼) 3. 콜백 (제일 좋음) 사용이유: i/o가 일어날때, 동시에 일하고 싶을때 */ public class Th06 { public static void main(String[] args) { int totalQty = 10; Store store = new Store(); Thread t1 = new Thread(store); t1.start(); while (true) { if (store.qty != null) break; try { Thread.sleep(10); System.out.println("지켜본다"); } catch (InterruptedException e) { throw new RuntimeException(e); } } System.out.println("재고수량 :" + (store.qty + totalQty)); } }
 
 

6.

package ex19; //콜백 //(1)번 콜백 메서드 만들기 interface Callback { void 입고(int qty); //리턴 받고 싶은 파라미터를 만들어주면 된다. } class Store implements Runnable { Callback callback; //(2) 리턴이 필요한곳으로 public Store(Callback callback) { this.callback = callback; } Integer qty; @Override public void run() { // 통신 -> 다운로드 try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } qty = 5; callback.입고(qty); // (3)종료시 콜백 메서드 호출 } } /* 스레드에서 받은 데이터를 리턴 받아서 응용하고 싶을때!! 1. 타이밍 맞추기 (임시방편 - 그래도 쓰는 사람 많음) 2. 리스너 (부하가 너무 큼) 3. 콜백 (제일 좋음) 사용이유: i/o가 일어날때, 동시에 일하고 싶을때 */ public class Th06 { public static void main(String[] args) { int totalQty = 10; Store store = new Store(qty -> { System.out.println("재고수량 :" + (qty + totalQty)); }); Thread t1 = new Thread(store); t1.start(); } }
Share article

sson17