java多线程详解三多线程5种实现方式
2017-02-19 09:53
288 查看
java种多线程实现方式主要有5种,继承Thread类,实现Runnable接口、实现Callable和 FutureTask包装器来创建Thread线程、使用ExecutorService、Callable、Future实现有返回结果的多线程,使用线程池来实现多线程。
方式一:继承Thread类
方式二:实现Runnable接口
方式四:使用Future和Callable接口
方式五:使用线程池创建线程
方式一:继承Thread类
public class ThreadTest { public static void main(String[] args){ for(int i = 0;i<100;i++){ MyThread my = new MyThread(); my.start(); } } } class MyThread extends Thread{ public void run(){ System.out.println(Thread.currentThread().getId()); } }
方式二:实现Runnable接口
public class ThreadTest { public static void main(String[] args){ for(int i = 0;i<100;i++){ Thread my = new Thread(new MyThread()); my.start(); } } } class MyThread implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getId()); } }方式三:使用FutureTask 和Callable接口的到返回值
Callable<String> callable = new Callable&l 4000 t;String>(){ @Override public String call() throws Exception { // TODO Auto-generated method stub return new Random().nextInt(1000)+""; } }; FutureTask<String> future = new FutureTask<String>(callable); new Thread(future).start(); try { Thread.sleep(1000); System.out.println(future.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); }
方式四:使用Future和Callable接口
ExecutorService threadPool = Executors.newCachedThreadPool(); CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool); for(int i = 1; i < 5; i++) { final int taskID = i; cs.submit(new Callable<Integer>() { public Integer call() throws Exception { return taskID; } }); } // 可能做一些事情 for(int i = 1; i < 5; i++) { try { System.out.println(cs.take().get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }
方式五:使用线程池创建线程
@Test public void testSingleThreadPool() { // 创建单个线程 ExecutorService singleThreadPool = Executors.newSingleThreadExecutor(); singleThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getId()); } }); } @Test public void testCachedThreadPool() { // 创建缓冲线程池 ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) cachedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getId()); } }); } @Test public void testFixedThreadPool(){ //创建5个线程的固定线程池 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5); for(int i = 0;i<5;i++ ) fixedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getId()); } }); } @Test public void testThreadPool(){ ThreadPoolExecutor pool = new ThreadPoolExecutor(5,8,1,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(100)); //这种式JDK实现线程池的方式 for(int i=0;i<100;i++) pool.execute(new Runnable(){ @Override public void run() { System.out.println(Thread.currentThread().getId()); } }); }
相关文章推荐
- java多线程详解二互斥同步方式实现多线程
- 详解多线程的两种实现方式以及4个线程,两个线程对j加1,两个线程对j减1完整代码。
- JAVA 实现线程的两种方式,举例,详解
- java多线程的6种实现方式详解
- 购物车3种实现方式 详解
- Java中线程实现的两种方式例程
- Java线程间通信-回调的实现方式
- Java线程退出实现方式
- Quartz源码分析(一)------ 以线程等待的方式实现按时间调度
- Java第七课 Java的多线程程序进程和线程的概念,实现多线程的两种方式,线程同步的原理,线程的死锁,运用wait和notify来实现producer - consumer关系,线程终止的两种情况。
- Java【多线程知识总结(5)】比较继承Thread类创建线程和实现Runnable接口创建线程这两种方式
- java多线程总结一:线程的两种创建方式及优劣比较
- JAVA线程的应用实例(运用2种中断线程方式,基于实现进度条为例)
- 远程线程嵌入方式 实现DLL的隐藏
- JAVA线程的应用实例(运用2种中断线程方式,基于实现进度条为例)
- 线程(俩种实现方式)
- js实现页面跳转的5种方式
- SWT中定时器的一种特殊实现方式/SWT中线程互访时display.asyncExec/display.syncExec...程序死掉无响应的解决办法
- SWT中定时器的一种特殊实现方式/SWT中线程互访时display.asyncExec/display.syncExec...程序死掉无响应的解决办法
- JavaScript 工作线程实现方式