对于java多线程的实现方法你都了解吗?下面的文章要给大家介绍的就是java的一些多线程实现方式,一起来了解一下吧。
1、线程池 Executors类
/** * * 线程池 * 跟数据库连接池类似 * 避免了线程的创建和销毁造成的额外开销 * * java.util.concurrent * * Executor 负责现成的使用和调度的根接口 * |--ExecutorService 线程池的主要接口 * |--ThreadPoolExecutor 线程池的实现类 * |--ScheduledExecutorService 接口,负责线程的调度 * |--ScheduledThreadPoolExecutor (extends ThreadPoolExecutor implements ScheduledExecutorService) * * * Executors工具类 * 提供了创建线程池的方法 * */ public class ThreadPool { public static void main(String[] args) { //使用Executors工具类中的方法创建线程池 ExecutorService pool = Executors.newFixedThreadPool(5); ThreadPoolDemo demo = new ThreadPoolDemo(); //为线程池中的线程分配任务,使用submit方法,传入的参数可以是Runnable的实现类,也可以是Callable的实现类 for (int i = 1; i <= 5; i++) { pool.submit(demo); } //关闭线程池 //shutdown : 以一种平和的方式关闭线程池,在关闭线程池之前,会等待线程池中的所有的任务都结束,不在接受新任务 //shutdownNow : 立即关闭线程池 pool.shutdown(); } } class ThreadPoolDemo implements Runnable { /**多线程的共享数据*/ private int i = 0; @Override public void run() { while (i <= 50) { System.out.println(Thread.currentThread() .getName() + "---" + i++); } } }
public class ThreadPool2 { public static void main(String args[]) { ExecutorService executorService = Executors.newFixedThreadPool(5); for (int i = 0; i < 5; i++) { Future < Integer > future = executorService.submit(new Callable < Integer > () { @Override public Integer call() throws Exception { int result = 0; for (int i = 0; i <= 10; i++) { result += i; } return result; } }); try { System.out.println(Thread.currentThread() .getName() + "--" + future.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } executorService.shutdown(); } }
2、实现Runnable接口,重写run方法
public class MyThread { public static void main(String ards[]) { Runnable implRunnable = new ImplRunnable(); for (int i = 0; i < 10; i++) { new Thread(implRunnable) .start(); } System.out.println(Thread.currentThread() .getName()); } } class ImplRunnable implements Runnable { private volatile int i = 0; @Override public void run() { System.out.println(Thread.currentThread() .getName() + "--" + i++); } }
3、继承Thread类,重写run方法
public class MyThread { public static void main(String ards[]) { for (int i = 0; i < 10; i++) { new ExtendsThread() .start(); } System.out.println(Thread.currentThread() .getName()); } } class ExtendsThread extends Thread { @Override public void run() { System.out.println(Thread.currentThread() .getName()); } }
4、实现Callable接口,重写call方法(有返回值)
public class MyThread { public static void main(String ards[]) throws InterruptedException, ExecutionException { for (int i = 0; i < 10; i++) { Callable < Integer > implCallable = new ImplCallable(); FutureTask < Integer > futureTask = new FutureTask < Integer > (implCallable); new Thread(futureTask) .start(); System.out.println(Thread.currentThread() .getName() + "----" + futureTask.get()); } System.out.println(Thread.currentThread() .getName()); } } class ImplCallable implements Callable < Integer > { @Override public Integer call() throws Exception { int result = 0; for (int i = 0; i < 10; i++) { result += i; } System.out.println(Thread.currentThread() .getName()); return result; } }
以上就是java多线程实现方式的介绍了,你都清楚了吧,更多java基础知识以及java编程常见问题,请继续关注奇Q工具网来进行了解吧。
推荐阅读: