Java Thread class 와 Runnable interface

2023. 2. 11. 16:36Java

프로젝트 내 src 내 javabasic 패키지 내 Ex2RunnableTest.java

package javabasic;

public class Ex2RunnableTest implements Runnable {
	String msg;
	int num;

	public Ex2RunnableTest(int num, String msg) {
		this.num = num;
		this.msg = msg;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i <= num; i = i + 1) {
			System.out.println(i + ":" + msg);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ex2RunnableTest ex1 = new Ex2RunnableTest(200, "one");
		Ex2RunnableTest ex2 = new Ex2RunnableTest(200, "two");
		Ex2RunnableTest ex3 = new Ex2RunnableTest(200, "three");
		Thread th1 = new Thread(ex1);
		Thread th2 = new Thread(ex2);
		Thread th3 = new Thread(ex3);

		// run 메소드 호출..여러개의 스레드가 스레드 스케줄러에 의해서 권한이 돌아가면서 실행되는걸 확인할수있다
		th1.start();
		th2.start();
		th3.start();

	}

}

첫 번째 실행 결과

1:one
2:one
3:one
4:one
5:one
6:one
7:one
8:one
9:one
10:one
11:one
12:one
13:one
14:one
15:one
16:one
17:one
18:one
19:one
20:one
21:one
22:one
23:one
24:one
25:one
26:one
27:one
28:one
29:one
30:one
31:one
32:one
33:one
34:one
35:one
36:one
37:one
38:one
39:one
40:one
41:one
42:one
43:one
44:one
45:one
46:one
47:one
48:one
49:one
50:one
51:one
52:one
53:one
54:one
55:one
1:two
2:two
3:two
4:two
5:two
6:two
7:two
8:two
9:two
10:two
11:two
12:two
13:two
14:two
15:two
16:two
17:two
18:two
19:two
20:two
56:one
21:two
57:one
58:one
22:two
59:one
60:one
23:two
61:one
24:two
62:one
25:two
26:two
63:one
27:two
64:one
28:two
29:two
30:two
65:one
31:two
32:two
33:two
34:two
66:one
35:two
36:two
37:two
67:one
68:one
69:one
70:one
71:one
72:one
73:one
74:one
75:one
76:one
77:one
78:one
79:one
80:one
81:one
82:one
83:one
38:two
84:one
39:two
85:one
86:one
87:one
88:one
89:one
90:one
91:one
92:one
93:one
94:one
40:two
95:one
41:two
96:one
42:two
97:one
43:two
98:one
44:two
99:one
45:two
100:one
46:two
101:one
102:one
103:one
104:one
105:one
47:two
106:one
48:two
107:one
49:two
108:one
1:three
50:two
51:two
52:two
53:two
54:two
55:two
56:two
2:three
109:one
3:three
57:two
58:two
59:two
60:two
61:two
62:two
4:three
110:one
5:three
63:two
6:three
111:one
7:three
64:two
8:three
112:one
9:three
65:two
10:three
113:one
11:three
66:two
67:two
12:three
114:one
13:three
68:two
14:three
115:one
15:three
69:two
16:three
116:one
17:three
70:two
18:three
117:one
19:three
71:two
72:two
20:three
118:one
21:three
73:two
22:three
119:one
23:three
74:two
24:three
120:one
25:three
75:two
26:three
121:one
27:three
76:two
28:three
122:one
29:three
77:two
30:three
123:one
31:three
78:two
32:three
124:one
33:three
79:two
34:three
125:one
35:three
80:two
36:three
126:one
37:three
81:two
38:three
127:one
39:three
40:three
41:three
42:three
82:two
83:two
84:two
43:three
44:three
45:three
46:three
47:three
128:one
48:three
85:two
49:three
129:one
50:three
86:two
51:three
130:one
52:three
87:two
53:three
131:one
54:three
88:two
55:three
132:one
56:three
89:two
57:three
133:one
58:three
90:two
59:three
60:three
134:one
61:three
91:two
62:three
63:three
64:three
135:one
65:three
92:two
66:three
136:one
137:one
67:three
93:two
68:three
69:three
138:one
70:three
94:two
71:three
139:one
72:three
73:three
74:three
95:two
75:three
76:three
140:one
77:three
96:two
78:three
141:one
79:three
97:two
80:three
142:one
81:three
98:two
82:three
143:one
83:three
99:two
84:three
144:one
85:three
100:two
86:three
145:one
87:three
101:two
88:three
146:one
89:three
90:three
91:three
92:three
102:two
93:three
94:three
95:three
96:three
97:three
98:three
147:one
99:three
103:two
100:three
101:three
148:one
102:three
104:two
103:three
104:three
149:one
105:three
105:two
106:three
150:one
107:three
106:two
108:three
151:one
109:three
107:two
110:three
152:one
111:three
112:three
108:two
109:two
113:three
153:one
114:three
110:two
115:three
154:one
116:three
111:two
117:three
155:one
118:three
112:two
119:three
120:three
121:three
122:three
156:one
123:three
113:two
124:three
125:three
126:three
157:one
127:three
114:two
128:three
158:one
129:three
130:three
131:three
132:three
115:two
133:three
134:three
135:three
136:three
137:three
138:three
139:three
140:three
141:three
142:three
143:three
144:three
145:three
146:three
147:three
148:three
149:three
150:three
151:three
152:three
153:three
154:three
155:three
156:three
157:three
158:three
159:three
160:three
161:three
162:three
163:three
164:three
165:three
159:one
166:three
167:three
168:three
169:three
170:three
116:two
171:three
172:three
173:three
160:one
174:three
117:two
175:three
161:one
176:three
177:three
178:three
179:three
118:two
180:three
181:three
182:three
183:three
184:three
185:three
162:one
186:three
187:three
188:three
189:three
190:three
191:three
192:three
193:three
119:two
194:three
195:three
163:one
164:one
165:one
166:one
167:one
196:three
120:two
121:two
197:three
168:one
169:one
170:one
171:one
172:one
173:one
198:three
122:two
199:three
174:one
200:three
123:two
124:two
125:two
126:two
127:two
128:two
129:two
130:two
131:two
132:two
133:two
134:two
135:two
175:one
136:two
176:one
177:one
178:one
179:one
180:one
181:one
182:one
137:two
138:two
139:two
140:two
141:two
142:two
183:one
143:two
144:two
145:two
146:two
147:two
148:two
149:two
150:two
151:two
152:two
153:two
154:two
155:two
156:two
157:two
158:two
159:two
160:two
161:two
162:two
163:two
164:two
165:two
166:two
167:two
168:two
169:two
170:two
171:two
172:two
184:one
173:two
174:two
185:one
175:two
176:two
177:two
178:two
179:two
186:one
180:two
187:one
181:two
182:two
183:two
184:two
185:two
186:two
187:two
188:two
189:two
190:two
191:two
192:two
193:two
194:two
195:two
188:one
196:two
197:two
198:two
199:two
189:one
200:two
190:one
191:one
192:one
193:one
194:one
195:one
196:one
197:one
198:one
199:one
200:one

