From afb71dd3faf9a99da301b96be18f2d319a6c93ad Mon Sep 17 00:00:00 2001 From: luojw <1104049235@qq.com> Date: Wed, 27 May 2020 14:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../multithread/MultiThreadWordCount1.java | 104 +++++++++++++++++- .../multithread/MultiThreadWordCount2.java | 72 +++++++++++- .../multithread/MultiThreadWordCount3.java | 50 ++++++++- .../multithread/MultiThreadWordCount4.java | 10 +- 4 files changed, 224 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java index 15c4aae..ca5d95a 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java @@ -1,12 +1,112 @@ package com.github.hcsp.multithread; +import com.sun.xml.internal.ws.addressing.WsaActionUtil; + import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; public class MultiThreadWordCount1 { // 使用threadNum个线程,并发统计文件中各单词的数量 - public static Map count(int threadNum, List files) { - return null; + //使用wait() 和 notify() + public static Map count(int threadNum, List files) throws InterruptedException { + //使用多个线程统计 + ArrayList> maps = new ArrayList<>(); + ConcurrentHashMap map = new ConcurrentHashMap<>(); + AtomicInteger nums = new AtomicInteger(0); + for (int i = 0; i { + synchronized (nums){ + File file = files.get(nums.get()); + if (nums.get() !=files.size()){ + maps.add(MultiThreadWordCount1.merge(file)); + nums.getAndIncrement(); + } + //提醒主线程已完成 + System.out.println("通知主线程完成"); + nums.notify(); + } + }); + thread.start(); + } + //主线程等待执行 + synchronized (nums){ + while (nums.get()!=files.size()) { + System.out.println("线程等待"); + nums.wait(); + System.out.println("线程等待结束"); + } + } + System.out.println("线程等待彻底结束"); + + for (Map stringIntegerMap : maps) { + Set strings = stringIntegerMap.keySet(); + for (String string : strings) { + Integer num = map.putIfAbsent(string, stringIntegerMap.get(string)); + if (num==null){ + //不存在 存入该值 + map.put(string, stringIntegerMap.get(string)); + } else { + //存在 相加 + map.put(string, stringIntegerMap.get(string)+map.get(string)); + } + + } + } + return map; + } + + + + + public static void main(String[] args) throws InterruptedException { + + Map count = count(2, Arrays.asList(new File("1.txt"), new File("2.txt"))); + System.out.println(count.toString()); + } + + //主线程生产 多线程消费 + + + + + /** + * + * + * @description: 合并map + * @return: map + * @author: luojw + * @param file + * @return Map + * @time: + */ + public static Map merge(File file){ + ConcurrentHashMap map = new ConcurrentHashMap<>(); + if (file != null) { + try { + List strings = Files.readAllLines(file.toPath()); + for (String string : strings) { + String[] s = string.split(" "); + for (String s1 : s) { + if (map.putIfAbsent(s1, 1) == null) { + map.put(s1, 1); + } else { + map.put(s1, map.get(s1) + 1); + } + + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return map; } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java index 3f23afa..9ac7c81 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java @@ -1,8 +1,72 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Arrays; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + public class MultiThreadWordCount2 { - // 使用threadNum个线程,并发统计文件中各单词的数量 - // public static Map count(int threadNum, List files) { - // return null; - // } + //使用threadNum个线程,并发统计文件中各单词的数量 + //使用 juc 包中的 Lock/Condition + public static Map count(int threadNum, List files) throws InterruptedException { + Lock lock = new ReentrantLock(); + ArrayList> maps = new ArrayList<>(); + ConcurrentHashMap map = new ConcurrentHashMap<>(); + AtomicInteger nums = new AtomicInteger(0); + Condition condition = lock.newCondition(); + for (int i = 0; i < threadNum; i++) { + Thread thread = new Thread(() -> { + try { + lock.lock(); + File file = files.get(nums.get()); + if (nums.get() != files.size()) { + maps.add(MultiThreadWordCount1.merge(file)); + nums.getAndIncrement(); + } + //提醒主线程已完成 + System.out.println("通知主线程完成"); + condition.signal(); + } finally { + lock.unlock(); + } + }); + thread.start(); + } + //主线程等待执行 + lock.lock(); + while (nums.get() != files.size()) { + System.out.println("线程等待"); + condition.await(); + System.out.println("线程等待结束"); + } + lock.unlock(); + System.out.println("线程等待彻底结束"); + for (Map stringIntegerMap : maps) { + Set strings = stringIntegerMap.keySet(); + for (String string : strings) { + Integer num = map.putIfAbsent(string, stringIntegerMap.get(string)); + if (num == null) { + //不存在 存入该值 + map.put(string, stringIntegerMap.get(string)); + } else { + //存在 相加 + map.put(string, stringIntegerMap.get(string) + map.get(string)); + } + + } + } + return map; + } + public static void main(String[] args) throws InterruptedException { + + Map count = count(2, Arrays.asList(new File("1.txt"), new File("2.txt"))); + System.out.println(count.toString()); + } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java index e180ee2..0d7284c 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java @@ -1,8 +1,52 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; + public class MultiThreadWordCount3 { // 使用threadNum个线程,并发统计文件中各单词的数量 - // public static Map count(int threadNum, List files) { - // return null; - // } + //使用countDownLatch + public static Map count(int threadNum, List files) throws Exception { + //使用多个线程统计 + ArrayList> maps = new ArrayList<>(); + ConcurrentHashMap map = new ConcurrentHashMap<>(); + CountDownLatch countDownLatch = new CountDownLatch(files.size()); + + for (int i = 0; i { + synchronized (files){ + System.out.println(""+(countDownLatch.getCount())); + maps.add(MultiThreadWordCount1.merge(files.get((int) (countDownLatch.getCount()-1)))); + countDownLatch.countDown(); + } + }); + thread.start(); + } + //主线程等待执行 + countDownLatch.await(); + System.out.println("线程等待彻底结束"); + + for (Map stringIntegerMap : maps) { + Set strings = stringIntegerMap.keySet(); + for (String string : strings) { + Integer num = map.putIfAbsent(string, stringIntegerMap.get(string)); + if (num==null){ + //不存在 存入该值 + map.put(string, stringIntegerMap.get(string)); + } else { + //存在 相加 + map.put(string, stringIntegerMap.get(string)+map.get(string)); + } + + } + } + return map; + } + public static void main(String[] args) throws Exception { + + Map count = count(2, Arrays.asList(new File("1.txt"), new File("2.txt"))); + System.out.println(count.toString()); + } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java index 13e0cad..f72ff83 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java @@ -1,8 +1,12 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.List; +import java.util.Map; + public class MultiThreadWordCount4 { // 使用threadNum个线程,并发统计文件中各单词的数量 - // public static Map count(int threadNum, List files) { - // return null; - // } + public static Map count(int threadNum, List files) { + return null; + } }