Skip to content

Commit 1a937ee

Browse files
committed
Today I Learned Thread
1 parent a60bc81 commit 1a937ee

8 files changed

Lines changed: 355 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package thread.control.interrupt;
2+
3+
import util.MyLogger;
4+
import util.ThreadUtils;
5+
6+
import static util.ThreadUtils.sleep;
7+
8+
public class ThreadStopMainV1 {
9+
public static void main(String[] args) {
10+
MyTask work = new MyTask();
11+
Thread thread = new Thread(work, "work");
12+
thread.start();
13+
14+
sleep(4000);
15+
MyLogger.log("Thread Flag == false");
16+
work.runFlag = false;
17+
18+
}
19+
20+
static class MyTask implements Runnable {
21+
22+
volatile boolean runFlag = true;
23+
24+
@Override
25+
public void run() {
26+
while(runFlag){
27+
MyLogger.log("작업 중");
28+
sleep(3000);
29+
30+
}
31+
MyLogger.log("자원 정리");
32+
MyLogger.log("자원 종료");
33+
}
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package thread.control.interrupt;
2+
3+
import util.MyLogger;
4+
5+
import static util.ThreadUtils.sleep;
6+
7+
public class ThreadStopMainV2 {
8+
public static void main(String[] args) {
9+
MyTask work = new MyTask();
10+
Thread thread = new Thread(work, "work");
11+
thread.start();
12+
13+
sleep(4000);
14+
MyLogger.log("작업 중지 요청 Interrupt");
15+
thread.interrupt();
16+
MyLogger.log("인터럽트 상태 " + thread.isInterrupted());
17+
18+
}
19+
20+
static class MyTask implements Runnable {
21+
22+
@Override
23+
public void run() {
24+
try {
25+
while(true) {
26+
MyLogger.log("작업 중");
27+
Thread.sleep(3000);
28+
}
29+
} catch (InterruptedException e) {
30+
MyLogger.log("work 스레드 인터럽트 상태 = " + Thread.currentThread().isInterrupted() );
31+
MyLogger.log("interrupt Message() = " + e.getMessage());
32+
MyLogger.log("Thread State() = " + Thread.currentThread().getState());
33+
}
34+
MyLogger.log("자원 정리");
35+
MyLogger.log("자원 종료");
36+
37+
}
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package thread.control.interrupt;
2+
3+
import util.MyLogger;
4+
5+
import static util.ThreadUtils.sleep;
6+
7+
public class ThreadStopMainV3 {
8+
public static void main(String[] args) {
9+
MyTask work = new MyTask();
10+
Thread thread = new Thread(work, "work");
11+
thread.start();
12+
13+
sleep(100);
14+
MyLogger.log("작업 중지 요청 Interrupt");
15+
thread.interrupt();
16+
MyLogger.log("인터럽트 상태 1 " + thread.isInterrupted());
17+
18+
}
19+
20+
static class MyTask implements Runnable {
21+
22+
@Override
23+
public void run() {
24+
while(!Thread.currentThread().isInterrupted()) {
25+
MyLogger.log("작업 중");
26+
}
27+
MyLogger.log("work 스레드 인터럽트 상태 = " + Thread.currentThread().isInterrupted() );
28+
29+
try {
30+
MyLogger.log("자원 정리");
31+
Thread.sleep(1000);
32+
MyLogger.log("자원 정리 완료");
33+
} catch (InterruptedException e) {
34+
MyLogger.log("자원 정리 실패 - 자원 정리 중 인터럽트 발생");
35+
MyLogger.log("work 스레드 인터럽트 상태3 = " + Thread.currentThread().isInterrupted());
36+
}
37+
MyLogger.log("자원 종료");
38+
39+
40+
41+
42+
43+
}
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package thread.control.interrupt;
2+
3+
import util.MyLogger;
4+
5+
import static util.ThreadUtils.sleep;
6+
7+
public class ThreadStopMainV4 {
8+
public static void main(String[] args) {
9+
MyTask work = new MyTask();
10+
Thread thread = new Thread(work, "work");
11+
thread.start();
12+
13+
sleep(100);
14+
MyLogger.log("작업 중지 요청 Interrupt");
15+
thread.interrupt();
16+
MyLogger.log("인터럽트 상태 1 " + thread.isInterrupted());
17+
18+
}
19+
20+
static class MyTask implements Runnable {
21+
22+
@Override
23+
public void run() {
24+
while(!Thread.interrupted()) {
25+
MyLogger.log("작업 중");
26+
}
27+
MyLogger.log("work 스레드 인터럽트 상태 = " + Thread.currentThread().isInterrupted() );
28+
29+
try {
30+
MyLogger.log("자원 정리");
31+
Thread.sleep(1000);
32+
MyLogger.log("자원 정리 완료");
33+
} catch (InterruptedException e) {
34+
MyLogger.log("자원 정리 실패 - 자원 정리 중 인터럽트 발생");
35+
MyLogger.log("work 스레드 인터럽트 상태3 = " + Thread.currentThread().isInterrupted());
36+
}
37+
MyLogger.log("자원 종료");
38+
39+
40+
41+
42+
43+
}
44+
}
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package thread.control.printer;
2+
3+
import util.MyLogger;
4+
5+
import java.util.Queue;
6+
import java.util.Scanner;
7+
import java.util.concurrent.ConcurrentLinkedQueue;
8+
9+
import static util.MyLogger.log;
10+
import static util.ThreadUtils.sleep;
11+
12+
public class MyPrinterV1 {
13+
14+
public static void main(String[] args) {
15+
Printer printer = new Printer();
16+
Thread printerThread = new Thread(printer, "printer");
17+
printerThread.start();
18+
19+
Scanner userInput = new Scanner(System.in);
20+
while(true){
21+
log("프린터할 문서를 입력하세요. 종료(q): ");
22+
String input = userInput.nextLine();
23+
if(input.equals("q")) {
24+
printer.work = false;
25+
break;
26+
}
27+
printer.addJob(input);
28+
}
29+
}
30+
31+
static class Printer implements Runnable {
32+
volatile boolean work = true;
33+
Queue<String> jobQueue = new ConcurrentLinkedQueue<>();
34+
35+
@Override
36+
public void run() {
37+
while(work){
38+
if(jobQueue.isEmpty()){
39+
continue;
40+
}
41+
String job = jobQueue.poll();
42+
log("출력 시작 " + job + ", 대기 문서 : " + jobQueue);
43+
sleep(3000);
44+
}
45+
log("프린터 종료");
46+
}
47+
48+
public void addJob(String input){
49+
jobQueue.offer(input);
50+
}
51+
}
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package thread.control.printer;
2+
3+
import java.util.Queue;
4+
import java.util.Scanner;
5+
import java.util.concurrent.ConcurrentLinkedQueue;
6+
7+
import static util.MyLogger.log;
8+
import static util.ThreadUtils.sleep;
9+
10+
public class MyPrinterV2 {
11+
12+
public static void main(String[] args) {
13+
Printer printer = new Printer();
14+
Thread printerThread = new Thread(printer, "printer");
15+
printerThread.start();
16+
17+
Scanner userInput = new Scanner(System.in);
18+
while(true){
19+
log("프린터할 문서를 입력하세요. 종료(q): ");
20+
String input = userInput.nextLine();
21+
if(input.equals("q")) {
22+
printerThread.interrupt();
23+
break;
24+
}
25+
printer.addJob(input);
26+
}
27+
}
28+
29+
static class Printer implements Runnable {
30+
Queue<String> jobQueue = new ConcurrentLinkedQueue<>();
31+
32+
@Override
33+
public void run() {
34+
while(!Thread.interrupted()){
35+
if(jobQueue.isEmpty()){
36+
continue;
37+
}
38+
39+
try {
40+
String job = jobQueue.poll();
41+
log("출력 시작 " + job + ", 대기 문서 : " + jobQueue);
42+
Thread.sleep(3000);
43+
log("출력 완료");
44+
} catch (InterruptedException e) {
45+
log("인터럽트!!");
46+
break;
47+
}
48+
}
49+
log("프린터 종료");
50+
}
51+
52+
public void addJob(String input){
53+
jobQueue.offer(input);
54+
}
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package thread.control.printer;
2+
3+
import java.util.Queue;
4+
import java.util.Scanner;
5+
import java.util.concurrent.ConcurrentLinkedQueue;
6+
7+
import static util.MyLogger.log;
8+
9+
public class MyPrinterV4 {
10+
11+
public static void main(String[] args) {
12+
Printer printer = new Printer();
13+
Thread printerThread = new Thread(printer, "printer");
14+
printerThread.start();
15+
16+
Scanner userInput = new Scanner(System.in);
17+
while(true){
18+
log("프린터할 문서를 입력하세요. 종료(q): ");
19+
String input = userInput.nextLine();
20+
if(input.equals("q")) {
21+
printerThread.interrupt();
22+
break;
23+
}
24+
printer.addJob(input);
25+
}
26+
}
27+
28+
static class Printer implements Runnable {
29+
Queue<String> jobQueue = new ConcurrentLinkedQueue<>();
30+
31+
@Override
32+
public void run() {
33+
while(!Thread.interrupted()){
34+
if(jobQueue.isEmpty()){
35+
Thread.yield();
36+
continue;
37+
}
38+
39+
try {
40+
String job = jobQueue.poll();
41+
log("출력 시작 " + job + ", 대기 문서 : " + jobQueue);
42+
Thread.sleep(3000);
43+
log("출력 완료");
44+
} catch (InterruptedException e) {
45+
log("인터럽트!!");
46+
break;
47+
}
48+
}
49+
log("프린터 종료");
50+
}
51+
52+
public void addJob(String input){
53+
jobQueue.offer(input);
54+
}
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package thread.control.yield;
2+
3+
import util.MyLogger;
4+
import util.ThreadUtils;
5+
6+
public class YieldMain {
7+
static final int THREAD_COUNT = 1000;
8+
public static void main(String[] args){
9+
for(int i = 0; i < THREAD_COUNT; i++) {
10+
Thread thread = new Thread(new MyRunnable());
11+
thread.start();
12+
}
13+
}
14+
15+
static class MyRunnable implements Runnable {
16+
@Override
17+
public void run() {
18+
for(int i = 0; i<10; i++){
19+
MyLogger.log(Thread.currentThread().getName() + " - " + i);
20+
// System.out.println(Thread.currentThread().getName() + " - " + i);
21+
// ThreadUtils.sleep(1);
22+
// Thread.yield();
23+
}
24+
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)