두 번째 실행 결과

1:two
2:two
3:two
1:three
2:three
1:one
2:one
3:three
4:three
5:three
6:three
7:three
8:three
9:three
10:three
11:three
12:three
4:two
5:two
6:two
7:two
8:two
9:two
10:two
11:two
12:two
13:three
14:three
15:three
16:three
17:three
18:three
19:three
20:three
21:three
22:three
23:three
24:three
25:three
26:three
27:three
3:one
28:three
29:three
30:three
31:three
32:three
33:three
34:three
35:three
36:three
37:three
13:two
14:two
15:two
16:two
17:two
38:three
39:three
40:three
4:one
5:one
6:one
41:three
42:three
43:three
44:three
45:three
46:three
47:three
48:three
49:three
50:three
51:three
52:three
53:three
54:three
55:three
56:three
18:two
19:two
20:two
21:two
22:two
23:two
24:two
25:two
26:two
27:two
28:two
29:two
30:two
31:two
32:two
33:two
34:two
35:two
36:two
37:two
38:two
39:two
40:two
41:two
42:two
43:two
44:two
45:two
46:two
47:two
48:two
49:two
50:two
51:two
52:two
53:two
54:two
55:two
56:two
57:two
58:two
59:two
60:two
57:three
58:three
59:three
7:one
8:one
9:one
10:one
11:one
12:one
13:one
14:one
60:three
61:three
62:three
63:three
64:three
65:three
66:three
67:three
68:three
69:three
70:three
71:three
61:two
72:three
73:three
15:one
16:one
17:one
18:one
19:one
20:one
21:one
22:one
23:one
24:one
25:one
26:one
27:one
28:one
29:one
30:one
31:one
32:one
33:one
34:one
35:one
74:three
75:three
76:three
77:three
78:three
79:three
80:three
62:two
63:two
64:two
65:two
66:two
67:two
68:two
69:two
70:two
71:two
72:two
73:two
74:two
75:two
76:two
77:two
78:two
79:two
80:two
81:two
82:two
83:two
84:two
85:two
86:two
87:two
88:two
89:two
90:two
81:three
82:three
83:three
84:three
85:three
86:three
36:one
37:one
38:one
39:one
40:one
41:one
42:one
87:three
91:two
92:two
93:two
94:two
95:two
96:two
97:two
98:two
99:two
88:three
89:three
90:three
91:three
43:one
92:three
100:two
101:two
102:two
93:three
94:three
95:three
96:three
97:three
98:three
44:one
99:three
103:two
100:three
45:one
101:three
104:two
102:three
46:one
103:three
105:two
104:three
47:one
105:three
106:three
107:three
108:three
109:three
106:two
107:two
108:two
109:two
110:two
111:two
112:two
113:two
114:two
115:two
116:two
117:two
118:two
119:two
120:two
121:two
122:two
123:two
124:two
125:two
126:two
127:two
128:two
129:two
130:two
131:two
132:two
133:two
134:two
110:three
111:three
112:three
113:three
114:three
48:one
49:one
50:one
115:three
116:three
117:three
118:three
119:three
120:three
121:three
122:three
123:three
124:three
125:three
126:three
127:three
128:three
129:three
130:three
131:three
132:three
133:three
134:three
135:three
136:three
135:two
136:two
137:two
138:two
139:two
140:two
141:two
142:two
143:two
144:two
137:three
138:three
139:three
140:three
141:three
142:three
143:three
144:three
145:three
146:three
147:three
148:three
149:three
150:three
151:three
152:three
153:three
154:three
155:three
156:three
157:three
51:one
52:one
53:one
54:one
55:one
56:one
57:one
158:three
159:three
160:three
161:three
162:three
163:three
164:three
165:three
166:three
167:three
168:three
169:three
170:three
171:three
145:two
146:two
147:two
148:two
149:two
172:three
58:one
59:one
60:one
173:three
174:three
175:three
176:three
177:three
178:three
179:three
180:three
150:two
151:two
152:two
153:two
154:two
155:two
156:two
157:two
181:three
182:three
183:three
184:three
185:three
61:one
62:one
63:one
64:one
65:one
66:one
67:one
68:one
69:one
70:one
71:one
72:one
73:one
74:one
75:one
76:one
77:one
78:one
79:one
186:three
187:three
188:three
189:three
190:three
191:three
192:three
193:three
194:three
195:three
196:three
197:three
198:three
199:three
200:three
158:two
159:two
160:two
161:two
162:two
80:one
81:one
82:one
83:one
84:one
85:one
86:one
87:one
88:one
89:one
90:one
91:one
92:one
93:one
94:one
95:one
96:one
97:one
98:one
99:one
100:one
101:one
102:one
103:one
104:one
105:one
106:one
107:one
108:one
163:two
164:two
165:two
166:two
167:two
168:two
169:two
170:two
171:two
172:two
173:two
174:two
175:two
176:two
177:two
178:two
109:one
110:one
111:one
112:one
113:one
114:one
115:one
116:one
117:one
118:one
119:one
120:one
121:one
122:one
123:one
179:two
180:two
181:two
182:two
183:two
184:two
124:one
185:two
186:two
125:one
187:two
126:one
127:one
128:one
129:one
130:one
131:one
188:two
189:two
190:two
191:two
192:two
193:two
194:two
195:two
196:two
197:two
198:two
199:two
200:two
132:one
133:one
134:one
135:one
136:one
137:one
138:one
139:one
140:one
141:one
142:one
143:one
144:one
145:one
146:one
147:one
148:one
149:one
150:one
151:one
152:one
153:one
154:one
155:one
156:one
157:one
158:one
159:one
160:one
161:one
162:one
163:one
164:one
165:one
166:one
167:one
168:one
169:one
170:one
171:one
172:one
173:one
174:one
175:one
176:one
177:one
178:one
179:one
180:one
181:one
182:one
183:one
184:one
185:one
186:one
187:one
188:one
189:one
190:one
191:one
192:one
193:one
194:one
195:one
196:one
197:one
198:one
199:one
200:one

