본문 바로가기
Java/이것이 자바다

이자바 12장(멀티 스레드) 확인문제

by k-mozzi 2023. 4. 25.
반응형
Preface

 

이번 장의 내용 자체가 워낙 복잡했어서 그런지 확인 문제에는 어려운 문제가 없다.


 

- 2번

package ch12;

class MovieThread extends Thread {
	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			System.out.println("동영상을 재생합니다.");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}
}

class MusicRunnable implements Runnable {
	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			System.out.println("음악을 재생합니다.");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}
}

public class Exercise2 {

	public static void main(String[] args) {
		Thread thread1 = new MovieThread();
		thread1.start();

		Thread thread2 = new Thread(new MusicRunnable());
		thread2.start();
	}

}

 

 

- 8번

package ch12;

class MovieThread2 extends Thread {
	@Override
	public void run() {
		while (true) {
			System.out.println("동영상을 재생합니다.");
			if(Thread.interrupted()) {
				break;
			}
		}
		System.out.println("quit");
	}
}

public class Exercise8 {

	public static void main(String[] args) {
		Thread thread = new MovieThread2();
		thread.start();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}

		thread.interrupt();
	}

}

 

 

- 10번

package ch12;

class MovieThread3 extends Thread {
	@Override
	public void run() {
		while (true) {
			System.out.println("동영상을 재생합니다.");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
	}
}

public class Exercise10 {

	public static void main(String[] args) {

		Thread thread = new MovieThread3();
		thread.setDaemon(true);
		thread.start();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
		}
	}

}

 

728x90
반응형

'Java > 이것이 자바다' 카테고리의 다른 글

이자바 13장(제네릭) 확인문제  (0) 2023.04.26
제네릭  (0) 2023.04.25
멀티 스레드  (0) 2023.04.21
이자바 11장(기본 API 클래스) 확인문제  (0) 2023.04.15
기본 API 클래스 (3)  (0) 2023.04.15

댓글