Thread 是什么?
thread 线程 是什么? 在百度百科上表述 线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。
线程就是对于进程中单独运行的子任务、每一个程序的启动相当于一个进程。比如说:启动qq ,在里面可以下载文件的同时进行聊天、这就是多线程程。
Java Thread 状态图
- 对于一个新的线程最开始就NEW 的状态
- 调用start()方法 变为READY RUNNABLE 准备 、 就绪状态
- RUNNABLLE 加锁lock\synchronized 变为BLOCKED 阻塞状态
- RUNNABLE 调用 wait()、join()、LockSupport.parkUntil() 方法 成为 WAITING 等待状态
- RUNNABLE 调用sleep(long time); wait(long timeout); join(long timeout); LockSupport.partUntil() 等方法 TIME_WAITING 线程等待时间
- 执行完毕TERMINATED
Thread 方法解析
- currentThread ();
currentThread() 方法可以返回代码段正在被哪个线程调用的信息。
public class ThreadCurrent(){
@Test
public void TestThread(){
Thread thread = Thread.currentThread();//这个方法是本地的静态方法,所以只能通过全局调用。
System.out.println(" thread Name"+thread.getName());//获得的是主线程
System.out.println(" thread Id"+thread.getId());//获取线程id
}
}
- isAlive
判断当前线程是否处于活动状态。(线程启动是否终止、线程处于正在运行或者准备开始运行都属于存活)。
/**
* 新建一个线程
**/
public class ThreadTest extends Thread
{
@Override
public void run(){
System.out.println("run"+this.isAlive());//输出false
System.out.print("输出线程未调用start 时候的状态");
}
}
//导入测试包
import org.junit.Test;
/**
* 调用test
**/
public class Test1(){
@Test
public void sleepTest(){
Thread thread = new Thread(new ThreadTest());
thread.start();
try{
thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(“sleep:”+ thread.isAlive()); // 输出fales
}
}
@Test
public void startTest(){
Thread thread = new Thread(new ThreadTest());
thread.start();
System.out.println("start:"+thread.isAlive());//输出ture
}
-
sleep()
方法sleep() 的作用是指定的毫秒数内让当前正在执行的线程休眠(暂停执行)、如果有锁会释放锁。 -
getId()
获取当前线程的唯一标识。 -
interrupt() | stop() |
stop() 是强行终止线程,但是不推荐使用。使用的时候不知道线程执行到哪一步了,会产生不可预料的结果。
interrupt() 线程中断,该方法仅仅是在线程中打一个停止的标记,不属于真正的停止线程。
- interrupted() | isInterrupted()
interrupted() 测试当前线程是否已经中断。线程状态由该方法清除。只能用于当前线程不能与实例化线程。
//导入测试包
import org.junit.Test;
/**
* 调用test
**/
public class Test1(){
@Test
public void startTest(){
Thread thread = new Thread(new ThreadTest());
thread.start();
thread.interruput();
Thread.currentThread().interrupt()
Sysout.out.println("是否中止当前线程:"+Thread.currentThread().interrupted());//true
Sysout.out.println("是否中止当前线程:"+Thread.currentThread().interrupted());//false 第二次调用为false 第一次调用时清除了状态
System.out.println("是否中止:"+thread.isInterrupted());//true
System.out.println("是否中止:"+thread.interrupted());//false 对于实例化无效
System.out.println("start:"+thread.isAlive());//输出ture
}
}
public class ThreadTest extends Thread
{
private volatile int count = 0;
pirvate boolean flg =true;
@Override
public void run(){
while(flg){
if(this.interrupted()){
System.out.println("run :"+this.isAlive());
System.out.print("输出线程未调用start 时候的状态");
}
count += 1;
if(count == 1000){
flg = false;
}
}
}
}
- yield()
yield () 方法是放弃当前CPU 的资源,将让给其他的任务去占用CPU 执行时间。但是放弃的时间不确定,有可能还是刚刚放弃,又获得CPU 时间片。(就好比跑步,本来你已经跑出去了但是要重新开始,就需要重新回到起点,但有可能你又是第一个跑出去的)。
上面只是讲了一部分的Thread 方法 、还有一部分的方法未标明。还有wait() 这个方法是Object 里面的方法 。
线程的优先级
在操作系统中线程可以划分优先级,优先级较高的线程可以得到CPU 资源较多,也就是CPU 优先执行较高的线程对象中的任务。
- setPriority() 方法
该方法就是设置线程的优先级。 JDK 设置了3个常量来定义优先级的值
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
这个是线程的设置的源码
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();//当前线程运行的权限
// 如果新的优先级 大于最大的常量 或者 小于最小的常量 就抛出异常
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
// 线程组不能为null 每一个线程都归于一个线程组
if((g = getThreadGroup()) != null) {
// 设置为最大的线程优先级
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
// 调用的hostp 代码 让新的级别设置为旧的级别
setPriority0(priority = newPriority);
}
}
- 优先级具有继承特性
比如说 A 线程启动 B 线程,则 B线程的优先级和 A是一样的。 - 优先级具有规则性
- 优先级具有随机性