Thread方法介绍
Thread方法介绍:
void start() | 启动一个新线程,start方法必须是子线程第一个调用的方法,新线程会调用Runnable接口的run方法,start方法不能重复调用 |
---|---|
void run() | run方法是子线程的执行体,子线程从进入run方法开始直至run方法执行接收,意味着子线程的任务执行接收,在主线程直接调用run方法是不能创建子线程,只是普通方法调用 |
static void yeild() | 让步或者暂停;Thread类的静态方法。让正在执行的线程停止或者让步,让给优先级较高的线程获取CPU的执行权,(不一定会让出CPU执行权,若等待的线程优先级较低的情况或者当前只有一个线程在执行时,那么当前的线程又会立即获取CPU的执行权) |
static native void sleep(long millis) throws InterruptedException | 1000毫秒 = 1秒;作用是让线程进行休眠,即让线程阻塞住,Thread类提供的静态方法,会抛出InterruptedException |
final synchronized void join(long millis) throws InterruptedException | 等待线程执行结束之后再执行,会抛出 InterruptedException异常,能够让程序按照一定的顺序进行执行, 假如在a线程中b线程进行b.join调用,a线程等待b线程执行结束后才能继续执行,控制多线程按照次序执行 |
void interrupt() | 中断操作,底层调用native wait()方法, native方法的作用是对特定标识位做修改, 主要作用于线程:运行中线程、阻塞线程(sleep、join); 运行中的线程:interrupt方法仅仅是对标志位做了修改,其他没有影响; 阻塞线程:interrupt方法对标识位做了修改,令阻塞中的线程感知到标志位做了修改,就会中断当前的阻塞状态,抛出InterruptedException异常 |
isInterrupted() | 判断是否发生了中断操作,返回为Boolean, true:表示发生中断 false:表示未发生中断 |
setDaemon(boolean on) | 设置守护线程 , true:设置为守护线程 false:用户线程 默认就是用户线程 |
isDaemon() | 判断当前线程是否为守护线程,返回Boolean类型 true:守护线程 false:非守护线程 |
setName()/构造函数传入线程名 | 设置线程名 |
setPriority(int newPriority) | 设置线程优先级 优先级具有一定的范围,优先级分为10级,优先级数字越大说明其优先级越高,优先级越高,说明会被优先调用的概率越大 MIN_PRIORITY = 1;最小优先级 NORM_PRIORITY = 5;默认优先级 MAX_PRIORITY = 10;最大优先级 |
getState() | 获取线程状态 |
isAlive() | 判断线程是否为活动线程(已启动但未死亡) |
suspend() | 挂起线程 |
resume() | 唤起线程 |
thread.yeild():thread线程 != 正在执行的线程;
- 例:
public class ThreadMethod0417 {
public static void main(String[] args) {
//匿名类实现线程创建
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start");
// try {
// Thread.sleep(5000);//线程睡眠5秒
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + " end");
}
},"线程一");
System.out.println(Thread.currentThread().getName() + " start");
thread1.start();//启动子线程
try {
thread1.join(5000);
//在main线程里调用thread1.join方法,
// main线程等待thread1执行结束后才继续执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread1.setDaemon(true);
thread1.isDaemon();
thread1.setPriority(10);
System.out.println(thread1.getPriority());
System.out.println(Thread.currentThread().getName() + " end");
}
}
//output:
// main start
//线程一 start
//线程一 end
// 5
//main end
- join()方法的应用:假设有A、B、C三个线程,按照C、B、A的顺序执行线程。
public class ThreadMethod0417 {
//join()方法,main方法中调用a.join()方法,在main方法中a线程执行完后再继续执行
public static void main(String[] args) {
//假设有A、B、C三个线程,按照C、B、A的顺序执行线程。
Thread C = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start");
System.out.println(Thread.currentThread().getName() + " end");
}
},"线程C");
Thread B = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start");
try {
C.join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " end");
}
},"线程B");
Thread A = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " start");
try {
B.join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " end");
}
},"线程A");
A.start();
B.start();
C.start();
}
}
//output:
线程A start
线程B start
线程C start
线程C end
线程B end
线程A end