首页 > 代码库 > java 多线程--- Thread Runnable Executors
java 多线程--- Thread Runnable Executors
java 实现多线程的整理:
- 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