반응형
*스레드 (Thread)
- 스레드 : 어떠한 프로그램(특히 프로세스) 내에서 실행되는 흐름의 단위.
- 프로세스 내 명령어 블록으로 시작점, 종료점을 가진다.
- 실행중에 멈출 수 있으며 동시 수행도 가능하다.
- 프로세스 : 메모리를 할당받아 실행 중인 프로그램.
1. 싱글 스레드 (Single Thread - Thread 클래스 상속)
class Main {
public static void main(String[] args) {
AutoThread th = new AutoThread();
th.start();
}
}
class AutoThread extends Thread {
boolean work = true;
public void run() {
while(work) {
try {
System.out.println("실행 중");
Thread.sleep(10000); // 1000 = 1초
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2. 싱글 스레드 (Single Thread - Runnable 인터페이스 상속) : 가장 많이 쓰이는 방법.
class Main {
public static void main(String[] args) {
AutoThread at = new AutoThread();
Thread th = new Thread(at);
th.start();
}
}
class AutoThread implements Runnable {
boolean work = true;
@Override
public void run() {
while(work) {
try {
System.out.println("실행 중");
Thread.sleep(10000); // 1000 = 1초
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3. 멀티 스레드 (Multi Thread)
class Main {
public static void main(String[] args) {
AutoThread at1 = new AutoThread();
AutoThread at2 = new AutoThread();
Thread th1 = new Thread(at1);
Thread th2 = new Thread(at2);
th1.start();
th2.start();
}
}
class AutoThread implements Runnable {
boolean work = true;
@Override
public void run() {
while(work) {
try {
System.out.println("실행 중");
Thread.sleep(10000); // 1000 = 1초
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
반응형
'프로그래밍 > 자바, JDBC' 카테고리의 다른 글
SQL 명령어 (0) | 2020.06.16 |
---|---|
MySQL, JDBC 연동 (0) | 2020.06.16 |
뉴렉처 학습(JDBC) 1강~2강 (0) | 2020.06.15 |
JDBC, MySQL Driver (0) | 2020.06.15 |
뉴렉처 학습(서블릿/JSP) 52강 (0) | 2020.06.04 |