➤ Java多线程编程【一文全解】

线程有五大状态:


public class TestStop implements Runnable{//1.设置一个标识位private boolean flag = true;@Overridepublic void run() {int i = 0;while(flag){System.out.println("run...Thread "+i++);}}//2.设置一个公开的方法停止线程,转换标志位public void stop(){ //自定义的停止方法this.flag = false;}public static void main(String[] args) {//创建Runnable实现类对象,通过线程对象开启线程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("线程停止");}}}
}
1 秒 = 1000 毫秒
倒计时 10、9、8 … 2、1 :
public class TestSleep implements Runnable {@Overridepublic void run() {for (int i = 10; i > 0; i--) {System.out.println(i);try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}public static void main(String[] args) {TestSleep testSleep = new TestSleep();new Thread(testSleep).start();}
}
//测试礼让线程
//礼让不一定成功,看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{@Overridepublic void run(){System.out.println(Thread.currentThread().getName()+"线程开始执行");Thread.yield();System.out.println(Thread.currentThread().getName()+"线程停止执行");}
}
运行结果如下(多种):
//测试join方法 相当于“插队”
public class TestJoin implements Runnable{@Overridepublic void run() {for (int i = 0; i < 1000; i++) {System.out.println("线程 BOSS 来啦"+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);}}
} //如果子线程会穿插在主线程中,可以在run()中加个sleep()
/*main 0main 1...main 198main 199线程 BOSS 来啦0线程 BOSS 来啦1线程 BOSS 来啦2...线程 BOSS 来啦998线程 BOSS 来啦999main 200main 201...main 498main 499
*/

//观察线程状态
public class TestState {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(()->{ //Lambda表达式for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("-------------");});//观察状态Thread.State state = thread.getState();//观察启动后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);//输出状态}thread.start();}
}
/* 输出:RUNNABLETIMED_WAITINGTIMED_WAITING......TIMED_WAITING-------------TERMINATED*/
//测试线程的优先级
public class TestPriority {public static void main(String[] args ){//主线程默认优先级System.out.println(Thread.currentThread().getName()+"--->Priority "+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);//先设置优先级,再启动t1.start();t2.setPriority(1);t2.start();t3.setPriority(4);t3.start();t4.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10t4.start();}
}
class MyPriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"--->Priority "+Thread.currentThread().getPriority());}
}
/*
main--->Priority 5
Thread-0--->Priority 5
Thread-3--->Priority 10
Thread-2--->Priority 4
Thread-1--->Priority 1进程已结束,退出代码0*/
优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用,都是看CPU的调度
守护线程是程序运行的时候在后台提供一种通用服务的线程。
所有用户线程停止,进程才会停掉所有守护线程,退出程序。
//测试守护线程
//上帝守护你
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{@Overridepublic void run(){while(true){System.out.println("上帝保佑你");}}
}
//你
class You implements Runnable{@Overridepublic void run(){for (int i = 0; i < 36500; i++) {System.out.println("开心快乐的活着");}System.out.println("--> goodbye world <--");}
} //可以自己运行一下,很有意思