一 通过继承Thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package
thread;
public
class
ThreadDemo1 {
public
static
void
main(String[] args) {
Demo1 demo1 =
new
Demo1(
"zifangsky"
);
Demo1 demo2 =
new
Demo1(
"admin"
);
demo1.start();
demo2.start();
}
}
class
Demo1
extends
Thread{
private
String name;
public
Demo1(String name){
this
.name = name;
}
public
void
run(){
System.out.println(
"Hi,"
+ name +
"欢迎您的访问"
);
}
}
|
二 通过实现Runnable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package
thread;
public
class
ThreadDemo2{
public
static
void
main(String[] args) {
Demo2 demo1 =
new
Demo2(
"zifangsky"
);
Demo2 demo2 =
new
Demo2(
"admin"
);
Thread thread1 =
new
Thread(demo1);
thread1.start();
Thread thread2 =
new
Thread(demo2);
thread2.start();
}
}
class
Demo2
implements
Runnable {
private
String name;
public
Demo2(String name) {
this
.name = name;
}
public
void
run() {
System.out.println(
"Hi,"
+ name +
"欢迎你的访问"
);
}
}
|
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1722421,如需转载请自行联系原作者