위 코드로 쓰여진 Java Application 을 실행 할 때 마다 다른 결과가 나온다.

java.lang.Thread

Thread 클래스

하나으 thread 란 한 프로그램 안에서 봤을 때 실행의 한 맥락(한 흐름, 한 주제) 이다.

thread 는 어떠한 프로그램 내에서, 특히 프로세스 내에서 실행되는 흐름의 단위를 말한다. 일반적으로 한 프로그램은

하나의 스레드를 가지고 있지만, 프로그램 환경에 따라 둘 이상의 스레드를 동시에 실행할 수 있다. 이러한 실행 방식을

멀티스레드라고 한다.

Java 가상 Machine 은 한개의 application 이 

실행하면서 여러개의 threads 를 동시에 작동시킬 수 있도록 허용한다.

모든 thread 는 각기 우선순위를 지닌다.

높은 우선순위를 지닌 Threads 들은 

낮은 우선순위를 지닌 threads 보다 우선적으로 실행된다.

한개의 Java 가상 Machine 이 완전히 제대로 준비된 상태로 시작할 때는,

한개으 daemon 이 아닌 thread 가 있고, 요 한개의 daemon 이 아닌 thread 는

일반적으로(전형적으로) 어떤 설계된 클래스에 있는 main 이라고 불리는 메서드를 호출한다.

 

