•
스레드 예제 코드 - ArrayList 활용
package com.collection.list;
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
// 첫번째 스레드 정의 [0부터 49까지 넣는 역할]
Thread thread0 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
list.add(i);
System.out.println("Thread-0 " + i + " 추가됨");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
// 두번째 스레드 정의 [50부터 99까지 넣는 역할]
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 50; i < 100; i++) {
list.add(i);
System.out.println("Thread-0 " + i + " 추가됨");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread0.start();
thread1.start();
try {
thread0.join();
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("총 길이 : " + list.size());
System.out.println(list);
}
}
Java
복사