7
7
(3)调用线程对象的start\(\) 方法来启动该线程。
8
8
9
9
``` java
10
- public class FirstThreadTest extends Thread {
11
- int i = 0 ;
12
- // 重写run方法,run方法的方法体就是现场执行体
13
- public void run ()
14
- {
15
- for (;i< 100 ;i++ ){
16
- System . out. println(getName()+ " " + i);
17
-
18
- }
19
- }
20
- public static void main (String [] args )
21
- {
22
- for (int i = 0 ;i< 100 ;i++ )
23
- {
24
- System . out. println(Thread . currentThread(). getName()+ " : " + i);
25
- if (i== 20 )
26
- {
27
- new FirstThreadTest (). start();
28
- new FirstThreadTest (). start();
29
- }
30
- }
31
- }
10
+ public class FirstThreadTest extends Thread {
11
+ int i = 0 ;
12
+
13
+ // 重写run方法,run方法的方法体就是现场执行体
14
+ public void run () {
15
+ for (; i < 100 ; i++ ) {
16
+ System . out. println(getName() + " " + i);
17
+
18
+ }
19
+ }
20
+
21
+ public static void main (String [] args ) {
22
+ for (int i = 0 ; i < 100 ; i++ ) {
23
+ System . out. println(Thread . currentThread(). getName() + " : " + i);
24
+ if (i == 20 ) {
25
+ new FirstThreadTest (). start();
26
+ new FirstThreadTest (). start();
27
+ }
28
+ }
29
+ }
32
30
33
31
}
34
32
```
@@ -44,32 +42,27 @@ public class FirstThreadTest extends Thread{
44
42
(3)调用线程对象的start\(\) 方法来启动该线程。
45
43
46
44
``` java
47
- public class RunnableThreadTest implements Runnable
48
- {
49
-
50
- private int i;
51
- public void run ()
52
- {
53
- for (i = 0 ;i < 100 ;i++ )
54
- {
55
- System . out. println(Thread . currentThread(). getName()+ " " + i);
56
- }
57
- }
58
- public static void main (String [] args )
59
- {
60
- for (int i = 0 ;i < 100 ;i++ )
61
- {
62
- System . out. println(Thread . currentThread(). getName()+ " " + i);
63
- if (i== 20 )
64
- {
65
- RunnableThreadTest rtt = new RunnableThreadTest ();
66
- new Thread (rtt," 新线程1" ). start();
67
- new Thread (rtt," 新线程2" ). start();
68
- }
69
- }
70
-
71
- }
72
-
45
+ public class RunnableThreadTest implements Runnable {
46
+ private int i;
47
+
48
+ public void run () {
49
+ for (i = 0 ; i < 100 ; i++ ) {
50
+ System . out. println(Thread . currentThread(). getName() + " " + i);
51
+ }
52
+ }
53
+
54
+ public static void main (String [] args ) {
55
+ for (int i = 0 ; i < 100 ; i++ ) {
56
+ System . out. println(Thread . currentThread(). getName() + " " + i);
57
+ if (i == 20 ) {
58
+ RunnableThreadTest rtt = new RunnableThreadTest ();
59
+ new Thread (rtt, " 新线程1" ). start();
60
+ new Thread (rtt, " 新线程2" ). start();
61
+ }
62
+ }
63
+
64
+ }
65
+
73
66
}
74
67
```
75
68
@@ -84,46 +77,35 @@ public class RunnableThreadTest implements Runnable
84
77
(4)调用FutureTask对象的get\(\) 方法来获得子线程执行结束后的返回值,调用get\(\) 方法会阻塞线程。
85
78
86
79
``` java
87
- public class CallableThreadTest implements Callable<Integer >
88
- {
89
-
90
- public static void main (String [] args )
91
- {
92
- CallableThreadTest ctt = new CallableThreadTest ();
93
- FutureTask<Integer > ft = new FutureTask<> (ctt);
94
- for (int i = 0 ;i < 100 ;i++ )
95
- {
96
- System . out. println(Thread . currentThread(). getName()+ " 的循环变量i的值" + i);
97
- if (i== 20 )
98
- {
99
- new Thread (ft," 有返回值的线程" ). start();
100
- }
101
- }
102
- try
103
- {
104
- System . out. println(" 子线程的返回值:" + ft. get());
105
- } catch (InterruptedException e)
106
- {
107
- e. printStackTrace();
108
- } catch (ExecutionException e)
109
- {
110
- e. printStackTrace();
111
- }
112
-
113
- }
114
-
115
- @Override
116
- public Integer call () throws Exception
117
- {
118
- int i = 0 ;
119
- for (;i< 100 ;i++ )
120
- {
121
- System . out. println(Thread . currentThread(). getName()+ " " + i);
122
- }
123
- return i;
124
- }
125
-
126
- }
80
+ public class CallableThreadTest implements Callable<Integer > {
81
+
82
+ public static void main (String [] args ) {
83
+ CallableThreadTest ctt = new CallableThreadTest ();
84
+ FutureTask<Integer > ft = new FutureTask<> (ctt);
85
+ for (int i = 0 ; i < 100 ; i++ ) {
86
+ System . out. println(Thread . currentThread(). getName() + " 的循环变量i的值" + i);
87
+ if (i == 20 ) {
88
+ new Thread (ft, " 有返回值的线程" ). start();
89
+ }
90
+ }
91
+ try {
92
+ System . out. println(" 子线程的返回值:" + ft. get());
93
+ } catch (InterruptedException e) {
94
+ e. printStackTrace();
95
+ } catch (ExecutionException e) {
96
+ e. printStackTrace();
97
+ }
98
+ }
99
+
100
+ @Override
101
+ public Integer call () throws Exception {
102
+ int i = 0 ;
103
+ for (; i < 100 ; i++ ) {
104
+ System . out. println(Thread . currentThread(). getName() + " " + i);
105
+ }
106
+ return i;
107
+ }
108
+ }
127
109
```
128
110
129
111
## 四、创建线程的三种方式的对比
0 commit comments