对于java多线程你了解多少呢?下面要讲到的就是java多线程实现方面的内容,那么你知道java实现多线程方式具体有哪几种吗?一起通过下文来了解吧。
1、实现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++); } }
2、线程池
/** * * 线程池 * 跟数据库连接池类似 * 避免了线程的创建和销毁造成的额外开销 * * 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(); } }
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架构师栏目来进行了解呢。
推荐阅读: