线程状态
线程停止
jak里面有停止线程方法,但是一般不使用。
通常使用标志位进行终止变量:即当flag = false,则终止线程运行。
package thread;
//测试stop
public class TestStop implements Runnable {
//设置一个标志位
boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("The Thread ....."+i++);
}
}
//设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i= 0;i < 1000;i++){
System.out.println("main"+i);
if (i == 900){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程停止了" );
}
}
}
}
线程休眠
模拟倒计时,打印当前时间
每个对象都有一个锁,sleep不会释放锁
package thread;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时和打印系统当前时间
public class TestSleep2 {
public static void main(String[] args) {
//倒计时
try {
tenDown();
} catch (Exception e) {
e.printStackTrace();
}
//打印打钱系统时间
Date starTime = new Date(System.currentTimeMillis());//获取当前系统时间
while (true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(starTime));
starTime = new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void tenDown() throws Exception {
int num =10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if (num < 0){
break;
}
}
}
}
线程礼让
package thread;
//测试礼让线程
//礼让不一定成功,看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()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
线程join
join合并线程,等待此线程执行完毕后,再执行其他线程,其他线程阻塞
- 强行插队
package thread;
//测试join方法
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程VIP来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动我们的线程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 500; i++) {
if (i == 200){
thread.join();//插队
}
System.out.println("main"+i);
}
}
}
###观察线程状态
package thread;
//观察测试线程的状态
public class TestState {
public static void main(String[] args) throws InterruptedException {
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);//Run
while (state != Thread.State.TERMINATED){//只要线程不终止就一直输出状态
Thread.sleep(100);
state = thread.getState();//更新线程状态
System.out.println(state);//输出状态
}
}
}
线程优先级
**PRIORITY **:优先级
-
java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级来决定调度那个线程
-
线程的优先级用数字来表示,范围:1—10
Thread.MIN_PRIORITY = 1; Thread.NORM_PRIORITY = 5; Thread.MAX_PRIORITY = 10;
-
获取或者改变优先级
getPriority().setPriority(int xxx)
package thread;
import java.sql.SQLOutput;
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.MAX_PRIORITY);//最高级10
t4.start();
t5.setPriority(8);
t5.start();
t6.setPriority(7);
t6.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}
守护线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程(mian())执行完毕
- 虚拟机不用等待守护线程(gcc())执行完毕
- 例如后台记录操作日志,监控内存,垃圾回收…
package thread;
//测试守护线程
//上帝守护你
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);//守护线程true 默认值时false是用户线程
thread.start();
new Thread(You).start();
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝保佑着你");
}
}
}
//你
class you implements Runnable{
@Override
public void run() {
for (int i = 0; i < 3650; i++) {
System.out.println("你一生都开心的活着");
}
System.out.println("-====goodbye! world!=====-");
}
}