프로세스(Process)란?
단순히 실행 중인 프로그램
- 사용자가 작성한 프로그램이 운영체제에 의해 메모리 공간을 할당 받아 실행 중인 것
- 프로그램에 사용되는 데이터와 메모리 등의 자원 그리고 스레드로 구성됨
스레드(Thread)란?
프로세스 내에서 실제로 작업을 수행하는 주체
- 모든 프로세스에는 한 개 이상의 스레드가 존재하여 작업을 수행
- 두 개 이상의 스레드를 가지는 프로세스를 멀티 스레드 프로세스라고 함
스레드의 생성과 실행
- Runnable 인터페이스를 구현하는 방법
- public class Test01 implements Runnable { @Override public void run() { // 스레드 실행코드 } }
- Thread 클래스를 상속받는 방법
- public class Test01 extends Thread { @Override public void run() { // 스레드 실행코드 } }
스레드 메소드
static void sleep(long msec) | msec에 지정된 밀리초 동안 대기 |
String getName() | 스레드의 이름을 s로 설정 |
void setName(String s) | 스레드의 이름을 s로 설정 |
void start() | 스레드를 시작, run() 메소드 호출 |
void join() throws InterruptedException | 스레드가 끝날 때까지 대기 |
void run() | 스레드가 실행할 부분 기술(오버라이딩 사용) |
void suspend() | 스레드가 일시정지 resume()에 의해 다시 시작 가능 |
void resume() | 일시 정지된 스레드를 다시 시작 |
setPriority()/getPriority() | 우선순위 |
- Thread 클래스 상속
class MyThread extends Thread{ @Override public void run() { int i; for(i = 0; i<200; i++) { System.out.print(i + "\t"); } } } public class ThreadTest { public static void main(String[] args) { System.out.println(Thread.currentThread()); MyThread th1 = new MyThread(); th1.start(); MyThread th2 = new MyThread(); th2.start(); } }
- Runnable 인터페이스 구현
class MyThread2 implements Runnable{ public void run(){ int i; for(i=0; i<200; i++){ System.out.print(i + "\t"); } } } public class ThreadTest2 { public static void main(String[] args) { System.out.println("main start"); MyThread2 mth = new MyThread2(); Thread th1 = new Thread(mth); th1.start(); Thread th2 = new Thread(new MyThread2()); th2.start(); System.out.println("main end"); } }
'Programming Language > JAVA' 카테고리의 다른 글
[JAVA] 인스턴스 메서드 (0) | 2023.10.19 |
---|---|
[JAVA] 오버로딩 & 오버라이딩 (0) | 2023.10.19 |
[JAVA] String 메소드 (0) | 2023.10.19 |
[JAVA] 재귀함수 (0) | 2023.10.19 |
[JAVA] 데이터 타입 크기 (0) | 2023.10.19 |