【2013-11-27】《Java编程思想》读书笔记:线程的声明

📅 2026/7/14 18:55:38
【2013-11-27】《Java编程思想》读书笔记:线程的声明
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2013-11-27 标题《Java编程思想》读书笔记线程的声明分类编程 / java / think_in_java 标签java·think in java·多线程《Java编程思想》读书笔记线程的声明ThreadSample1.java整理 Java 多线程相关技术这里备份线程声明的三种常用方式继承Thread类实现Runnable接口再用Thread包装用Thread构造函数 匿名内部类实现GitHubhttps://github.com/cstriker1407/think_in_javaThreadSample1.javapublicclassThreadSample1{publicstaticvoidtest(){newSample1Thread().start();newThread(newSample1Runnable()).start();newThread(newRunnable(){Overridepublicvoidrun(){System.out.println(Thread.currentThread().getName());for(inti0;i10;i){try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println(this is from Runnable: i);}}},this is thread name).start();}}classSample1ThreadextendsThread{Overridepublicvoidrun(){/* * http://www.ibm.com/developerworks/cn/education/java/j-threads/section7.html *线程优先级 Thread API 让您可以将执行优先级与每个线程关联起来。但是这些优先级如何映射到底层操作系统调度程序取决于实现。在某些实现中多个 ― 甚至全部 ― 优先级可能被映射成相同的底层操作系统优先级。 在遇到诸如死锁、资源匮乏或其它意外的调度特征问题时许多人都想要调整线程优先级。但是通常这样只会把问题移到别的地方。大多数程序应该完全避免更改线程优先级。 */setPriority(Thread.MAX_PRIORITY-1);System.out.println(this.getName());for(inti0;i10;i){try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println(this is from sample1thread: i);}}}classSample1RunnableimplementsRunnable{publicvoidrun(){for(inti0;i10;i){try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println(this is from Sample1Runnable: i);}}}