Java多线程-线程常用方法
1,getName(); 返回该线程的名称。
2,currentThread();返回对当前正在执行的线程对象的引用。
3,isAlive();测试线程是否处于活动状态。
4,sleep();线程休眠。
5,setPriority(int newPriority);更改线程的优先级。
6,yield();暂停当前正在执行的线程对象,并执行其他线程。
下面给出一些实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
com.java1234.chap09.sec04;
public
class
Demo1
implements
Runnable{
@Override
public
void
run() {
// TODO Auto-generated method stub
for
(
int
i=
0
;i<
10
;i++){
// 获取当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName()+
":"
+i);
// 返回线程的名称
}
}
public
static
void
main(String[] args) {
Demo1 demo1=
new
Demo1();
new
Thread(demo1).start();
new
Thread(demo1).start();
new
Thread(demo1,
"线程3"
).start();
}
}
|
运行输出:
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9
线程3:0
线程3:1
线程3:2
线程3:3
线程3:4
线程3:5
线程3:6
线程3:7
线程3:8
线程3:9
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
com.java1234.chap09.sec04;
public
class
Demo2
implements
Runnable{
@Override
public
void
run() {
// TODO Auto-generated method stub
for
(
int
i=
0
;i<
10
;i++){
// 获取当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName()+
":"
+i);
// 返回线程的名称
}
}
public
static
void
main(String[] args) {
Demo2 demo2=
new
Demo2();
Thread t1=
new
Thread(demo2);
System.out.println(
"t1是否活动:"
+t1.isAlive());
t1.start();
System.out.println(
"t1是否活动:"
+t1.isAlive());
}
}
|
运行输出:
t1是否活动:false
t1是否活动:true
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
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
|
package
com.java1234.chap09.sec04;
public
class
Demo3
implements
Runnable{
@Override
public
void
run() {
// TODO Auto-generated method stub
for
(
int
i=
0
;i<
10
;i++){
try
{
Thread.sleep(
1000
);
// 获取当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName()+
":"
+i);
// 返回线程的名称
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public
static
void
main(String[] args) {
Demo3 demo1=
new
Demo3();
new
Thread(demo1).start();
}
}
|
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
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
29
30
31
32
33
34
|
package
com.java1234.chap09.sec04;
public
class
Demo4
implements
Runnable{
@Override
public
void
run() {
// TODO Auto-generated method stub
for
(
int
i=
0
;i<
10
;i++){
try
{
Thread.sleep(
1000
);
// 获取当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName()+
":"
+i);
// 返回线程的名称
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public
static
void
main(String[] args) {
Demo4 demo4=
new
Demo4();
Thread t1=
new
Thread(demo4,
"线程A"
);
Thread t2=
new
Thread(demo4,
"线程B"
);
Thread t3=
new
Thread(demo4,
"线程C"
);
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t3.start();
t1.start();
t2.start();
}
}
|
线程C:0
线程A:0
线程B:0
线程A:1
线程C:1
线程B:1
线程A:2
线程B:2
线程C:2
线程A:3
线程C:3
线程B:3
线程A:4
线程C:4
线程B:4
线程A:5
线程B:5
线程C:5
线程A:6
线程C:6
线程B:6
线程A:7
线程C:7
线程B:7
线程A:8
线程C:8
线程B:8
线程A:9
线程C:9
线程B:9
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
29
30
31
32
|
package
com.java1234.chap09.sec04;
public
class
Demo5
implements
Runnable{
@SuppressWarnings
(
"static-access"
)
@Override
public
void
run() {
// TODO Auto-generated method stub
for
(
int
i=
0
;i<
10
;i++){
try
{
Thread.sleep(
100
);
// 获取当前线程
Thread t=Thread.currentThread();
System.out.println(t.getName()+
":"
+i);
// 返回线程的名称
if
(i==
5
){
System.out.println(
"线程礼让:"
);
Thread.currentThread().yield();
}
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public
static
void
main(String[] args) {
Demo5 demo1=
new
Demo5();
new
Thread(demo1,
"线程A"
).start();
new
Thread(demo1,
"线程B"
).start();
}
}
|
线程A:0
线程B:0
线程A:1
线程B:1
线程A:2
线程B:2
线程A:3
线程B:3
线程A:4
线程B:4
线程A:5
线程礼让:
线程B:5
线程礼让:
线程A:6
线程B:6
线程A:7
线程B:7
线程A:8
线程B:8
线程A:9
线程B:9