for(int i=0; i < 300; i++) System.out.printf("%s", new String("-")); for(int i=0; i < 300; i++) System.out.printf("%s", new String("|"));
package thread;
public class threadTest {
public static void main(String[] args) {
/* 싱글스레드 (Single Thread) */
for (int i = 0; i < 300; i++) {
System.out.printf("-");
}
for (int i = 0; i < 300; i++) {
System.out.printf("|");
}
// ===========================================
/* 멀티스레드 (Multi Thread) */
MyThread1 th1 = new MyThread1();
MyThread2 th2 = new MyThread2();
th1.start();
th2.start();
System.out.println("main 종료");
}
}
class MyThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 300; i++) {
System.out.printf("-");
}
System.out.println("th1 종료");
}
}
class MyThread2 extends Thread {
@Override
public void run() {
for (int i = 0; i < 300; i++) {
System.out.printf("|");
}
System.out.println("th2 종료");
}
}
- 메인 스레드가 종료되더라도 실행 중인 스레드가 하나라도 있다면 프로세스는 종료되지 않습니다.
📌메인 스레드
모든 자바 어플리케이션은 메인 스레드(main thread)가 main() 메소드를 실행하면서 시작
메인 스레드는 필요에 따라 작업 스레드들을 만들어서 병렬로 코드 실행이 가능하다.
싱글 스레드 어플리케이션에서는 메인 스레드를 종료하면 프로세스도 종료되지만, 멀티 스레드 애플리케이션처럼 실행 중인 스레드가 하나라도 있다면, 프로세스는 종료되지 않는다.
(메인 스레드가 먼저 종료되더라도 상관X)
- 학생마다 Student 객체를 생성하고
4명의 학생 정보를 ArrayList 컬렉션에 저장한 후에,
ArrayList의 모든 학생(4명) 정보를 출력하고 학생 이름을 입력 받아
해당 학생의 학점 평균을 출력하는 프로그램을 작성하라.학생 이름, 학과, 학번, 학점평균 입력하세요. \>> 황기태, 모바일, 1, 4.1 \>> 이재문, 안드로이드, 2, 3.9 \>> 김남윤, 웹공학, 3, 3.5 \>> 최찬미, 빅데이터, 4, 4.25 ---------------------------------- 이름: 황기태 학과: 모바일 학번: 1 학점평균: 4.1 ---------------------------------- 이름: 이재문 학과: 안드로이드 학번: 2 학점평균: 3.9 ---------------------------------- 이름: 김남윤 학과: 웹공학 학번: 3 학점평균: 3.5 ---------------------------------- 이름: 최찬미 학과: 빅데이터 학번: 4 학점평균: 4.25 ---------------------------------- 학생 이름 >> 최찬미 최찬미, 빅데이터, 4, 4.25 학생 이름 >> 이재문 이재문, 안드로이드, 2, 3.9 학생 이름 >> 그만
ArayList 대신, HashMap<String, Studnet> 해시맵을 이용하여 다시 작성하라. 해시맵에서 키는 학생 이름으로 한다.