Java Thread小结
2012-11-29 13:31
197 查看
1. 线程进程概念 多线程: 单个程序内部好象在同一时刻运行多种运算. 多进程: 同一时间内好象有多个程序运行. 2. 线程状态 2.1 创建状态 2.2 可运行状态 2.3 不可运行状态 2.4 死亡状态 2.1 创建状态(new thread) Thread TestThread = new TestThread(); 没有被启动,仅仅是空的线程对象,系统不为它分配资源. 可调用方法:start() stop() 2.2 可运行状态(runnable) TestThread.start(); 该方法产生了运行这个线程所需的系统资源,安排其运行 并调用线程体run(),使程序处于可运行状态. 注意:并不一定在运行状态,也许线程还没真正执行. 2.3 不可运行状态(not runnable)及其返回 (1)调用了sleep()>时间过去 (2)调用了suspend()>resume() (3)调用了wait()>该条件变量所在对象notify()/notifyAll() (4)输入输出流中发生线程阻塞->特定的I/O指令 2.4 死亡状态(dead) 自然撤消: run()方法中正常退出 主动停止: stop()方法停止当前线程 3. 线程体构造 3.1继承构造 3.2接口构造 3.1 继承构造 class TestThread extends Thread { public TestThread(String str){ super(str); } public void run(){ ........ } } class Caller() { public static void main(String[] args) { new TestThread("May 1st").start(); } } 3.2 接口构造 class TestThread implements Runnable { public void run(){ ........ } } class Caller(){ public static void main(String[] args){ TestThread test = new TestThread(); new Thread(test).start(); } } 注意:实现Runnable仅仅意味着有一个run()方法,不具有天生的线程能力. 4. 其它 4.1 构造方法选择 当一个线程已继承了另一个类时,用Runnable()接口 4.2 实用方法 isAlive():线程已被启动并且未被终止,返回true. 当应用程序需要经常处理大量短周期线程时,采用线程池技术是一个很好的解决方案。以下是小的测试程序 package org.net9.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class MainApp{ public static void main(String[] args) { MainApp test = new MainApp(1000); test.start(); test.close(); } private int size = 0; ExecutorService pool = null; public MainTask(int size) { this.size = size; } public void start() { long start = System.currentTimeMillis(); try { // create cache thread pool pool = Executors.newCachedThreadPool(); for (int i = 0; i < size; i ) { Thread.sleep(50); // thread usage // new Thread(new SubTask()).start(); // thread-pool usage pool.execute(new SubTask()); } } catch (InterruptedException ie) { ie.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("test cost " (end - start) " ms!"); } public void close() { // disable new tasks from being submitted pool.shutdown(); try { // wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { // cancel currently executing tasks pool.shutdownNow(); // wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { // thread pool cannot terminate System.err.println("thread pool cannot terminate"); } } } catch (InterruptedException ie) { // re-cancel if current thread also interrupted pool.shutdownNow(); // preserve interrupt status Thread.currentThread().interrupt(); } } } package org.net9.thread; public class SubThread implements Runnable { public void run() { try { Thread.sleep(500); } catch (InterruptedException ie) { ie.printStackTrace(); } } } |
相关文章推荐
- java.lang.Thread 和 ja…
- java 用组件绘制程序界面方法小结
- java io(输入输出)小结
- eclipse(java) 使用SQL …
- Java网络编程(1) Socket缓…
- Eclipse&nbsp;Java自动代码自动补齐窍门
- Android错误:项目导入时,出现Exception in thread "main" java.lang.NoClassDef、Could not write file 。。。.classpath
- 【Java 易混点】 关于eq…
- java发送https post请求实例
- Java GUI 使用 Vector 类 建立表格
- Exception in thread "png-cruncher_2" java.lang.RuntimeException: Timed out while waiting for slave a
- 【Java多线程学习】Java多线程的两种实现方式:继承Thread类 & 实现Runable接口
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Synchronization
- Java&nbsp;单例模式详解
- Java多线程基础使用(Thread|Runable|TimerTask&Timer)
- java 逻辑运算符的应用
- java 最新力作
- java&nbsp;泛型
- Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
- JAVA 存储空间 寄存器 堆栈 堆…