Thread类
Thread类实现了Runnable接口,所以Thread对象也是可运行Runnable对象,同时Thread类也是线程类
Thread类定义
- Thread()//一般用于在Thread类中覆盖定义run方法,可以使用匿名内部类进行定义
- Thread(Runnable)//使用最多的情况,run方式是由Runnable参数对象提供
- Thread(String name) //自定义线程名称
- Thread(Runnable,String name)
由于Runnable接口属于函数式接口,所以一般使用简化写法
Thread t = new Thread(()->{
System.out.println(Thread.currentThread());
});
t.start();
常见方法
方法 | 说明 |
void start() | 使该线程开始执行,注意不是立即执行,不是一般方法调用;Java 虚拟机调用该线程的 run 方法 |
void run() | 线程的执行体 |
void setName(String) | 改变线程名称 |
void setPriority(int) | 更改线程的优先级,Java中线程的优先级可以分为1-10,默认为5 |
void setDaemon(boolean) | 设置守护线程,守护线程是一种用于提供服务的线程,一般线程体中使用的是死循环,会在所有非守护线程退出后自动关闭 |
void join()/(long millisec) | 等待该线程终止的时间最长为 millis 毫秒 |
void interrupt() | 中断线程,不是中断线程的执行,而是修改中断参数 |
boolean isAlive() | 测试线程是否处于活动状态,活动状态就是线程已经启动且尚未终止。线程处于正在运行或准备开始运行的状态,就认为线程是“存活”的 |
static void yield() | 暂停当前正在执行的线程对象,并执行其他线程 |
static void sleep(long millisec) | 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响 |
static Thread currentThread() | 返回对当前正在执行的线程对象的引用 |
java针对线程提供了10级优先,优先级越高则获取更多的运行机会
但是不同的操作系统不一定支持10级优先,所以设置优先级时可能会出现在java中的不同优先级映射到操作系统中相同的优先级上。在具体编程中如果需要使用优先级,建议把优先级的差距拉开
Thread类中定义了3个常量
public static final int MIN_PRIORITY = 1;最小优先级
public static final int NORM_PRIORITY = 5;默认优先级
public static final int MAX_PRIORITY = 10;最大优先级
package com.yan1;
public class Test4 {
public static void main(String[] args) {
Thread t1=new Thread(()->{//一般守护线程都会使用死循环,因为自动终止
while(true) {
System.out.println(Thread.currentThread());
}
});
t1.setDaemon(true);//设置守护线程应该在start方法之前
t1.start();
System.out.println("Main...end");
}
}
package com.yan1;
/*
* void join()/(long millisec)等待该线程终止的时间最长为millis毫秒
*
* while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
*/
public class Test5 {
static int res=0;
public static void main(String[] args) {
Thread[] th=new Thread[10];
for(int i=1;i<=10;i++) {
int begin=(i-1)*100+1;
int end=i*100;
th[i-1]=new Thread(()->{
// begin++;//匿名内部类中使用外部的临时变量(不是属性),要求临时变量必须final,final可写可不写
for(int k=begin;k<=end;k++) {
res+=k;
//可能休眠的时候,正好在+1之前停止,可能会把+1给忘了
// try {
// Thread.sleep(5);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
});
th[i-1].start();
}
for(Thread tmp:th) {
if(tmp!=null) {
try {
// tmp.join();//无限制等待,直到出现异常或者等待的对象tmp执行结束
tmp.join(0);//0表示无限制等待,等价于无参数
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(res);
}
}
package com.yan1;
/*
* void interrupt()中断线程,不是中断线程的执行,而是修改中断参数
*
* 具体实现依赖于调用interrupt方法后产生InterruptException实现的
*/
public class Test6 {
public static void main(String[] args) {
Thread t1=new Thread(()->{
while(true) {
System.out.println(Thread.currentThread());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
}
});
t1.start();
t1.interrupt();
System.out.println("Main......end");
}
}
package com.yan1;
/*
* boolean isAlive()测试线程是否处于活动状态,活动状态就是线程以及启动
* 且尚未终止。线程处于正在运行或准备开始运行的状态,就认为线程是"存活"的
*/
public class Test7 {
public static void main(String[] args) {
Thread t1=new Thread(()->{
System.out.println(Thread.currentThread());
});
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(t1.isAlive());
System.out.println(t1);//Thread[Thread-0,5,]线程组main不要了,已经执行完了
}
}