tobby48 4 years ago
parent
commit
6922668b08
1 changed files with 113 additions and 0 deletions
  1. 113
    0
      src/main/java/kr/co/swh/lecture/opensource/thread/ExcutorExample.java

+ 113
- 0
src/main/java/kr/co/swh/lecture/opensource/thread/ExcutorExample.java View File

@@ -0,0 +1,113 @@
1
+package kr.co.swh.lecture.opensource.thread; 
2
+
3
+import java.io.FileInputStream;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+import java.util.concurrent.Callable;
7
+import java.util.concurrent.ExecutionException;
8
+import java.util.concurrent.ExecutorService;
9
+import java.util.concurrent.Executors;
10
+import java.util.concurrent.Future;
11
+
12
+import org.apache.log4j.Logger;
13
+
14
+
15
+/**
16
+ * <pre>
17
+ * kr.co.swh.lecture.opensource.thread 
18
+ * ExcutorExample.java
19
+ *
20
+ * 설명 :
21
+ * </pre>
22
+ * 
23
+ * @since : 2020. 10. 11.
24
+ * @author : tobby48
25
+ * @version : v1.0
26
+ */
27
+public class ExcutorExample {
28
+
29
+	private Logger log = Logger.getLogger(this.getClass());
30
+
31
+	public static void main(String[] args) {
32
+		// TODO Auto-generated method stub
33
+		
34
+		ExecutorService executor = Executors.newFixedThreadPool(4);
35
+		ExecutorService executor1 = Executors.newSingleThreadExecutor();
36
+		
37
+		executor1.execute(new ThreadRunner());
38
+		executor1.submit(new ThreadRunner());
39
+		
40
+		Future<Message> result = executor1.submit(new ThreadRunnerCall());
41
+		try {
42
+			System.out.println(result.get());
43
+		} catch (InterruptedException e) {
44
+			// TODO Auto-generated catch block
45
+			e.printStackTrace();
46
+		} catch (ExecutionException e) {
47
+			// TODO Auto-generated catch block
48
+			e.printStackTrace();
49
+		}
50
+
51
+		List<ThreadRunnerCall> list = new ArrayList<ThreadRunnerCall>();
52
+		list.add(new ThreadRunnerCall());
53
+		list.add(new ThreadRunnerCall());
54
+		list.add(new ThreadRunnerCall());
55
+		list.add(new ThreadRunnerCall());
56
+		try {
57
+			List<Future<Message>> response = executor.invokeAll(list);
58
+			
59
+		} catch (InterruptedException e) {
60
+			// TODO Auto-generated catch block
61
+			e.printStackTrace();
62
+		}
63
+		
64
+		
65
+//		Thread t = new Thread(new ThreadRunner());
66
+//		t.start();
67
+//		
68
+//		ThreadRunner1 t2 = new ThreadRunner1();
69
+//		t2.start();
70
+	}
71
+
72
+}
73
+
74
+class Message{
75
+	private String name;
76
+	private String user;
77
+	public Message(String name, String user) {
78
+		this.name = name;
79
+		this.user = name;
80
+	}
81
+}
82
+class ThreadRunnerCall implements Callable<Message>{
83
+
84
+	@Override
85
+	public Message call() throws Exception {
86
+		// TODO Auto-generated method stub
87
+		System.out.print(1);
88
+
89
+//		return "정상처리되었습니다";
90
+		return new Message("돌샘", "정상처리되었습니다");
91
+	}
92
+
93
+}
94
+
95
+class ThreadRunner implements Runnable{
96
+
97
+	@Override
98
+	public void run() {
99
+		// TODO Auto-generated method stub
100
+		System.out.print(1);
101
+	}
102
+	
103
+}
104
+class ThreadRunner1 extends Thread {
105
+
106
+	@Override
107
+	public void run() {
108
+		// TODO Auto-generated method stub
109
+		System.out.print(1);
110
+	}
111
+	
112
+}
113
+