线程礼让–yield
- 礼让线程,让当前正在执行的线程暂停,但不阻塞
- 将线程从运行状态转为就绪状态
- 让CPU重新调度,礼让不一定成功!看CPU心情
//世间安得双全法,不负如来不负卿
package com.lean.demo02;
//测试礼让线程
//礼让不一定成功,看CPU心情
public class TestYield {
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()+"--->end");
}
}
线程的强制执行–Join
- Join合并线程,待此线程执行完后,再执行其他线程,其他线程阻塞
- 可以想象成插队
//第一最好不相见,如此便可不相恋;第二最好不相知,如此便可不想思
package com.lean.demo02;
//测试Join方法,想象成插队
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("线程VIP来了");
}
}
public static void main(String[] args) throws InterruptedException{
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 1000; i++) {
if(i==200){
thread.join();//插队
}
System.out.println("main-->"+i);
}
}
}
线程状态观测—Thread.State
线程状态。线程可以处于以下状态之一:
- New :尚未启动的线程处于此状态
- RUNNABLE:再Java虚拟机中执行的线程处于此状态
- BLOCKED:被阻塞等待监视器锁定的线程处于此状态
- WAITING:正在等待另一个线程执行特定动作的线程处于此状态
- TIME_WAITING:正在等待另一个线程执行动作达到指定等待时间的线程处于此状态
- TERMINATED:已退出的线程处于此状态
package com.lean.demo02;
//观测线程的状态
public class TestState {
public static void main(String[] args) {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("/");
});
//观测状态
Thread.State state = thread.getState();
System.out.println(state);
//观测启动后
thread.start();
state = thread.getState();
System.out.println(state);
while (state!=Thread.State.TERMINATED){//只要线程不终止,一直刷新状态
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
state = thread.getState();
System.out.println(state);
}
//thread.start();//死亡之后的线程不能再次启动,线程只能启动一次
}
}
线程的优先级—Poriority
线程优先级高,并不一定会先执行,执行顺序由CPU觉得。就相当于优先级高的是100张彩票,优先级低的是10张彩票,但是不一定优先级高的会得奖,只不过是得奖的可能性高而已,换句话说就是分配资源多。
线程的优先级用数字表示,范围从1~10
-
Thread.MIN_PRIORITY = 1
-
Thread.MAX_PRIORITY = 10
-
Thread.NORM_PRIORITY = 1
改变或获取优先级的方式:
- getPriority()或setPriority()
优先级需要先设置再启动
package com.lean.demo03;
public class TestPriority {
public static void main(String[] args) {
//主线程的默认优先级
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
//优先级设置,再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.NORM_PRIORITY);
t4.start();
t5.setPriority(Thread.MAX_PRIORITY);
t5.start();
//优先级大于10或优先级小于1就会报错
// t6.setPriority(-1);
// t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
}
}
守护线程—daemon
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 如,后台记录操作日志,监控内存,垃圾回收等待
package com.lean.demo03;
//测试守护线程
//上帝保佑,人生不过三万天
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true);//默认为false,表示用户线程,正常的线程都是用户线程
thread.start();//上帝守护线程启动
new Thread(you).start();//你 用户线程启动
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("god bless you");
}
}
}
//你
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("开心的活着");
}
System.out.println("-----Goodbye,World-----");
}
}