💛让一个线程由运行状态--->就绪状态, 和其他就绪状态的线程同时等待CPU的调度.

💛线程礼让不一定会成功, 全看CPU的调度.

package com.smile.test.thread;

public class TestThreadYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"A").start();
        new Thread(myYield,"B").start();
    }

}
class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "  start");
        Thread.yield();
        System.out.println(Thread.currentThread().getName() + "  stop");
    }
}
/*
三种运行结果
1.礼让成功
A  start
B  start
B  stop
A  stop

2.礼让不成功
A  start
A  stop
B  start
B  stop

3.礼让成功
A  start
B  start
A  stop
B  stop

Process finished with exit code 0
 */

Java 线程插队--join

💛join()可以合并线程,待此线程执行完毕后,再执行其他线程,其他线程阻塞.

package com.smile.test.thread;

public class TestThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        MyJoin myJoin = new MyJoin();
        Thread vip = new Thread(myJoin, "VIP");
        vip.start();
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "start");
            if (i == 5) {
                vip.join();
            }
        }


    }
}
class MyJoin implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "start");
        }
    }
}
/*
mainstart
mainstart
mainstart
mainstart
mainstart
mainstart
VIPstart
VIPstart
VIPstart
VIPstart
...
*/

Java 线程的状态

  • 获取多线程的状态:
Thread.State state = 线程对象.getState();
NEW(新建状态)
RUNNABLE[运行状态:包括RUNNING(运行中) 和 READY(就绪)]
BLOCKED[阻塞状态]
WAITING[等待状态]
TIMED_WAITING[超时等待]
TERMINATED[终止]

Java 线程优先级

🌼Java 提供了一个线程调度器来监控就绪状态的所有线程, 并按照线程的优先级来决定调用哪一个线程执行.

🌼优先级的设定要在调用start()之前.

🌼优先级低只是意味着获得调度的概率低.​

package com.smile.test.thread;

public class TestThreadPriority implements Runnable{
    public static void main(String[] args) {
        TestThreadPriority testThreadPriority = new TestThreadPriority();
        Thread a = new Thread(testThreadPriority, "A");
        Thread b = new Thread(testThreadPriority, "B");
        Thread c = new Thread(testThreadPriority, "C");
        Thread d = new Thread(testThreadPriority, "D");
        a.setPriority(1);
        b.setPriority(4);
        c.setPriority(6);
        d.setPriority(10);
        System.out.println(Thread.currentThread().getName() + "[优先级: " + Thread.currentThread().getPriority() + "]");
        a.start();
        b.start();
        c.start();
        d.start();
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "[优先级: " + Thread.currentThread().getPriority() + "]");
    }
}
/*
main[优先级: 5]
D[优先级: 10]
C[优先级: 6]
B[优先级: 4]
A[优先级: 1]

Process finished with exit code 0
*/

Java 守护线程

🌼线程分为用户线程, 守护线程.

🌼虚拟机必须等待用户线程执行完毕.

🌼虚拟机不用等待守护线程执行完毕.

🌼守护线程可以用来记录日志,​ 垃圾回收等.

package com.smile.test.thread;

public class TestThreadDeamon {
    public static void main(String[] args) {
        Thread you = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("you are alive");
            }
            System.out.println("you are died");
        });
        Thread god = new Thread(()->{
            while(true){
                System.out.println("God is Protecting you");
            }
        });
        god.setDaemon(true);
        god.start();
        you.start();
    }
}
/*
God is Protecting you
you are alive
.
.
.
you are alive
you are alive
you are alive
you are died
God is Protecting you
God is Protecting you
Process finished with exit code 0
省略号都是you are alive.
*/