呼叫一個方法,直接實現多執行緒執行任務
阿新 • • 發佈:2018-12-12
import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.LinkedBlockingQueue; import java.util.function.Consumer; public class MutilProcessor<E> { LinkedBlockingQueue<E> QUEUE = new LinkedBlockingQueue<E>(100); List<Thread> THREAD_LIST = new ArrayList<>(); volatile boolean isReading = true; volatile boolean isEnd = false; public MutilProcessor<E> read(Collection<E> collection) { collection.stream().forEach(e -> { try { QUEUE.put(e); } catch (InterruptedException e1) { } }); isReading = false; return this; } public <X> MutilProcessor<E> addThreadNum(int threadNum, Consumer<E> consumer) { if (THREAD_LIST.size() < threadNum) { int add = threadNum - THREAD_LIST.size(); for (int i = 0; i < add; i++) { Thread thread = new Thread(() -> doing(consumer)); thread.start(); THREAD_LIST.add(thread); } } return this; } private <X> void doing(Consumer<E> consumer) { while (true) { E take = QUEUE.poll(); if (take != null) { try { consumer.accept(take); } catch (Exception e) { } } else { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if (QUEUE.isEmpty() && !isEnd) { break; } } } } public void end() { while (isReading) { try { Thread.sleep(1); } catch (InterruptedException e) { } } isEnd = false; } public static <E> MutilProcessor<E> getInstance() { return new MutilProcessor<E>(); } }
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class MutiString extends MutilProcessor<String> { @SuppressWarnings("unchecked") public static MutiString getInstance() { return new MutiString(); } public MutiString readFile(File filePath) { new Thread(() -> { try { BufferedReader csvReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); String records = csvReader.readLine(); while (records != null) { try { QUEUE.put(records); } catch (InterruptedException e) { e.printStackTrace(); } records = csvReader.readLine(); } csvReader.close(); isReading = false; } catch (IOException e) { e.printStackTrace(); } }).start(); return this; } public MutiString readFile(String filePath) { readFile(new File(filePath)); return this; } }
import java.util.Arrays;
public class ThreadTest {
public static void main(String[] args) {
new MutilProcessor<Integer>().read(Arrays.asList(1, 2, 3, 4, 5)).addThreadNum(1, ThreadTest::print).end();
}
public static void print(Integer i) {
System.out.println(i);
}
}