java 实现多线程的整理:

  1.   Thread实现多线程的两种方式:

    (1)继承 Thread类,同时重载 run 方法:

class PrimeThread extends Thread {
long minPrime;
primeThread(long minPrime) {
this.minPrime = minPrime;
} public void run() {
// compute primes larger than minPrime }
} PrimeThread p = new PrimeThread(143);
p.start();

  Thread的源码:

public class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
......

  /* What will be run. */

   private Runnable target;

   

/**

* If this thread was constructed using a separate

* <code>Runnable</code> run object, then that

* <code>Runnable</code> object's <code>run</code> method is called;

* otherwise, this method does nothing and returns.

* <p>

* Subclasses of <code>Thread</code> should override this method.

*

* @see     #start()

* @see     #stop()

* @see     #Thread(ThreadGroup, Runnable, String)

*/

@Override

public void run() {

if (target != null) {

target.run();

}

}

}

  (2) 声明一个实现了Runnable接口的类。--- Thread 类其实也是实现了Runnable 接口的类 参见上面的源码  

package java.lang;
public interface Runnable {
public abstract void run();

  如果不需要实现Thread 类中的其他方法,可以仅仅实现Runnable接口中的Run()方法来实现多线程。

  "This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class." --- Arthur van Hoff

  --- 因为当程序员不准备修改或者增强这个类的功能的时候,就不应该成为这个类的子类。

  

class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime){
this.minPrime = minPrime;
} public void run() {
//compute primes larger than minPrime
....
}
}
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
 

2. Executors

 Executor 接口

  一个执行提交Runnable 任务的对象。对任务的提交与任务的执行,线程的使用,调度进行解耦。

  取代 new Thread(new(RunnableTask())).start()

  转而使用:

  Executor executor = anExecutor;

  executor.execute(new RunnableTask1());

  executor.execute(new RunnableTask2());

简单的用法:

 class DirectExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
} class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
new Thread(r).start();
}
}

一个复合的Executor:

class SerialExecutor implements Executor {
final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
final Executor executor;
Runnable active; SerialExecutor(Executor executor) {
this.executor = executor;
} public synchronized void execute(final Runnable r) {
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (active == null) {
scheduleNext();
}
} protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
executor.execute(active);
}
}
}

  interface ExecutorService

  接口继承Executor提供了方法来管理终止以及可以产生Future结果的同步或者异步任务。

public interface ExecutorService extends Executor {
  void shutdown();//停止说有的任务,不会再接收新的任务
  List<Runnable> shutdownNow();//停止全部活跃的任务,停止等待的任务,返回等待执行任务的列表。
  boolean isShutdown(); // 返回true 如果executor 已经被停止
boolean  isTerminated(); //如果全部的任务在shutDown后都完成了 返回为true.只有在shutdown()或者 shutdownNow() 被调用后才会返回true.
  boolean awaitTermination(long timeout, TimeUnit) throws InterruptedException; //阻塞直到所有的任务在shutdown()请求完成后,或者超时发生,或者现有的线程中断。
  <T>  Future<T>  submit(Callable<T> task); //提交一个值返回的任务运行,同时返回Future类,通过Future 的get()方法可以获得任务的执行结果。
  <T>  Future<T>  submit(Runnable task, T result);
  Future<?>  submit(Runnable task);//提交一个Runnable 任务执行,返回一个Future代表这个任务。
  <T>  List<Future<T>> invokeAll(Colleaciton<? extends Callable<T>> tasks) throws InterruptedException;//执行指定的多个任务,返回Future的list,包含他们的状态,以及运行完成的结果。
  <T>  List<Future<T>> invokeAll(Collectio<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException;//执行给定的任务,返回Future的列表,知道所有的任务都完成或者超时时间达到。
  <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;//执行给定的任务,返回任意一个完成的结果。
  <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;//执行给定的任务,直到任务在超时时间内完成
}

  class Executors

  支持运行异步任务,管理一个线程池,无需手动去创建新的线程。

public class executors {
public static void main(String[] args){
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
});
}
}

但是java进程没有结束,Executors必须显示的停止,可以调用上面ExecutorService中的方法来终止:shutdown()  会等待当前的任务运行完成,shutdownNow() 会终止所有的当前正在运行的任务并且立即关闭executor。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class executors {
public static void main(String[] args){
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
});
try {
System.out.println("attempt to shutdown executor");
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.err.println("tasks interrupted");
} finally {
if(!executor.isTerminated()) {
System.err.println("cancel non-finished tasks");
}
executor.shutdownNow();
System.out.println("shutdown finished");
}
}
}

  Executors 还支持另外一种类型的任务:Callable。Callable会返回一个值。  

import java.util.concurrent.*;

