目录
线程的五个状态:
创建状态:
开发者定义线程类,实例化后调用start()进入进入就绪状态。就绪状态:
等待CPU的调度。执行状态:
执行run()方法内容。阻塞状态:
在就绪状态因为不可执行(中断等)的一些原因进入阻塞状态或线程执行完就如阻塞状态后等待就绪。结束状态:
线程执行完后结束线程或进入阻塞状态后强制结束线程。线程常用方法:
获取活动的线程:
Thread.currentThread(); ```java class CurrentThread implements Runnable{ @Override public void run() { //Thread.currentThread()获取当前线程运行对象 //System.out.println(Thread.currentThread().getName()); //获取当前运行对象的线程名 } } public class Demo01 { public static void main(String[] args) { CurrentThread current = new CurrentThread(); new Thread(current).start(); new Thread(current).start(); } } 结果:Thread[Thread-0,5,main] Thread[Thread-1,5,main] ```获取活动的线程的名字:
Thread.currentThread().getName();class CurrentThread implements Runnable{
@Override
public void run() {
//Thread.currentThread()获取当前线程运行对象
System.out.println(Thread.currentThread().getName()); //获取当前运行对象的线程名
}
}
public class Demo01 {
public static void main(String[] args) {
CurrentThread current = new CurrentThread();
new Thread(current).start();
new Thread(current).start();
}
}
结果:Thread-0
Thread-1
改变线程的名字:
Thread.currentThread().setNname() ;线程休眠:
Thread.sleep();class SleepThread implements Runnable{
@Override
public void run() {
try {
//要强制try...catch(InterruptedException 异常)
Thread.sleep(1000); //Thread.sleep(毫秒) Thread.sleep(毫秒,纳秒)
System.out.println(Thread.currentThread().getName()+"运行!!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Demo01 {
public static void main(String[] args) {
SleepThread sleep = new SleepThread();
new Thread(sleep,"线程A").start(); //new Thread(Runnable子类,线程名)
sleep.run();
}
}
线程中断:
-
interrupt():线程终止
-
isInterrupted():判断线程是否终止
class InterruptedThread implements Runnable {
@Override
public void run() {
try {
Thread.sleep(10000);
System.out.println("线程运行无异常!!!");
} catch (InterruptedException e) {
System.out.println("线程中断!!!");
}
}
}
public class Demo01 {
public static void main(String[] args) throws InterruptedException {
InterruptedThread interrupted = new InterruptedThread();
Thread thread = new Thread(interrupted,"线程--0");
//thread.isInterrupted():表示判断线程的中断情况
System.out.println(thread.getName() + "线程中断情况:" +thread.isInterrupted()); //false
thread.start();
thread.sleep(2000);
thread.interrupt(); //线程中断
System.out.println(thread.getName() + "线程中断情况:" + thread.isInterrupted()); //true
结果:
线程--0线程中断情况:false
线程中断!!!
线程--0线程中断情况:true
强制执行:
1.public final void join()
2.public final void join(毫秒数)
3.public final void join(毫秒数,纳秒数)
public class Demo01 {
public static void main(String[] args) throws InterruptedException {
Thread mainThread = Thread.currentThread(); //获取主线程的对象
Thread thread = new Thread(()->{
for(int i=0;i<500;i++){
if(i == 20){
try {
mainThread.join(); //主线程强制执行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" 运行:"+i);
}
},"线程A");
thread.start();
for (int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"运行"+i);
}
}
}
强制礼让:
让出CPU资源,不和其他线程去抢,让其他线程先执行。
public class Demo01 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 50; i++) {
if (i == 2) {
System.out.println("礼让一次!");
}
System.out.println(Thread.currentThread().getName() + "运行:" + i);
}
}, "线程A");
thread.start();
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName() + "运行:" + i);
}
}
}
结果:
main运行:0
main运行:1
main运行:2
线程A运行:0
线程A运行:1
礼让一次!
main运行:3
线程A运行:2
线程等待:
- public final void wait():未唤醒一直等待。
- public final void wait(毫秒数):等待毫秒数过完,自动唤醒。
- public final void wait(毫秒数,纳秒数):等待时间过完,自动唤醒。
线程唤醒:
- public final void notify():唤醒第一个线程
- public final void notifyAll():唤醒所有线程,先执行优先度最高的
线程的优先级:
- public final void setPriority(int newPriority) 设置优先级
- public final int getPriority(int newPriority) 获取优先级
- public static final int MIN_PRIORITY 最低优先级(10)
- public static final int NORM_PRIORITY 中等优先级(5)
- public static final int MAX_PRIORITY 最高优先级(1)
- 线程优先级越高,CPU调度的可能性越大
- 主线程和用户自定义的线程优先度都为5
public class Demo01 {
public static void main(String[] args) {
Runnable run = () ->{
for (int i=0;i<20;i++){
System.out.println(Thread.currentThread().getName()+"运行:"+i);
}
};
Thread threadA = new Thread(run,"线程A");
Thread threadB = new Thread(run,"线程B");
Thread threadC = new Thread(run,"线程C");
threadA.setPriority(Thread.NORM_PRIORITY); //MORN_PRIORITY(5)
threadB.setPriority(Thread.MIN_PRIORITY); //MIN_PRIORITY(1)
threadC.setPriority(Thread.MAX_PRIORITY); //MAX_PRIORITY(10)
threadA.start();
threadB.start();
threadC.start();
}
}