当前位置: 移动技术网 > IT编程>开发语言>Java > 【并发编程】Thread类的详细介绍

【并发编程】Thread类的详细介绍

2019年12月06日  | 移动技术网IT编程  | 我要评论

本博客系列是学习并发编程过程中的记录总结。由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅。


thread类是java中实现多线程编程的基础类。本篇博客就来介绍下thread类的常用api和常见用法。

thread类常用的方法如下:

  • thread.activecount():这个方法用于返回当前线程的线程组中活动线程的数量,返回的值只是一个估计值,因为当此方法遍历内部数据结构时,线程数可能会动态更改。)。
  • thread.checkaccess(): 检验当前正在执行的线程是否有权限修改thread的属性,这个方法我们一般不自己进行调用,thread类的set方法在进行属性修改时都会先调用这个方法。
  • thread.currentthread():获取当前正在运行的线程。
  • thread.dumpstack():输出线程栈,一般在debug的时候调用。
  • thread.enumerate(thread tarray[]):??使用场景。
  • thread.getallstacktraces():获取系统中所有线程的线程栈信息。
  • thread.getname():获取线程的名字。
  • thread.getpriority():获取线程的优先级。
  • thread.getstacktrace():获取堆栈信息。
  • thread.getstate():获取线程状态。
  • thread.getthreadgroup():获取线程所在线程组。
  • thread.interrupt():使得指定线程中断阻塞状态,并将阻塞标志位置为true。
  • thread.interrupted():测试当前线程是否被中断。
  • thread.isalive():判断线程是否还存活着。
  • thread.isdaemon():判断线程是否是守护线程。
  • thread.join():在当前线程中加入指定线程,使得当前线程必须等待指定线程运行结束之后,才能结束。可以理解成线程插队、等待该线程终止。
  • thread.sleep(long):强制线程睡眠一段时间。
  • thread.start():启动一个线程。
  • thread.setname(name):设置线程的名字。
  • thread.setpriority(priority):设置线程的优先级。
  • thread.setdaemon(true):将指定线程设置为守护线程。
  • thread.yield():使得当前线程退让出cpu资源,把cpu调度机会分配给同样线程优先级的线程。
  • object.wait()、object.notify()、object.notifyall():object类提供的线程等待和线程唤醒方法。

还有thread类提供了功能丰富的构造函数,大家可以选合适的使用

示例代码

public class mythread  {

    public static void main(string[] args) {
          thread thread = thread.currentthread();
        
                //这个方法返回的是当前线程所在线程组以及这个线程组的子线程组内活动的线程数
                //这个值是一个估计值,所以这个方法的应用场景不大
                int activecount = thread.activecount();
                system.out.println("当前系统中活动线程数["+activecount+"]");
        
                //向标准错误输出流输出当前的线程栈,不会阻断程序的继续执行
                thread.dumpstack();
        
                //获取所有线程栈信息
                map<thread, stacktraceelement[]> allstacktraces = thread.getallstacktraces();
        
                //获取类加载器
                classloader contextclassloader = thread.getcontextclassloader();
        
                //获取当前线程名字
                string threadname = thread.getname();
                system.out.println("current thread name["+threadname+"]");
        
                //获取当前线程id
                long threadid = thread.getid();
                system.out.println("current thread id["+threadid+"]");
        
                //获取当前线程的优先级,一共有1~10总共10个优先级,这个优先级并不是在
                //所有平台都生效的
                int priority = thread.getpriority();
                system.out.println("current thread priority["+priority+"]");
        
                stacktraceelement[] stacktrace = thread.getstacktrace();
                system.out.println("-------------stacktrace info--------------");
                for (int i = 0; i < stacktrace.length; i++) {
                    stacktraceelement element = stacktrace[i];
                    system.out.println("classname:["+element.getclassname()+"]");
                    system.out.println("filename:["+element.getfilename()+"]");
                    system.out.println("line nunber:["+element.getlinenumber()+"]");
                    system.out.println("method name:["+element.getmethodname()+"]");
                    system.out.println("is native method:["+element.isnativemethod()+"]");
                    system.out.println("------------------------------------------");
                }
        
                thread.state state = thread.getstate();
                system.out.println("thread state:["+state+"]");
        
                threadgroup threadgroup = thread.getthreadgroup();
                string threadgroupname = threadgroup.getname();
                system.out.println("thread group name:["+threadgroupname+"]");
        
                //线程睡眠,调用sleep方法会使得线程进入timed_waiting状态,如果线程已经
                //获得了锁资源,调用sleep方法是不会释放这个锁的
                thread.sleep(2000,500);
                thread.sleep(1000);
                timeunit.seconds.sleep(2);
        
                thread thread1 = new thread(new runnable() {
                    @sneakythrows
                    @override
                    public void run() {
                        timeunit.seconds.sleep(100);
                    }
                });
                thread1.start();
                thread1.join(50);

    }


}

