Java/이것이 자바다
이자바 12장(멀티 스레드) 확인문제
k-mozzi
2023. 4. 25. 17:35
반응형
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
반응형