关于JAVA多线程编程的一点笔记
2012-04-26 23:59
489 查看
1.Future类的使用:用于跟踪线程池中某一线程的执行状态
2.抽象类可以有构造方法,该构造方法可供子类的构造方法调用,但该抽象类不能直接实例化。
相关代码如下:
类RunnableDemoImpl2、RunnableDemoImpl3与类RunnableDemoImpl1类似,只是暂停时间Thread.sleep()不一样。
2.抽象类可以有构造方法,该构造方法可供子类的构造方法调用,但该抽象类不能直接实例化。
相关代码如下:
/** * 类功能描述: * Author:chenza * Date:2012-4-26 下午10:23:36 */ package test.tech.concurrent; public interface IRunnableDemo extends Runnable { public void execute() throws Exception; }
/** * 类功能描述: * Author:chenza * Date:2012-4-26 下午10:27:16 */ package test.tech.concurrent; public abstract class AbstractRunnableDemo implements IRunnableDemo { public int id; public AbstractRunnableDemo(){ } public AbstractRunnableDemo(int id){ this.id = id; } @Override public void run(){ System.out.println(Thread.currentThread().getName() + " start ..."); try { this.execute(); } catch (Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " end!!!"); } @Override public abstract void execute() throws Exception; }
/** * 类功能描述: * Author:chenza * Date:2012-4-26 下午10:30:32 */ package test.tech.concurrent; public class RunnableDemoImpl1 extends AbstractRunnableDemo { public RunnableDemoImpl1(){ super(110); } public RunnableDemoImpl1(int id){ super(id); } @Override public void execute() throws Exception{ Thread.sleep(10000); for(int i = 0;i < 5;i++){ System.out.println(Thread.currentThread().getName() + ":" + id + " === " + this.getClass().getName() + " [" + i + "]!"); } } }
类RunnableDemoImpl2、RunnableDemoImpl3与类RunnableDemoImpl1类似,只是暂停时间Thread.sleep()不一样。
/** * 类功能描述: * Author:chenza * Date:2012-4-26 下午10:36:59 */ package test.tech.concurrent; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ThreadPoolDemo implements Runnable { private ExecutorService pool = Executors.newFixedThreadPool(5,Executors.defaultThreadFactory()); @Override public void run() { System.out.println(Thread.currentThread().getName() + " start ..."); IRunnableDemo rd = null; List<Future<?>> futures = new ArrayList<Future<?>>(); for(int i = 0;i < 10;i++){ if(i % 3 == 0){ rd = new RunnableDemoImpl1(111); }else if(i % 3 == 1){ rd = new RunnableDemoImpl2(222); }else if(i % 3 == 2){ rd = new RunnableDemoImpl3(333); } Future<?> future = pool.submit(rd); futures.add(future); } try { Thread.sleep(18000); } catch (InterruptedException e) { e.printStackTrace(); } for(Future<?> future : futures){ System.out.println("Done or not : " + future.isDone()); } System.out.println("start to shutdown thread pool ..."); pool.shutdown();//将会等线程池中的所有线程执行完后才关闭。 //pool.shutdownNow(); // 马上关闭,注意二者的区别 System.out.println("shutdown thread pool successful!!!"); System.out.println(Thread.currentThread().getName() + " end!!!"); } /** * @param args */ public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + " start ..."); new Thread(new ThreadPoolDemo()).start(); //ThreadPoolDemo demo = new ThreadPoolDemo(); //demo.run(); System.out.println(Thread.currentThread().getName() + " end!!!"); } }
相关文章推荐
- (笔记)关于LM3S片内FLASH编程的一点建议
- (笔记)关于binder机制的一点理解
- 关于DrillThrough的一点笔记
- 关于linux进程编程的一些笔记整理
- java多线程编程核心技术笔记-死锁
- 《Java多线程编程核心技术》 笔记 未完待续
- 【学习笔记】一个关于在时间轴上编程的问题
- 并发编程学习笔记 三------- 关于在if中和while中使用wait(),唤醒之后的区别
- 关于同步异步阻塞非阻塞一点笔记
- 关于RBM的一点笔记
- 关于数模中编程的一点愚见
- unix 环境编程学习笔记------对于信号的一点认识
- 第四章 控制执行流程的一些笔记以及关于char的一点东西
- 【红宝书笔记】一点关于JS的垃圾回收机制
- 关于java多线程网络编程的注意事项!
- 关于学习编程(struts)的一点小感想
- 计算机学习笔记2:关于微程序的一点己见
- 关于DLL的一点笔记
- [笔记].关于使用JLINK的三线SWD模式调试NUC1xx的一点粗浅认识
- 编程笔记(objective-c :关于属性)