守护线程

守护线程可以理解为服务线程,他们的作用就是服务于其他用户线程。当系统中不存在其他用户线程时,这些守护线程也会自动消亡。比如jvm的垃圾清理线程就是守护线程。我们可以使用如下方法查看和设置线程是否是守护线程。

thread.isdaemon();
thread.setdaemon(true);

join方法

调用线程的join方法会使得调用线程进入waiting状态,直到被调用的线程执行结束,调用线程才会重新获得执行的机会。

public class mythread  {

    public static void main(string[] args) throws exception {
    
        thread thread1 = new thread(new runnable() {
            @sneakythrows
            @override
            public void run() {
                timeunit.seconds.sleep(100);
            }
        });
        thread1.start();
        thread1.join();
        system.out.println("main thread end...");
    }

}

上面的代码中,main线程调用了thread1的join方法,main线程会被挂起进入waiting状态,直到thread1执行完毕之后,main线程才有机会重新获得执行机会。

join方法还有一个重载方法,这个方法可以指定超时时间。

thread1.join(50);

如果thread1线程在50ms内还没执行完,main线程就可以重新获得执行机会。

yeild方法

调用线程的yield方法不是一定会成功。

  • 退让成功时,退让线程会由running(运行)转为runnable(就绪)状态。
  • 退让了的线程,与其他同优先级级别的线程一样,同样有再次获取cpu使用权的机会。

中断

先贴上一段对线程中断的总结。

  • 除非是线程自己interrupt()自己,否则checkaccess()方法都会被调用,并可能抛出一个securityexception异常。
    如果当前线程处于blocked阻塞(因为调用wait、sleep和join造成的)状态时被interrupt了,那么[中断标志位]将被清除,并且收到一个interruptedexception异常。
  • 如果当前线程处于blocked阻塞(因为nio的interruptiblechannel进行的i/o操作造成的)状态时被interrupt了,则会关闭channel,[中断标志位]将会被置为true,并且当前线程会收到一个closedbyinterruptexception异常。
  • 如果当前线程处于blocked阻塞(因为nio的selector造成的)状态时被interrupt了,那么[中断标志位]将被置为true,然后当前线程会立即从选择器区域返回并返回值(可能为非零的值)。
  • 如果前面的情况都没有发生,则线程会将[中断标志位]将被置为true。

更加易懂的说法(不包括nio部分):

  • interrupt()方法并不是中断线程,而是中断阻塞状态,或者将线程的[中断标志位]置为true。
  • 对于未阻塞的线程,interrupt()只是造成[中断标志位]=rue,线程本身运行状态不受影响。
  • 对于阻塞的线程,interrupt()会中断阻塞状态,使其转换成非阻塞状态,并清除[中断标志位]。
  • 造成阻塞状态的情况有:sleep()、wait()和join()。
  • 阻塞状态的线程被中断时,只是中断了阻塞状态,即sleep()、wait()和join(),线程本身还在继续运行。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网