Os 수업을 들었다면 알거라고 생각하지만 한번더 정리하는 개념으로 

Process 

실행중인 프로그램

OS로부터 메모리르 할당 받음

 

Thread

실제 프로그램이 수행되는 작업의 최소 단위

하나의 프로세스에는 하나이상의 스레드를 가지게됨 

 

자바에서 thread 사용법 

Thread 클래스를 상속 받거나 Runnable 인터페이스를 상속 받는 방법입니다.

그냥 A클래스에서 Thread 클래스만 상속 받는다면 Class A extends Thread를 사용하면됩니다. 

하지만 이미 클래스에서 Class A extend B를 했다면 일반적인 클래스는 다중상속이 안되므로 

다중 상속이 가능한 인터페이스인  Runnable 인터페이스를 상속해줍니다.

Class A extend B implements Runnable

package Test01;


class MyThread extends Thread{
	public void run() {
		int i;
		for( i=0;i<10;i++) {
			System.out.print(i+"\t");
		
			try {
				 sleep(100);
			} catch (Exception e) {
				// TODO: handle exception
			}
	
		
		
		}
		
	}
}


public class Main {

	
	
	public static void main(String [] args) {
		System.out.println("start");
		MyThread th1=new MyThread();
		MyThread th2=new MyThread();
		
		th1.start();
		th2.start();
		System.out.println("end");
		
		
      
	}
	
	
	
}

결과:

 

 

package Test01;


class MyThread implements Runnable{
	public void run() {
		int i;
		for( i=0;i<10;i++) {
			System.out.print(i+"\t");
		
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO: handle exception
			}
	
		
		
		}
		
	}

	
}


public class Main {

	
	
	public static void main(String [] args) {
		System.out.println("start");
		MyThread th1=new MyThread();
		Thread th1Thread=new Thread(th1);
		
		MyThread th2=new MyThread();
		Thread th2Thread=new Thread(th2);
		
		th1Thread.start();
		th2Thread.start();
		
		System.out.println("end");
		
		
      
	}
	
	
	
}

결과:

 

 

'프로그래밍언어 > JAVA' 카테고리의 다른 글

<JAVA>멀티 스레드 프로그래밍 -2  (0) 2020.12.13
<JAVA>멀티스래드 프로그래밍-1  (0) 2020.12.13
<JAVA>데코레이터 패턴  (0) 2020.12.13
<JAVA>몇가지 추가 입출력 클래스  (0) 2020.12.13
<JAVA>직렬화  (0) 2020.12.13

+ Recent posts