실행을 하는 동안 한개의 새 thread 를 생성하는 두가지 방법이 있다.

한가지는 한 클래스를 Thread 클래스의 하위클래스(자식클래스)로써 선언하는 것이다.

고 하위(자식)클래스는 Thread 클래스의 run 메서드를 override 해야 한다.

고 하위(자식)클래스의 인스턴스(객체) 하나는 그러면 할당될 수 있으며 시작될 수 있다.

 

한개으 thread 를 생성하는 다른 방법은 Runnable 인터페이스(규격) 를 구현하는 하나의 클래스를 선언하는 것이다.

고 클래스는 그리고 나서 run 메서드를 구현한다.

그리고는 고 클래스의 객체 하나가 할당될 수 있고, 

Thread 를 생성할 때 고 클래스의 객체가 argument 로 전달되어질 수 있으며,

그 다음에는 고 클래스의 객체가 시작되어질 수 있다.

 

모든 thread 는 식별 목적을 위해서 각기 하나의 이름을 가진다.

한개 thread 이상이 같은 이름을 가질 수도 있다.

만약에 하나의 thread 가 생성될 때 이름이 기입되지 않는다면, 

하나의 새 이름이 그것을 위해 생성된다.

 

java.lang.Thread.Thread(Runnable target)

Thread(Runnable target) 생성자 메서드는

