Cirry's Blog

线程常用的操作方法

2019-10-08
技术
java
最后更新:2024-03-22
6分钟
1136字

线程的命名与取得

  • 构造方法:public Thread(Runnable target , String name);
  • 设置名字:public final void setName(String name);
  • 取得名字:public final String getName();

范例:

1
package cn.cccc.demo;
2
class MyThread implements Runnable{
3
public void run (){
4
System.out.println(Thread.currentThread().getName());
5
}
6
}
7
public class ThreadDemo {
8
public static void main(String[] args) throws Exception {
9
MyThread mt = new MyThread();
10
new Thread(mt,"线程A").start();
11
new Thread(mt).start();
12
new Thread(mt,"线程B").start();
13
}
14
}
15
//线程A
2 collapsed lines
16
//Thread-0
17
//线程B

线程的休眠 休眠:public static void sleep(long millis ) throws InterruptedException; 休眠:public static void sleep(long millis, int nanos ) throws InterruptedException; 在进行休眠的时候可能会产生中断异常“InterruptedException”,中断异常属于Exception的子类必须处理。 范例:

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
new Thread(()->{
5
for (int x= 0 ; x <10; x++){
6
System.out.println(Thread.currentThread().getName()+"、x = " + x);
7
try {
8
Thread.sleep(1000); //暂缓执行
9
} catch (Exception e) {
10
// TODO Auto-generated catch block
11
e.printStackTrace();
12
}
13
}
14
},"线程对象").start();
15
}
1 collapsed line
16
}

线程中断 在之前发现线程休眠提供有一个中断异常,实际上就证明线程的休眠是可以被打断的。 在Thread类里面有提供这种中断执行的处理方法: 判断线程是否被中断:public boolean isInterrupteed(); 中断线程执行:public void interrupt();

范例:

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
Thread thread = new Thread(()->{
5
System.out.println("我要睡觉了");
6
try {
7
Thread.sleep(3000);
8
System.out.println("睡够了");
9
} catch (Exception e) {
10
// TODO Auto-generated catch block
11
System.out.println("敢打扰我睡觉,老子宰了你");
12
} //准备睡十秒
13
14
});
15
thread.start();
10 collapsed lines
16
Thread.sleep(1000);
17
if(!thread.isInterrupted()){
18
System.out.println("打扰一下你睡觉");
19
thread.interrupt();
20
}
21
}
22
}
23
//我要睡觉了
24
//打扰一下你睡觉
25
//敢打扰我睡觉,老子宰了你

所有执行的线程都是可以被中断的,中断线程必须进行异常的处理。

线程的强制执行: 所为的线程的强制执行指的是当满足某些条件之后,某一个线程对象将可以一直独占资源,一直到该线程的程序执行结束。 范例:观察一个没有强制执行的程序

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
Thread thread = new Thread(()->{
5
for (int x= 0; x < 100; x++) {
6
System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x);
7
}
8
},"玩耍的线程");
9
thread.start();
10
for(int x = 0; x < 100; x++){
11
System.out.println("[霸道的main线程]number = " + x);
12
}
13
}
14
}

这个时候主线程和子线程都在交替执行,一直抢占资源。如果希望主程序独占执行,那么就可以利用: 强制执行:public final void join() throws InterruptedException;

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
Thread mainThread = Thread.currentThread(); //获得主线程
5
Thread thread = new Thread(()->{
6
for (int x= 0; x < 100; x++) {
7
if(x ==3 ){ // 如果执行到等于3 了,则霸道线程加入
8
try {
9
mainThread.join();
10
} catch (Exception e) {
11
// TODO Auto-generated catch block
12
e.printStackTrace();
13
}
14
}
15
System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x);
8 collapsed lines
16
}
17
},"玩耍的线程");
18
thread.start();
19
for(int x = 0; x < 100; x++){
20
System.out.println("[霸道的main线程]number = " + x);
21
}
22
}
23
}

线程的礼让:线程的礼让指的是先将资源让出去让别的线程先执行。线程的礼让可以使用Thread中提供的方法: 礼让:public static void yield(); 范例:礼让操作

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
Thread mainThread = Thread.currentThread(); //获得主线程
5
Thread thread = new Thread(()->{
6
7
for (int x= 0; x < 100; x++) {
8
if(x %3 ==0 ){
9
Thread.yield(); //线程礼让
10
System.out.println("####线程的礼让操作####");
11
}
12
System.out.println(Thread.currentThread().getName() + " 执行、 x = " + x);
13
}
14
},"玩耍的线程");
15
thread.start();
5 collapsed lines
16
for(int x = 0; x < 100; x++){
17
System.out.println("[霸道的main线程]number = " + x);
18
}
19
}
20
}

每次调用yeild()方法只会礼让一次。

线程的优先级:从理论上来讲,线程的优先级越高就越有可能先执行。在Thread类里面针对优先级操作有两个处理方法 设置优先级:public final void setPriority(int newPriority) 获取优先级:public final int getPriority() 在进行优先级定义的时候都是通过int型的数字来完成的,而对于此数字的选择在Thread类里面就定义有三个常量: 最高优先级:public static final int MAX_PRIORITY;(10) 中等优先级:public static final int NORM_PRIORITY;(5) 最低优先级:public static final int MIN_PRIORITY;(1)

1
package cn.cccc.demo;
2
public class ThreadDemo {
3
public static void main(String[] args) throws Exception {
4
Runnable run = ()->{
5
for(int x= 0; x < 5; x ++){
6
try {
7
Thread.sleep(1000);
8
} catch (Exception e) {
9
// TODO Auto-generated catch block
10
e.printStackTrace();
11
}
12
System.out.println(Thread.currentThread().getName()+"执行");
13
}
14
};
15
Thread threadA = new Thread(run , "线程对象A");
11 collapsed lines
16
Thread threadB = new Thread(run , "线程对象B");
17
Thread threadC = new Thread(run , "线程对象C");
18
threadA.setPriority(1);
19
threadB.setPriority(5);
20
threadC.setPriority(10);
21
threadA.start();
22
threadB.start();
23
threadC.start();
24
25
}
26
}

优先级高的有可能限制性,而不是绝对先执行

本文标题:线程常用的操作方法
文章作者:Cirry
发布时间:2019-10-08
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode