线程事件通知 & 线程方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { B b = new B(); b.start(); Thread.sleep(5 * 1000); b.setLoop( false ); } } class B extends Thread { private int n; private boolean loop = true ; public void setLoop(boolean loop) { this .loop = loop; } @Override public void run() { while (loop) { System. out .println( "B " + n++); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
interrupt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { B b = new B(); b.setName( "uiop" ); b.setPriority(Thread.MIN_PRIORITY); b.start(); for ( int i = 0; i < 5; ++i) { Thread.sleep(1000); System. out .println( "main thread" ); } System. out .println(b.getName() + " b priority " + b.getPriority()); b.interrupt(); } } class B extends Thread { @Override public void run() { while ( true ) { for ( int i = 0; i < 100; ++i) { System. out .println(Thread.currentThread().getName() + " i = " + i); } try { System. out .println(Thread.currentThread().getName() + " sleeping" ); Thread.sleep(20 * 1000); } catch (InterruptedException e) { System. out .println(Thread.currentThread().getName() + " interrupted" ); } } } } |
join
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { B b = new B(); b.start(); for ( int i = 0; i < 8; ++i) { Thread.sleep(1000); System. out .println( "Main " +i); if (i == 3) { System. out .println(b.getClass().getName()+ " join" ); b. join (); System. out .println(b.getClass().getName()+ " join finish" ); } } } } class B extends Thread { @Override public void run() { for ( int i = 0; i < 7; ++i) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System. out .println( this .getClass().getName() + ' ' + i); } } } |
Thread.yield() 礼让cpu not comple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { B b = new B(); b.start(); for ( int i = 0; i < 8; ++i) { Thread.sleep(1000); System. out .println( "Main " +i); if (i == 3) { System. out .println(b.getClass().getName()+ " join" ); // b.join(); Thread.yield(); // not compel System. out .println(b.getClass().getName()+ " join finish" ); } } } } class B extends Thread { @Override public void run() { for ( int i = 0; i < 7; ++i) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System. out .println( this .getClass().getName() + ' ' + i); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { Thread t = new Thread( new B()); int c=0; do { System. out .println( "Main " + ++c); if (c == 3) { t.start(); t. join (); } Thread.sleep(500); } while (c != 6); } } class B implements Runnable{ private int c; @Override public void run() { do { System. out .println( this .getClass().getName() + ++c); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } while (c != 5); } } |
Daemon Thread
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { DaemonThread daemonThread = new DaemonThread(); daemonThread.setDaemon( true ); daemonThread.start(); int c=0; do { System. out .println(T. class + " " +c); Thread.sleep(500); } while (c++ != 4); } } class DaemonThread extends Thread{ @Override public void run() { for (; ; ) { System. out .println( this .getName()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
查看线程状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package ersatz.thread; public class T { public static void main(String[] args) throws InterruptedException { B b = new B(); System. out .println(b.getName() + " state: " + b.getState()); b.start(); while (b.getState() != Thread.State.TERMINATED) { System. out .println(b.getName() + " state: " + b.getState()); Thread.sleep(500); } System. out .println(b.getName() + " state: " + b.getState()); } } class B extends Thread { @Override public void run() { while ( true ) { for ( int i = 0; i < 10; ++i) { System. out .println( this .getClass().getName() + ' ' + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } break ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律