Skip to content

Commit 9c7ec12

Browse files
committed
修正创建线程文章中的编辑错误
1 parent 627e407 commit 9c7ec12

File tree

1 file changed

+70
-88
lines changed

1 file changed

+70
-88
lines changed

java/concurrence/CreateThread.md

Lines changed: 70 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,26 @@
77
(3)调用线程对象的start\(\)方法来启动该线程。
88

99
```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+
}
3230

3331
}
3432
```
@@ -44,32 +42,27 @@ public class FirstThreadTest extends Thread{
4442
(3)调用线程对象的start\(\)方法来启动该线程。
4543

4644
```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+
7366
}
7467
```
7568

@@ -84,46 +77,35 @@ public class RunnableThreadTest implements Runnable
8477
(4)调用FutureTask对象的get\(\)方法来获得子线程执行结束后的返回值,调用get\(\)方法会阻塞线程。
8578

8679
```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+
}
127109
```
128110

129111
## 四、创建线程的三种方式的对比

0 commit comments

Comments
 (0)