Java多线程3:Thread中的实例方法
一、Thread类中的方法调用方式
学习Thread类中的方法是学习多线程的第一步。在学习多线程之前特别提出一点,调用Thread中的方法的时候,在线程类中,有两种方式,一定要理解这两种方式的区别:
1、this.XXX()
这种调用方式表示的线程是线程实例本身
2、Thread.currentThread.XXX()或Thread.XXX()
上面两种写法是一样的意思。这种调用方式表示的线程是正在执行Thread.currentThread.XXX()所在代码块的线程
当然,这么说,肯定有人不理解两者之间的差别。没有关系,之后会讲清楚,尤其是在讲Thread构造函数这块。讲解后,再回过头来看上面2点,会加深理解。
二、Thread类中的实例方法
从Thread类中的实例方法和类方法的角度讲解Thread中的方法,这种区分的角度也有助于理解多线程中的方法。实例方法,只和实例线程(也就是new出来的线程)本身挂钩,和当前运行的是哪个线程无关。看下Thread类中的实例方法:
1、start()方法
start()方法的作用讲得直白点就是通知"线程规划器",此线程可以运行了,正在等待CPU调用线程对象的run()方法,产生一个异步执行的效果。补充知识点:怎样理解阻塞非阻塞与同步异步的区别?
举例:
public class Thread01 implements Runnable{
@Override
public void run() {
for(int i = 0; i < 5; i++) {
try {
Thread.sleep((int) Math.random() * 5000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Runnable runnable = new Thread01();
Thread thread = new Thread(runnable);
thread.start();
for(int i = 0; i < 5; i++) {
try {
Thread.sleep((int) Math.random() * 5000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
第一次运行结果:
Thread-0
main
Thread-0
main
Thread-0
main
Thread-0
main
Thread-0
main
第二次运行结果:
main
Thread-0
main
Thread-0
main
Thread-0
Thread-0
Thread-0
main
main
可以看到,CPU调用哪个线程具有不确定性。再举例说明start()方法的顺序是否就是线程启动的顺序?
public class Thread01 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class Test {
public static void main(String[] args) {
Runnable runnable = new Thread01();
//创建线程1
Thread thread1 = new Thread(runnable);
//创建线程2
Thread thread2 = new Thread(runnable);
//创建线程3
Thread thread3 = new Thread(runnable);
thread1.start();
thread2.start();
thread3.start();
}
}
第一次运行的结果:
Thread-1
Thread-0
Thread-2
第二次运行的结果:
Thread-2
Thread-0
Thread-1
尽管启动线程的顺序是thread1,thread2,thread3,但是被调用的顺序却不是按照启动顺序来的。即:调用start()方法的顺序不代表线程的启动顺序,线程的启动顺序具有不确定性。
2、run()方法
线程开始执行,虚拟机调用的是线程run()方法中的内容,当线程没有通过start()方法让其处于被调用状态的时候,其线程的run()方法不会被执行的。但是如果直接通过.run()来调用的话,还是可以调用的到,但这个时候run()方法所在线程就是主线程了,仅仅只是一个普通方法而已,不在充当线程中的run()方法的作用。
举例:通过.start()方法获取run()方法所在的线程
public class Thread01 implements Runnable{
@Override
public void run() {
System.out.println("run方法所在的线程为:" + Thread.currentThread().getName());
}
}
public class Test {
public static void main(String[] args) {
Runnable runnable = new Thread01();
//创建线程
Thread thread1 = new Thread(runnable);
thread1.start();
}
}
结果:
run方法所在的线程为:Thread-0
可以看到run()方法所在的线程就是我们创建的线程,而不再main主线程内。
通过.run()方法获取run()方法所在的线程
public class Thread01 implements Runnable{
@Override
public void run() {
System.out.println("run方法所在的线程为:" + Thread.currentThread().getName());
}
}
public class Test {
public static void main(String[] args) {
Runnable runnable = new Thread01();
//创建线程
Thread thread1 = new Thread(runnable);
thread1.run();
}
}
结果:
run方法所在的线程为:main
可以看到,此时run()方法所在的线程就是main主线程,而不再是我们自己创建的那个线程。
所以,通过start()方法启动线程将自动调用 run()方法,这是由jvm的内存机制规定的。不通过start()方法来直接调用的话,run()方法只是thread的一个普通方法,还是在主线程里执行。
3、isAlive()方法
判断线程是否处于活动状态,只要线程处于启动且没有终止,返回的就是true。
/**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
*/
public final native boolean isAlive();
举例:
public class Thread01 extends Thread{
@Override
public void run() {
System.out.println("线程run时的isAlive=====" + this.isAlive());
}
}
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
System.out.println("线程被new出来还未被调用时的isAlive======" + thread01.isAlive());
thread01.start();
System.out.println("线程通过start被调用时的isAlive======" + thread01.isAlive());
try {
thread01.sleep(1000);
System.out.println("线程执行完时的isAlive======" + thread01.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结果:
线程被new出来还未被调用时的isAlive======false
线程通过start被调用时的isAlive======true
线程run时的isAlive=====true
线程执行完时的isAlive======false
看到在start()之前,线程的isAlive是false,start()之后就是true了。main函数中加上Thread.sleep(100)的原因是为了确保Thread06的run()方法中的代码执行完,否则有可能end这里打印出来的是true,如下去掉sleep方法之后
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
System.out.println("线程被new出来还未被调用时的isAlive======" + thread01.isAlive());
thread01.start();
System.out.println("线程通过start被调用时的isAlive======" + thread01.isAlive());
System.out.println("线程执行完时的isAlive======" + thread01.isAlive());
}
}
结果:
线程被new出来还未被调用时的isAlive======false
线程通过start被调用时的isAlive======true
线程执行完时的isAlive======true
线程run时的isAlive=====true
产生这种结果的原因是main线程和thread01这两个线程并发执行,main线程执行完了的时候,thread01 线程还未开始执行,所以其isAlive的值是true。
4、getID()方法
这个方法比较简单,返回的就是当前线程对应的tid(Thread ID),这个tid是通过全局唯一的线程ID生成器threadSeqNumber来维护的,每new出一个线程threadSeqNumber都会自增一次,并赋予线程的tid属性,这个是Thread自己做的,用户无法自己制定tid。
举例:
public class Thread01 extends Thread{
@Override
public void run() {
System.out.println("线程run时的isAlive=====" + this.isAlive());
}
}
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
Thread01 thread02 = new Thread01();
System.out.println(thread01.getId());
System.out.println(thread02.getId());
}
}
结果:
11
12
5、getName()方法
new一个线程的时候,可以指定线程的名字,也可以不指定。如果指定,线程的名字就是我们指定的名字,getName()返回的就是我们指定的线程的名字。如果不指定,getName()返回的是"Thread-" + threadInitNumber,其中threadInitNumber是一个int型全局唯一的线程初始号生成器,threadInitNumber通过自增来维护线程初始号,所以返回的就是Thread-0,Thread-1,Thread-2等等。
举例:
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
Thread01 thread02 = new Thread01();
System.out.println(thread01.getName());
System.out.println(thread02.getName());
}
}
结果:
Thread-0
Thread-1
6、getPriority()和setPriority(int newPriority)
这两个方法用于获取和设置线程的优先级,优先级高的线程获取CPU的资源比较多,会比较容易先执行(不是一定会先执行)。线程的优先级别为1-10,其中1优先级最低,10优先级最高,即优先级越高的线程越先执行。
举例:未设置优先级时
public class Thread01 extends Thread{
@Override
public void run() {
for(int i = 0; i < 100000; i++) {
}
System.out.println("※※※" + "的优先级为:" + this.getPriority());
}
}
public class Thread02 extends Thread{
@Override
public void run() {
for(int i = 0; i < 100000; i++) {
}
System.out.println("======" + "的优先级为:" + this.getPriority());
}
}
public class Test {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Thread01 thread01 = new Thread01();
Thread02 thread02 = new Thread02();
thread01.start();
thread02.start();
}
}
}
结果:
※※※的优先级为:5
======的优先级为:5
※※※的优先级为:5
======的优先级为:5
======的优先级为:5
※※※的优先级为:5
※※※的优先级为:5
======的优先级为:5
======的优先级为:5
※※※的优先级为:5
可以看到,未设置线程的优先级的时候,线程的优先级别默认是5,此时※与=代表的线程并没有明显的先后执行顺序,下面设置一下线程的优先级
public class Test {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Thread01 thread01 = new Thread01();
Thread02 thread02 = new Thread02();
thread01.setPriority(10);
thread02.setPriority(1);
thread01.start();
thread02.start();
}
}
}
结果:
※※※的优先级为:10
※※※的优先级为:10
※※※的优先级为:10
======的优先级为:1
======的优先级为:1
※※※的优先级为:10
※※※的优先级为:10
======的优先级为:1
======的优先级为:1
======的优先级为:1
可以看到,优先级别为10的线程会比较容易先执行,但并不是优先级高的线程执行完了在执行优先级低的线程,而是在该时间片内,优先级高的线程会容易先执行。
7、isDaemon()方法和setDaemon()方法
daemon:[ˈdi:mən] 守护的意思。讲解两个方法前,首先要知道理解一个概念。Java中有两种线程,一种是用户线程(user thread),一种是守护线程(daemon thread),守护线程是一种特殊的线程,它的作用是为其他线程的运行提供便利的服务,最典型的应用便是GC线程。如果进程中不存在非守护线程(即用户线程)了,那么守护线程自动销毁,因为没有存在的必要,为别人服务,结果服务的对象都没了,当然就销毁了。
举例:thread01不设置成守护线程
public class Thread01 extends Thread{
private int i = 1;
@Override
public void run() {
while(true){
try {
Thread.sleep(200);
System.out.println(i);
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
thread01.start();
System.out.println("thread01是否是守护线程==" + thread01.isDaemon());
System.out.println("main线程是否是守护线程==" + Thread.currentThread().isDaemon());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main主线程执行完了");
}
}
说明:此程序中存在两个线程,一个是main主线程(用户线程),一个是thread01(用户线程),两个线程并发执行,所以main线程执行完了之后并不影响thread01 线程的继续执行,因为thread01中的while一致为true,所以thread01会一直执行下去
结果:
thread01是否是守护线程==false
main线程是否是守护线程==false
1
2
3
4
main主线程执行完了
5
6
7
8
9
...
...
...
...
...
thread01设置成守护线程后
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
thread01.setDaemon(true);
thread01.start();
System.out.println("thread01是否是守护线程==" + thread01.isDaemon());
System.out.println("main线程是否是守护线程==" + Thread.currentThread().isDaemon());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main主线程执行完了");
}
}
结果:
thread01是否是守护线程==true
main线程是否是守护线程==false
1
2
3
4
main主线程执行完了
说明:将thread01线程设置成守护线程后,该程序中的两个线程(main线程和thread01线程)并发执行,但是当main线程执行完之后,作为守护线程的thread01自动销毁,不在继续执行。
关于守护线程,有一个细节注意下,setDaemon(true)必须在线程start()之前设置(This method must be invoked before the thread is started)。
8、interrupt()方法
这是一个有点误导性的名字,实际上Thread类的interrupt()方法无法中断线程。
举例:
public class Thread01 extends Thread{
@Override
public void run() {
for(int i = 0; i < 50000; i++) {
System.out.println("i = " + (i + 1));
}
}
}
public class Test {
public static void main(String[] args) {
Thread01 thread01 = new Thread01();
thread01.start();
thread01.interrupt();
}
}
结果:
.............
.............
i = 49997
i = 49998
i = 49999
i = 50000
看结果还是打印到了50000。也就是说,尽管调用了interrupt()方法,但是线程并没有停止。interrupt()方法的作用实际上是:在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞状态。换句话说,没有被阻塞的线程,调用interrupt()方法是不起作用的。关于这个会在之后讲中断机制的时候,专门写一篇文章讲解。
9、isInterrupted()方法
测试线程是否处于中断状态,但是不清除状态标识。(Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method)。这个和interrupt()方法一样,在后面讲中断机制的文章中专门会讲到。
10、join()方法
该方法暂时不写,后续会补上。
参考资料:
[Java] Thread的start()和run()函数区别
Java多线程3:Thread中的实例方法的更多相关文章
- java 多线程 2 Thread中start()和run()的区别
- Java多线程2:Thread中的实例方法
Thread类中的方法调用方式: 学习Thread类中的方法是学习多线程的第一步.在学习多线程之前特别提出一点,调用Thread中的方法的时候,在线程类中,有两种方式,一定要理解这两种方式的区别: 1 ...
- 2.Thread中的实例方法
(转自:http://www.cnblogs.com/xrq730/p/4851233.html) Thread类中的方法调用方式: 1.this.XXX 这种调用方式表示的线程是:线程实例本身 2. ...
- java多线程创建-Thread,Runnable,callable和threadpool
java创建多线程的方式有许多种,这里简要做个梳理 1. 继承Thread类 继承java.lang.Thread类,创建本地多线程的类,重载run()方法,调用Thread的方法启动线程.示例代码如 ...
- Java多线程01(Thread类、线程创建、线程池)
Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...
- JAVA多线程(一) Thread & Runnable
githut代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service/ ...
- Java 多线程(1)-Thread和Runnable
一提到Java多线程,首先想到的是Thread继承和Runnable的接口实现 Thread继承 public class MyThread extends Thread { public void ...
- Java 多线程之 Thread 类 和 Runnable 接口初步使用
目录 Thread 类 Thread之定义线程类 Thread之开启线程 Runnable 接口 Runnable 之定义线程类 Runnable 之开启线程 @ Thread 类 Thread 类是 ...
- Java 多线程查找文件中的内容
学过了操作系统,突然不知道多线程有什么用了. 看了一下百度,发现多线程,可以提升系统利用率 在系统进行IO操作的时候,CPU可以处理一些其他的东西,等IO读取到内存后,CPU再处理之前的操作. 总之可 ...
随机推荐
- 在Windows .NET平台下使用Memcached (Enyim使用)
1. 启动并配置Memcached的服务端 1. 下载Memcached http://download.csdn.net/download/ful1021/7969231 2. 解压到任意目录下, ...
- 深度学习框架PyTorch一书的学习-第四章-神经网络工具箱nn
参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 本章介绍的nn模块是构建与autogr ...
- direct path read temp的处理方法
Examine the SQL statement currently being run by the session experiencing waits to see what is causi ...
- oracle(环境搭建一)
图形化安装Oracle11gR2 环境: verify: CentOS release 6.5 (Final) release:CentOS release 6.7 (Final) 1.登录到root ...
- java 在实例化异常的时候做的事情
new Exception 这段代码,会调用父类ThrowAble的构建方法,在构造方法中会调用fillInstackTrace这个native方法. 会把当前线程异常发生的信息,上下文记录在栈调用指 ...
- BZOJ1063 NOI2008 道路设计 树形DP
题目传送门: BZOJ 题意精简版:给出一棵树,在一种方案中可以将树的若干链上的所有边的边权改为$0$,但需要保证任意两条链之间没有交点.问最少的一种方案,使得从根节点到其他节点经过的边的边权和的最大 ...
- 理解MySql的锁&事务隔离级别
这几篇文章是从网上(http://www.hollischuang.com)看到的一系列文章,也是重温了一下数据库的相关知识.下面是对这些文章的一些前后行文逻辑的说明: 我们知道,在DBMS的多个事业 ...
- MVC5+EF6 --自定义控制Action访问权限
本章主要讲解在MVC中灵活控制Action的访问权限: 本章所使用的示例表也是上一张所使用的TbUser.TbRole.TbUserRole: 最终的效果是针对任意一个Action或Controlle ...
- python 可调用对象之类实例
可调用对象,即任何可以通过函数操作符()来调用的对象. python可调用对象大致可以分为4类: 1.函数 python中有三种函数:内建函数(BIFs).用户自定义函数(UDF).lambda表达式 ...
- Python _内置函数3_45
reversed: #reversed() l = [1,2,3,4,5] l.reverse() print(l) #改变了原来的列表 l = [1,2,3,4,5] l2 = reversed(l ...