하나의 새 Thread 클래스의 객체를 할당한다.

요 생성자 메서드는 Thread (null, target, gname) 과 같은 효과를 가지는데, 여기서 gname 이란 한개의 새롭게 생성된 이름을 말한다.

자동적으로 생성된 이름들은 "Thread-"+n 형태를 따르는데, 여기서 n 은 정수이다.

 

파라메터 Runnable target 은 

다음과 같은 객체이다.

생성될 새로운 요 thread 가 시작할 때

Runnable 인터페이스(규격) 의 객체인 target 이 지닌 run 메서드가 호출되어진다.

만약에 Runnable target 파라메터에 argument 로써 null 값을 전달한다면, 

요기서 run 메서드는 아무것도 하지 않는다.

 

java.lang.Runnable

 

Runnable 인터페이스(규격) 는 어떤 클래스에 의해서 구현되어져야 하는데,

그 어떠한 클래스의 각각의 객체들(각각의 인스턴스들) 은 

제각각 한 개의 thread 로 실행되게 의도되어진다.

 

각각의 객체들(인스턴스들) 마다

각각 한개의 thread(한개의 맥락, 한개의 context, 한개의 주제, 한개의 테마,한개의 흐름) 로 실행되도록 의도되어진 고러한 객체들(인스턴스들) 을 생성하는 아무 클래스에 의해서 Runnable 인터페이스(규격) 가 구현되어져야 한다.

그 어떤 클래스는 run 이라고 불리는 아무 arguments 도 없는 메서드를 반드시 정의해야 한다.

 

이 Runnable 인터페이스(규격) 는 객체들을 위해서 하나의 공통된 프로토콜(통신 규약) 을 제공하려고 설계되었고,

왜냐하면, 그 객체들이 활동하는 동안에 

그 다른 객체들이 코드를 실행하기를 원하기 때문이다.

예를 들자면, Runnable 인터페이스(규격) 를 Thread 클래스가 구현한다.

그 객체들이 활동한다는 것은 단순하게

한개의 thread 가 이전부터 지금까지 계속 시작되서 그 흐름을 타고 계속 작동되고 있으며 아직 멈추지 않고 작동하고 있다는 것을 의미한다.

 

게다가, Runnable 인터페이스(규격) 는 

Thread 클래스를 상속받지 않고서도

하나의 어떤 클래스가 활동할 수 있는 방법들을 제공한다. 

 

하나의 어떤 클래스가 Runnable 인터페이스(규격) 를 구현하게 된다면

한개의 Thread 객체를 초기화하는 것과 하나의 어떤 클래스 자기 자신을 Runnable target 파라메터의 argument 로써 전달하는 것으로 인해서

Thread 클래스를 상속받지 않은 채로

run 메서드를 작동할 수 있다.

 

대부분의 경우에, 그 Runnable 인터페이스(규격) 는, 

만약에 개발자가 오로지 run() 메서드만 override 하고 그 외에 다른 어떤 Thread 클래스으 메서드들은 override 하지 않을 계획을 하고 있다면 그 Runnable 인터페이스(규격) 가 이용되어져야 한다.

이것은 중요하다 왜냐하면

만약에 프로그래머가 클래스의 근본적인 행동(기능)을 수정하거나 개선하려는 의도가 없다면

클래스는 상속받지 말아야 하고

클래스는 하위클래스가 되지 말아야 하기 때문이다.

 

 

'Java' 카테고리의 다른 글

Java InetAddress  (0) 2023.02.11
Java start()  (0) 2023.02.11
Java run()  (0) 2023.02.11
Java KeyEvent  (0) 2023.02.11
Java List component  (0) 2023.02.09