본문 바로가기

하../java

ch13 싱글 스레드와 멀티 스레드 스레드의 i/o블로킹

main스레드

- main메서드의 코드를 수행하는 스레드

- 스레드는 '사용자 스레드'와 '데몬 스레드'(보조 스레드) 두 종류가 있다.

실행 중인 사용자 스레드가 한도 없을때 프로그램은 종료된다.

(메인 스레드는 종료되어도 보조 스레드가 실행중이면 종료되지 않음)

 

	ThreadEx11_1 th1 = new ThreadEx11_1();
		ThreadEx11_2 th2 = new ThreadEx11_2();
		th1.start();
		th2.start();
		startTime = System.currentTimeMillis();

		try {
			th1.join();	// main쓰레드가 th1의 작업이 끝날 때까지 기다린다.
			th2.join();	// main쓰레드가 th2의 작업이 끝날 때까지 기다린다.
		} catch(InterruptedException e) {}

		System.out.print("소요시간:" + (System.currentTimeMillis() - Ex13_11.startTime));
	} // main
}

class ThreadEx11_1 extends Thread {
	public void run() {
		for(int i=0; i < 300; i++) {
			System.out.print(new String("-"));
		}
	} // run()
}

class ThreadEx11_2 extends Thread {
	public void run() {
		for(int i=0; i < 300; i++) {
			System.out.print(new String("|"));
		}
	} // run()
}
출력값 
----||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||--------
---------------------------------------|||||||||||||||||||||||||||||
|||||||||||||||||||------|||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||소요시간:5

 

싱글 스레드

public static void main(String args[]) {
		long startTime = System.currentTimeMillis();

		for(int i=0; i < 50; i++)
			System.out.printf("%s", new String("-"));		

		System.out.print("소요시간1:" +(System.currentTimeMillis()- startTime)); 

		for(int i=0; i < 50; i++) 
			System.out.printf("%s", new String("|"));		

 		System.out.print("소요시간2:"+(System.currentTimeMillis() - startTime));
	}
}
출력값
--------------------------------------------------소요시간1:21||||||||||||||||||||||
||||||||||||||||||||||||||||소요시간2:22

멀티스레드

public static void main(String args[]) {
		ThreadEx3_1 th1 = new ThreadEx3_1();
		th1.start();
		startTime = System.currentTimeMillis();

		for(int i=0; i < 50; i++)
			System.out.printf("%s", new String("-"));	

		System.out.print("소요시간1:" + (System.currentTimeMillis() - Ex13_3.startTime));
	} 
}

class ThreadEx3_1 extends Thread {
	public void run() {
		for(int i=0; i < 50; i++)
			System.out.printf("%s", new String("|"));	

		System.out.print("소요시간2:" + (System.currentTimeMillis() - Ex13_3.startTime));
	}
}
출력값 :
--------------------------------------------------||||||
|||||||소요시간1:19|||||||||||||||||||||||||||||||||||||소요시간2:21
번갈아 가면서 작업

번갈아가면서 하기때문에 contextSwitching이 발생하면서 멀티스레드가 시간이 조금 더 걸림

시간이 조금 더 걸리더라도 두가지 작업을 동시에 할수 있다는 장점이있음.

 

스레드의 I/O블로킹(input/output blocking)입출력

public static void main(String[] args) throws Exception {
		String input = JOptionPane.showInputDialog("아무 값이나 입력하세요."); 
		System.out.println("입력하신 값은 " + input + "입니다.");

		for(int i=10; i > 0; i--) {
			System.out.println(i);
			try {
				Thread.sleep(1000);  // 1초간 시간을 지연한다.
			} catch(Exception e ) {}
		}
	}
}
싱글스레드는 값을 입력하기 전까 아래 작업을 실행하지 못함
입력하신 값은 5입니다.
10
9
8
7
6
5
4
3
2
1

멀티스레드는 입력을 하지 않더라도 따로 작업을 하기떄문에 빨리 작업을 끝낼수 있음

public static void main(String[] args) throws Exception  {
		ThreadEx5_1 th1 = new ThreadEx5_1();
		th1.start();

		String input = JOptionPane.showInputDialog("아무 값이나 입력하세요."); 
		System.out.println("입력하신 값은 " + input + "입니다.");
	}
}

class ThreadEx5_1 extends Thread {
	public void run() {
		for(int i=10; i > 0; i--) {
			System.out.println(i);
			try {
				sleep(1000);
			} catch(Exception e ) {}
		}
	} // run()
}
10
9
8
입력하신 값은 23입니다.
7
6
5
4
3
2
1