05 다음 JAVA로 구현된 프로그램을 분석하여 괄호에 들어갈 알맞은 답을 쓰시오.
class Car implements Runnable {
int a;
public void run() {
try {
while(++a < 100) {
System.out.println("miles traveled : " + a);
Thread.sleep(100);
}
} catch(Exception E) {}
}
}
public class Test {
public static void main(String args[]) {
Thread t1 = new Thread(new ( )());
t1.start();
}
}
답
더보기
Car
해설
코드 | 실행순서 및 해석 | ||
1 | class Car implements Runnable { | Runnable 인터페이스 구현 | |
2 | int a; | ||
3 | public void run() { | 쓰레드 내에서 어떤 작업을 할 지를 알려주는 메서드 | |
4 | try { | ||
5 | while(++a < 100) { | ||
6 | System.out.println("miles traveled : " + a); | 출력: miles traveled : 1 출력: miles traveled : 2 … 출력: miles traveled : 99 |
|
7 | Thread.sleep(100); | thread를 0.1초 간 일시정지 후 반복 | |
8 | } | ||
9 | } catch(Exception E) {} | ||
10 | } | ||
11 | } | ||
12 | public class Test { | ||
13 | public static void main(String args[]) { | 1 | 실행 |
14 | Thread t1 = new Thread(new ( )()); | 2 | 쓰레드 구현 Thread 클래스의 객체 변수를 t1으로 선언 runnable interface를 상속받은 Car 클래스를 써 주어야 한다 |
15 | t1.start(); | 3 | start는 run() 메서드 호출 |
16 | } | ||
17 | } |
Thread: 하나의 프로세스 내에서 독립적으로 실행되는 작업 단위
→ 한 프로세스 내에서 한 가지 이상의 일을 동시에 할 수 있다
<Thread를 구현하는 방식>
1) Thread를 상속받아서 사용
class A extends Thread {
public void run() {
작업내용
}
}
2) Runnable Interface를 구현해서 사용하는 방법
class A implements Runnable {
public void run() {
작업내용
}
}
'자격증 > 정보처리기사 실기 - 기출문제' 카테고리의 다른 글
[정보처리기사 실기] 2022년 1회 07 - 프로그래밍 [Python] (0) | 2025.07.19 |
---|---|
[정보처리기사 실기] 2022년 1회 06 - 데이터베이스 (0) | 2025.07.19 |
[정보처리기사 실기] 2022년 1회 04 - SQL (0) | 2025.07.19 |
[정보처리기사 실기] 2022년 1회 03 - 데이터베이스 (0) | 2025.07.19 |
[정보처리기사 실기] 2022년 1회 02 - 용어 (0) | 2025.07.19 |