线程不安全的
解决:加synchronized
package com.imooc.java.escape.synchronized_; public class MainActive implements Runnable { private int value = 0; @Override public synchronized void run() { String name = Thread.currentThread().getName(); while (true) { if (value < 1000) { System.out.println(name + " start : " + value); value++; System.out.println(name + " done : " + value); } else { break; } } } }只有一个线程能获得执行权
package com.imooc.java.escape.threadpool; /** * <h1>读书的任务</h1> * */ public class Reading implements Runnable { private int count; private String name; public Reading(int count, String name) { this.count = count; this.name = name; } @Override public void run() { while (count > 0) { System.out.println(Thread.currentThread().getName() + " reading " + name); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } --count; } } } package com.imooc.java.escape.threadpool; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * <h1>简单使用线程池</h1> * */ public class EasyUseThreadPool { private static void useFixedThreadPool(int threadCount) { //固定线程的线程池 ExecutorService executor = Executors.newFixedThreadPool(threadCount); Runnable runnable01 = new Reading(3, "Java 编程思想"); Runnable runnable02 = new Reading(2, "Spring 实战"); Runnable runnable03 = new Reading(3, "SpringBoot 实战"); Runnable runnable04 = new Reading(1, "MySQL 权威指南"); Runnable runnable05 = new Reading(2, "SpringCloud 实战"); executor.execute(runnable01); executor.execute(runnable02); executor.execute(runnable03); executor.execute(runnable04); executor.execute(runnable05); executor.shutdown(); } /** * <h2>自定义线程池</h2> * */ private static void customThreadPool() { //1走默认的拒绝策略,会抛出异常 ThreadPoolExecutor custom1 = new ThreadPoolExecutor( 1, 1, 30, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(2) ); //2自定义类拒绝策略,不会抛出异常,put()有阻塞的作用, ThreadPoolExecutor custom2 = new ThreadPoolExecutor( 1, 1, 30, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(2), new CustomRejectHandler() ); for (int i = 0; i != 5; ++i) { custom2.execute(new Reading(3, "Java 编程思想")); } custom2.shutdown(); } //自定义类拒绝策略 private static class CustomRejectHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { executor.getQueue().put(r); } catch (InterruptedException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { // useFixedThreadPool(3); customThreadPool(); } }