[Java] 84. Class로 Thread 만들기

백하림's avatar
Feb 21, 2025
[Java] 84. Class로 Thread 만들기
package ex19; import java.util.ArrayList; import java.util.List; class MyThread extends Thread { List<Integer> list; @Override public void run() { // Thread 상속 받아서 상태 보관 타켓 재정의 for (int i = 0; i < 10; i++) { System.out.println("MyThread: " + i); try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public MyThread(List<Integer> list) { this.list = list; } public void addList(int num) { list.add(num); } public List<Integer> getList() { return list; } } // 클래스로 쓰레드 만들기 (쓰레드별 상태 보관) public class Th04 { public static void main(String[] args) { MyThread t1 = new MyThread(new ArrayList<>()); //t1.run(); t1.start(); } }
notion image
Share article

harimmon