一对一
1 public static void main(String[] args) { 2 LinkedBlockingQueue list = new LinkedBlockingQueue(); 3 List<Integer> fileList = Collections.synchronizedList(new ArrayList<>()); 4 for (int i = 0, size = 10000; i < size; i++) { 5 int num = new Random().nextInt(); 6 fileList.add(num); 7 } 8 ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build(); 9 ReentrantLock lock = new ReentrantLock(); 10 Condition condition = lock.newCondition(); 11 //生产 12 ThreadPoolExecutor downExecutor = new ThreadPoolExecutor(0, 8, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), factory, new RejectedExecutionHandler() { 13 @Override 14 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 15 try { 16 if (!executor.isShutdown()) { 17 executor.getQueue().put(r); 18 } 19 } catch (InterruptedException e) { 20 LOGGER.error("队列阻塞策略异常,切换到CallerRunsPolicy!"); 21 if (!executor.isShutdown()) { 22 r.run(); 23 } 24 } 25 } 26 }); 27 28 //消费 29 ThreadPoolExecutor upExecutor = new ThreadPoolExecutor(0, 8, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), factory, new RejectedExecutionHandler() { 30 @Override 31 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 32 try { 33 if (!executor.isShutdown()) { 34 executor.getQueue().put(r); 35 } 36 } catch (InterruptedException e) { 37 LOGGER.error("队列阻塞策略异常,切换到CallerRunsPolicy!"); 38 if (!executor.isShutdown()) { 39 r.run(); 40 } 41 } 42 } 43 }); 44 45 downExecutor.execute(new Runnable() { 46 @Override 47 public void run() { 48 lock.lock(); 49 try { 50 while (true) { 51 if (fileList.size() > 0) { 52 if (list.size() < 1) { 53 Integer first = fileList.get(0); 54 list.add(first); 55 fileList.remove(0); 56 System.out.println("生产了:" + first); 57 condition.signalAll(); 58 } else { 59 System.out.println("fileList:" + fileList.size() + "list:" + list.size()); 60 condition.await(); 61 } 62 } else { 63 System.out.println("生产执行完毕~~~~~"); 64 downExecutor.shutdownNow(); 65 break; 66 } 67 } 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } finally { 71 lock.unlock(); 72 } 73 } 74 }); 75 76 upExecutor.execute(new Runnable() { 77 @Override 78 public void run() { 79 lock.lock(); 80 try { 81 while (true) { 82 if (list.size() > 0) { 83 int num = (int) list.remove(); 84 System.out.println("消费了:" + num); 85 condition.signalAll(); 86 } else { 87 if (fileList.size() > 0) { 88 System.out.println("list:" + list.size()); 89 condition.await(); 90 } else { 91 System.out.println("消费执行完毕~~~~~"); 92 upExecutor.shutdownNow(); 93 break; 94 } 95 } 96 } 97 } catch (InterruptedException e) { 98 e.printStackTrace(); 99 } finally { 100 lock.unlock(); 101 } 102 } 103 }); 104 }
多对多
1 public static void main(String[] args) { 2 LinkedBlockingQueue list = new LinkedBlockingQueue(); 3 List<Integer> fileList = Collections.synchronizedList(new ArrayList<>()); 4 for (int i = 0, size = 1000; i < size; i++) { 5 int num = new Random().nextInt(); 6 fileList.add(num); 7 } 8 ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build(); 9 ReentrantLock lock = new ReentrantLock(); 10 Condition condition = lock.newCondition(); 11 //生产 12 ThreadPoolExecutor downExecutor = new ThreadPoolExecutor(0, 8, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), factory, new RejectedExecutionHandler() { 13 @Override 14 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 15 try { 16 if (!executor.isShutdown()) { 17 executor.getQueue().put(r); 18 } 19 } catch (InterruptedException e) { 20 LOGGER.error("队列阻塞策略异常,切换到CallerRunsPolicy!"); 21 if (!executor.isShutdown()) { 22 r.run(); 23 } 24 } 25 } 26 }); 27 28 //消费 29 ThreadPoolExecutor upExecutor = new ThreadPoolExecutor(0, 8, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), factory, new RejectedExecutionHandler() { 30 @Override 31 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 32 try { 33 if (!executor.isShutdown()) { 34 executor.getQueue().put(r); 35 } 36 } catch (InterruptedException e) { 37 LOGGER.error("队列阻塞策略异常,切换到CallerRunsPolicy!"); 38 if (!executor.isShutdown()) { 39 r.run(); 40 } 41 } 42 } 43 }); 44 45 downExecutor.execute(new Runnable() { 46 @Override 47 public void run() { 48 try { 49 while (true) { 50 if (fileList.size() > 0) { 51 if (list.size() < 1) { 52 Integer first = fileList.get(0); 53 list.add(first); 54 fileList.remove(0); 55 } 56 } else { 57 System.out.println("生产执行完毕~~~~~fileList:"+fileList.size()); 58 downExecutor.shutdownNow(); 59 break; 60 } 61 } 62 } catch (Exception e) { 63 e.printStackTrace(); 64 } 65 } 66 }); 67 68 upExecutor.execute(new Runnable() { 69 @Override 70 public void run() { 71 try { 72 while (true) { 73 if (list.size() > 0) { 74 int num = (int) list.remove(); 75 } else { 76 if (fileList.size() > 0) { 77 } else { 78 System.out.println("消费执行完毕~~~~~list:"+list.size()); 79 upExecutor.shutdownNow(); 80 break; 81 } 82 } 83 } 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } 87 } 88 }); 89 }
一学就会,一用就懵系列,多线程还是要多练习~
来源:https://www.cnblogs.com/fangyanr/p/12171899.html