您的位置 首页 java

Java多线程学习(二)——Thread的一些方法介绍

1、current Thread ()方法

currentThread()方法可以返回代码被 那个 线程调用的信息。

测试方法如下:

输出内容:

构造器中线程名字:main
this is MyThread
run方法中线程名字:myThread-name
 

2、isAlive()方法

判断当前线程是否处于活跃状态,活跃状态是指线程已经启动并且尚未终止。

测试代码:

输出结果:

begin start false
after start true
run is alive = true
 

3、sleep()方法

sleep()方法是让正在运行的线程在指定毫秒数内暂停。

测试代码:

输出结果:

begin time = 1551138333348
begin
end
end time = 1551138335348
 

可以看出时间相差2秒。

4、getId()方法

获取线程的唯一标识。

5、停止线程

停止线程意味着在线程处理完任务之前停掉正在做的操作,也就是放弃当前操作。有以下三种方法终止正在运行中的线程:

  1. 使用退出标志,使线程正常退出,就是当run方法完成后终止线程;
  2. 使用stop方法钱箱终止线程,但是不推荐,因为stop和suspend及resume一样是过期作废方法;
  3. 使用interrupt中断线程。

5.1 interrupt方法

interrupt()方法的使用并不像for+break语句那样,马上就停止循环。调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真的停止线程。所以引出this.interrupted()和this.isInterrupted()方法。

  1. this.interrupted()是判断当前线程是否已经是中断状态,执行此方法后具有将线程的中断状态标志清除为false的功能;
  2. this.isInterrupted()是判断当前线程是否已经是中断状态,但是不清除状态标志。

所以使用interrupt()时需要判断线程是否有中断标志,在使用return或者抛异常的方式中断此线程。

5.2 stop()方法

stop方法是暴力停止线程,已经弃用的方法不建议使用。而且使用可能会抛出 Java .lang.ThreadDeath异常。如果强制让线程停止则可能使一些清理性的工作的不能完成。另一种情况就是对锁定的对象解锁,出现数据不一致的情况。

5.3 暂停线程

暂停线程可以使用suspend()方法,使用resume()方法恢复。但是这两个方法都是被废弃的方法,不建议使用。这两个方法如果使用不当会造成同步对象的独占,是其他线程无法访问公共同步对象;也有可能产生数据不同步的情况。

所以建议使用wait()方法暂停线程,使用notify()或者notifyAll()方法唤醒线程,这两种方法会在线程后面的文章线程通信部分讲解。

6、yield()方法

yield()方法的作用的是放弃当前的CPU资源,将他让给其他的任务去占用CPU执行时间,但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片。

7、线程的优先级

在操作系统中,线程可以划分优先级,优先级高的线程得到的CPU资源较多,也就是说CPU优先执行优先级高的线程。线程的优先级具有继承性,比如A线程启动B线程,则B线程的优先级与A线程的一样。

修改优先级的方法是setPriority()。

优先级高的线程总是大部分先执行,但不代表优先级高的线程全部先执行。线程优先级还具有“随机性”,也就是优先级高的线程不一定每次都先执行。

8、守护线程

在Java线程中有两种线程,一种是用户线程,另一种就是守护线程。守护线程具有陪伴的含义,当进程中不存在非守护线程了,则守护线程自动销毁。典型的守护线程就是垃圾回收线程。可以通过调用Thead类的setDaemon(true)方法设置当前的线程为守护线程。

注意事项:

  1. setDaemon(true)必须在start()方法前执行,否则会抛出IllegalThreadStateException异常;
  2. 在守护线程中产生的新线程也是守护线程 3. 不是所有的任务都可以分配给守护线程来执行,比如读写操作或者计算逻辑。
public class DaemonThread extends Thread {
 private int i = 0;
 @Override
 public void run() {
 super.run();
 try {
 while (true){
 i++;
 System.out.println("i = " + i);
 Thread.sleep(1000);
 }
 }catch (Exception e){
 e.printStackTrace();
 }
 }
}
public class DaemonThread extends Thread {
 private int i = 0;
 @Override
 public void run() {
 super.run();
 try {
 while (true){
 i++;
 System.out.println("i = " + i);
 Thread.sleep(1000);
 }
 }catch (Exception e){
 e.printStackTrace();
 }
 }
}
 

输出结果:

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
我离开thread对象也不再打印了,也就是停止了!
 

欢迎关注我

文章来源:智云一二三科技

文章标题:Java多线程学习(二)——Thread的一些方法介绍

文章地址:https://www.zhihuclub.com/196504.shtml

关于作者: 智云科技

热门文章

评论已关闭

3条评论

  1. PMDA was performed to a depth of approximately 100 microns, including removal of epidermis and disruption of the papillary dermis detectable by a shiny, whitish appearance inducing the formation of small pinpoints of blood in the treated area

  2. It became possible to design FLEx vectors Flip excision vectors that only allow one inversion event when it was discovered that it is not the exact sequence of the asymmetric target site core but its 8 bp length that is critical for Cre and FLP function

网站地图