线程状态2
五大状态
停止线程
线程方法
线程停止
package thread;
//测试stop
/*建议线程正常停止————利用次数不建议是循环
* 建议使用标志位,设置一个标志位
* 必要使用stop或者destroy等过时或者jdk不建议使用的方法
*/
public class Demo5 implements Runnable{
boolean flag = true;//定义线程停止使用标志位
@Override
public void run() {
int i = 0;
while(flag){
System.out.println(i++);
}
}//设置一个停止线程的公开方法,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
Demo5 demo5 = new Demo5();
new Thread(demo5).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main + " + i);
if (i == 900) {
demo5.stop();//调用stop方法切换标志位,让线程停止
System.out.println("线程停止了!!!");
}
}
}
}
线程休眠
package thread;
//模拟网络延迟,放大问题的发生性
public class Demo6 implements Runnable{
private int StickNum = 10;
@Override
public void run() {
while (true){
if (this.StickNum <= 0) break;
else{
System.out.println(Thread.currentThread().getName() + this.StickNum--);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Demo6 demo6 = new Demo6();
new Thread(demo6,"ming").start();
new Thread(demo6,"hong").start();
new Thread(demo6,"huang").start();
}
}
package thread;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class Demo7 {
public static void main(String[] args) {
// try {
// tenDown();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
Date date = new Date(System.currentTimeMillis());//获取当前时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
date = new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void tenDown() throws InterruptedException {
int num = 10;
while (true){
Thread.sleep(1000);
if (num <= 0) {
break;
}else
System.out.println(num--);
}
}
}
线程礼让
礼让线程,让当前正在执行的线程暂停,但不阻塞,将线程从运行状态转为就绪状态,让CPU重新调度,礼让不一定成功!看CPU心情!
package thread;
//yield,测试礼让线程,礼让不一定成功
public class Demo8 {
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 Demo9 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程VIP来了!" + i);
if (i %30==0){
Thread.yield();//礼让没啥用
}
}
}
public static void main(String[] args) throws InterruptedException {
Demo9 demo9 = new Demo9();
Thread thread = new Thread(demo9);
thread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
if (i == 200){
thread.join();
}
}
}
}
观测线程状态
package thread;
public class Demo01 {
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);//NEW
//观察线程启动后
thread.start();//启动线程
state = thread.getState();
System.out.println(state);//Run
while (state != Thread.State.TERMINATED){//只要线程不终止,就一直输出
try {
Thread.sleep(100);//TIMED_WAITING
state = thread.getState();//更新线程状态
System.out.println(state);//打印线程状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}//TERMINATED
}//死亡之后的线程不能够再次启动
}
线程优先级
java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度那个线程来执行。
线程的优先级用数字表示,范围1-10,
Thread.MIN_PRIORITY = 1;
Thread.MAX_PRIORITY = 10;
Thread.NORM_PRIORITY = 5;
使用getPriority().setPriority(int xxx)来改变优先级或获取优先级
package thread;
public class TestPriority implements Runnable{
public static void main(String[] args) {
MyPriority myPriority = new MyPriority();
Thread thread1 = new Thread(myPriority);
thread1.setPriority(10);
TestPriority testPriority = new TestPriority();
Thread thread2 = new Thread(testPriority);
thread2.setPriority(Thread.MAX_PRIORITY);
thread2.start();
thread1.start();
System.out.println(Thread.currentThread().getName() + Thread.currentThread().getPriority());
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "thread1");
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + Thread.currentThread().getPriority());
}
}//在java中,优先级高的并不一定先执行,main方法会先执行,大部分优先级高的会先执行
守护线程
线程分为1用户线程和守护线程,虚拟机必须确保用户线程执行完毕,虚拟机不用等待守护线程执行完毕,例如:后台记录操作日志,监控内存,垃圾回收等待等。
package thread;
//守护线程
public class TestDaemon {
public static void main(String[] args) {
Person1 person1 = new Person1();
MyDaemon myDaemon = new MyDaemon();
Thread thread = new Thread(myDaemon);
thread.setDaemon(true);//设置守护线程,默认是false表示是用户线程,正常的线程都表现为用户线程
thread.start();//启动守护线程
new Thread(person1).start();//person,用户线程启动
}
}
class Person1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 3650; i++) {
System.out.println("开心学习每一天!");
}
System.out.println("=======goodbye!");
}
}
class MyDaemon implements Runnable{
@Override
public void run() {
while (true){
System.out.println("我的守护线程!");
}
}
}
标签:状态,thread,Thread,System,线程,new,public
From: https://www.cnblogs.com/1234sdg/p/17028464.html