-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPrinterV4.java
More file actions
56 lines (47 loc) · 1.56 KB
/
Copy pathMyPrinterV4.java
File metadata and controls
56 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package thread.control.printer;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ConcurrentLinkedQueue;
import static util.MyLogger.log;
public class MyPrinterV4 {
public static void main(String[] args) {
Printer printer = new Printer();
Thread printerThread = new Thread(printer, "printer");
printerThread.start();
Scanner userInput = new Scanner(System.in);
while(true){
log("프린터할 문서를 입력하세요. 종료(q): ");
String input = userInput.nextLine();
if(input.equals("q")) {
printerThread.interrupt();
break;
}
printer.addJob(input);
}
}
static class Printer implements Runnable {
Queue<String> jobQueue = new ConcurrentLinkedQueue<>();
@Override
public void run() {
while(!Thread.interrupted()){
if(jobQueue.isEmpty()){
Thread.yield();
continue;
}
try {
String job = jobQueue.poll();
log("출력 시작 " + job + ", 대기 문서 : " + jobQueue);
Thread.sleep(3000);
log("출력 완료");
} catch (InterruptedException e) {
log("인터럽트!!");
break;
}
}
log("프린터 종료");
}
public void addJob(String input){
jobQueue.offer(input);
}
}
}