public class callable{
public static void main(String[] args) throws IllegalStateException,InterruptedException, ExecutionException{
Callable<Integer> task = () -> {
              try {
TimeUnit.SECONDS.sleep(1);
return 123;
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(task);
System.out.println("future done? " + future.isDone()); Integer res = future.get();
System.out.println("future done? " + future.isDone());
System.out.print("result : " + res);
executor.shutdownNow();
}
}

  Executors 可以通过invokeAll()一次批量提交多个callable任务。

import java.util.concurrent.*;
import java.util.*; public class moreCallable{
public static void main(String[] args) throws InterruptedException{
ExecutorService executor = Executors.newWorkStealingPool();
List<Callable<String>> callables = Arrays.asList(
() -> "task1",
() -> "task2",
() -> "task3");
executor.invokeAll(callables).stream().map(future -> {
try{
return future.get();
}catch (Exception e) {
throw new IllegalStateException(e);
}
}).forEach(System.out::println);
}
}

-----

Executors 是一个包含有很多static静态方法的类,使用时,可以作为一个工具类使用,

  Executors.newWorkStealingPool() 这个方法又拖出来一个类:ForkJoinPool(extends AbstractExecutorService (since 1.7))

后续继续写 Future,ForkJoinPool 以及线程的调度:ScheduledExecutorService。

参考:

http://www.open-open.com/lib/view/open1431307471966.html

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html

http://www.cnblogs.com/lucky_dai/p/5509261.html

java源码

 

java 多线程--- Thread Runnable Executors的更多相关文章

  1. [java多线程] - Thread&Runnable运用

    负载是一个很大的话题,也是一个非常重要的话题.不管是在大的互联网软件中,还是在一般的小型软件,都对负载有一定的要求,负载过高会导致服务器压力过大:负载过低又比较浪费服务器资源,而且当高请求的时候还可能 ...

  2. 探Java多线程Thread类和Runnable接口之间的联系

    首先复习一下Java多线程实现机制,Java实现多线程方法有如下这么几种: 1.继承了(extends)Thread类 2.实现了(implements)Runnable接口 也就是说  有如下两种情 ...

  3. JAVA多线程Thread VS Runnable详解

    要求 必备知识 本文要求基本了解JAVA编程知识. 开发环境 windows 7/EditPlus 演示地址 源文件   进程与线程 进程是程序在处理机中的一次运行.一个进程既包括其所要执行的指令,也 ...

  4. java多线程(三)-Executors实现的几种线程池以及Callable

    从java5开始,类库中引入了很多新的管理调度线程的API,最常用的就是Executor(执行器)框架.Executor帮助程序员管理Thread对象,简化了并发编程,它其实就是在 提供了一个中间层, ...

  5. java多线程开发,Executors、FutureTask、Callable

    java多线程如何应用呢,几乎学java的同学都知道Thread类和Runable接口.继承Thread类或者实现Runable接口,调用thread的start方法即可启动线程. 然后是线程池,就是 ...

  6. 第39天学习打卡(多线程 Thread Runnable 初始并发问题 Callable )

    多线程详解 01线程简介 Process与Thread 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程则是执行程序的一次执行过程,它是一个动态的概念.是系统资源分配的 ...

  7. Java多线程Thread

    转自:http://www.cnblogs.com/lwbqqyumidi/p/3804883.html Java总结篇系列:Java多线程(一)   多线程作为Java中很重要的一个知识点,在此还是 ...

  8. android 多线程Thread,Runnable,Handler,AsyncTask

    先看两个链接: 1.http://www.2cto.com/kf/201404/290494.html 2. 链接1: android 的多线程实际上就是java的多线程.android的UI线程又称 ...

  9. [Java多线程]-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

随机推荐

  1. Coursera Machine Learning : Regression 评估性能

    评估性能 评估损失 1.Training Error 首先要通过数据来训练模型,选取数据中的一部分作为训练数据. 损失函数可以使用绝对值误差或者平方误差等方法来计算,这里使用平方误差的方法,即: (y ...

  2. php 连接 mssql 常见的所有问题

    php连接mssql时 ntwdblib.dllPHP连接MSSQL配置和PHP代码演示 收藏 如果实现了PHP和MySQL链接了,PHP和MSSQL的链接其实很简单: 支持MSSQL的本地链接和远程 ...

  3. 腾讯OAuth授权联合登录

    /** * unionLoginCallbackPath */ @Value("${QQ_UNION_LOGIN_CALLBACK_PATH}") private String q ...

  4. Flask备注4(Structure)

    Flask备注4(Structure) package 通过Flask可以非常简单的通过一个module(一个py文件)创建一个简单的application.这种简单程序的文件结构如下: /youra ...

  5. XE3随笔15:使用 IXMLHTTPRequest 简单获取网页源代码

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  6. final

    final的变量的值不能被改变.(包括形参) final的方法不能被重写. final的类不能被继承.

  7. .NET entityframework for mysql ,datetime字段存储值时有误差

    昨天Tester发现数据有问题,大部分时间“datetime类型”都多了一秒,很少一部分数据的时间能完全对上(年月日时分秒),因为缺少关键日志,就各种排查,最后发现在调用Savechange方法前一刻 ...

  8. 20145318 GDB调试汇编堆栈分析

    20145318 GDB调试汇编堆栈分析 代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const sta ...

  9. jquery之右下角消息提示框

    messager.js (function (jQuery) { var window; var obj = new Object(); obj.version = '@1.0'; obj.title ...

  10. Gray Code

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...