- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- public class ThreadPoolExecuteTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomething() {
- for (int i = 0; i < 10; i++) {
- executorService.execute(new Runnable() {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName());
- }
- });
- }
- System.out.println("over");
- }
-
- public static void main(String[] args) {
- new ThreadPoolExecuteTemplate().doSomething();
- }
- }
一般主线程不会等线程并发执行完,所以需要用到其他工具
ThreadTask.java
- package codeTemplate.thread;
-
- import java.util.Map;
- import java.util.concurrent.CountDownLatch;
-
- public class ThreadTask implements Runnable {
- private final String param;
- private final CountDownLatch latch;
- private Map
result; -
- public ThreadTask(Map
result, String param, CountDownLatch latch) { - this.result = result;
- this.param = param;
- this.latch = latch;
- }
-
- @Override
- public void run() {
- try {
- System.out.println(Thread.currentThread().getName());
- result.put(param, param + "over");
- } finally {
- latch.countDown();
- }
- }
- }
使用
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- public class CountDownLatchTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomethingNeedResult() {
- try {
- Map
result = new HashMap<>(); - List
params = Arrays.asList("1", "2", "3", "4", "5"); - CountDownLatch latch = new CountDownLatch(params.size());
- for (String param : params) {
- executorService.execute(new ThreadTask(result, param, latch));
- }
- latch.await(10, TimeUnit.MINUTES);
- System.out.println(result);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- new CountDownLatchTemplate().doSomethingNeedResult();
- }
- }
ThreadResultToListTask.java
- package codeTemplate.thread;
-
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.TimeUnit;
-
- public class ThreadResultToListTask implements Runnable {
- private final String param;
- private final CountDownLatch latch;
- private List
result; -
- public ThreadResultToListTask(List
result, String param, CountDownLatch latch) { - this.result = result;
- this.param = param;
- this.latch = latch;
- }
-
- @Override
- public void run() {
- try {
- int randomNumber = new Random().nextInt(3);
- System.out.println(Thread.currentThread().getName()+"------------"+randomNumber);
- TimeUnit.SECONDS.sleep(randomNumber);
- System.out.println(Thread.currentThread().getName()+"------------"+randomNumber);
- result.add(param + "over");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- latch.countDown();
- }
- }
- }
使用
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.*;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- public class CountDownLatchTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomethingNeedListResult() {
- try {
- List
result = new ArrayList<>(); - List
params = Arrays.asList("1", "2", "3", "4", "5"); - CountDownLatch latch = new CountDownLatch(params.size());
- for (String param : params) {
- executorService.execute(new ThreadResultToListTask(result, param, latch));
- }
- latch.await(10, TimeUnit.MINUTES);
- System.out.println(result);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- new CountDownLatchTemplate().doSomethingNeedListResult();
- }
- }
ThreadResultHoldOrderTask.java
- package codeTemplate.thread;
-
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.TimeUnit;
-
- public class ThreadResultHoldOrderTask implements Runnable{
- private final List
params; - private final CountDownLatch latch;
- private final int index;
-
- public ThreadResultHoldOrderTask(List
params,int index, CountDownLatch latch) { - this.params = params;
- this.latch = latch;
- this.index=index;
- }
-
- @Override
- public void run() {
- try {
- int randomNumber = new Random().nextInt(3);
- System.out.println(Thread.currentThread().getName()+"------------"+randomNumber);
- TimeUnit.SECONDS.sleep(randomNumber);
- System.out.println(Thread.currentThread().getName()+"------------"+randomNumber);
- params.set(index,params.get(index)+"over");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- latch.countDown();
- }
- }
- }
使用
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.*;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
-
- public class CountDownLatchTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomethingNeedOrderListResult() {
- try {
- List
params = Arrays.asList("1", "2", "3", "4", "5"); - CountDownLatch latch = new CountDownLatch(params.size());
- for (int i = 0; i < params.size(); i++) {
- executorService.execute(new ThreadResultHoldOrderTask(params, i, latch));
- }
- latch.await(10, TimeUnit.MINUTES);
- System.out.println(params);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- new CountDownLatchTemplate().doSomethingNeedOrderListResult();
- }
-
- }
SemaphoreTask.java
- package codeTemplate.thread;
-
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
-
- public class SemaphoreTask implements Runnable {
- private final Semaphore semaphore;
- private AtomicInteger resource;
-
- public SemaphoreTask(Semaphore semaphore, AtomicInteger resource) {
- this.semaphore = semaphore;
- this.resource = resource;
- }
-
- @Override
- public void run() {
- try {
- semaphore.acquire();
- TimeUnit.SECONDS.sleep(3);
- System.out.println(Thread.currentThread().getName() + "-------------" + resource.incrementAndGet());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- semaphore.release();
- }
-
- }
- }
使用
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
-
- public class SemaphoreTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomething() {
- Semaphore semaphore = new Semaphore(3);
- AtomicInteger resource = new AtomicInteger();
- for (int i = 0; i < 10; i++) {
- executorService.execute(new SemaphoreTask(semaphore, resource));
- }
- }
-
- public static void main(String[] args) {
- new SemaphoreTemplate().doSomething();
- }
-
- }
SemaphoreResultTask.java
- package codeTemplate.thread;
-
- import java.util.List;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.TimeUnit;
-
- public class SemaphoreResultTask implements Runnable{
- private final Semaphore semaphore;
- private final CountDownLatch latch;
- private final List
params; - private final int index;
-
- public SemaphoreResultTask(Semaphore semaphore, CountDownLatch latch, List
params,int index) { - this.semaphore=semaphore;
- this.latch = latch;
- this.params = params;
- this.index = index;
- }
-
- @Override
- public void run() {
- try{
- semaphore.acquire();
- params.set(index, params.get(index) + " over");
- TimeUnit.SECONDS.sleep(1);
- System.out.println("in task: "+params);
- semaphore.release();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }finally {
- latch.countDown();
- }
-
- }
- }
使用
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.Arrays;
- import java.util.List;
- import java.util.concurrent.*;
- import java.util.concurrent.atomic.AtomicInteger;
-
- public class SemaphoreTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
-
- public void doSomethingNeedResult() {
- try {
- List
params = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); - CountDownLatch latch = new CountDownLatch(params.size());
- Semaphore semaphore = new Semaphore(3);
- for (int i = 0; i < params.size(); i++) {
- executorService.execute(new SemaphoreResultTask(semaphore, latch, params, i));
- }
- latch.await(10, TimeUnit.MINUTES);
- System.out.println("result: "+params);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- }
-
- public static void main(String[] args) {
- new SemaphoreTemplate().doSomethingNeedResult();
- }
- }
- package codeTemplate.thread;
-
- import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import java.util.concurrent.*;
-
- public class ThreadPoolSubmitCallTemplate {
- private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 20, 30, TimeUnit.SECONDS,
- new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat("basic-thread-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
- public static Map
> sessionMap = new HashMap<>(); -
- public void doSomething(String index) {
- Future
future = executorService.submit(new Callable() { - @Override
- public String call() throws Exception {
- TimeUnit.SECONDS.sleep(new Random().nextInt(5));
- return "ok";
- }
- });
- sessionMap.put(index, future);
- }
-
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- ThreadPoolSubmitCallTemplate submitCallTemplate = new ThreadPoolSubmitCallTemplate();
- for (int i = 0; i < 10; i++) {
- submitCallTemplate.doSomething("session-" + i);
- }
- System.out.println(sessionMap.get("session-7").get());
- }
- }