调用一个方法,直接实现多线程执行任务

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
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;
  volatile CountDownLatch countDown;

  public MutilProcessor<E> read(Collection<E> collection) {
    isReading = true;
    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);
      }
    }
    countDown = new CountDownLatch(threadNum);
    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) {
          e.printStackTrace();
        }
      } else {
        try {
          Thread.sleep(50);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        if (QUEUE.isEmpty() && !isEnd) {
          break;
        }
      }
    }
    countDown.countDown();
  }

  public void end() {
    while (isReading) {
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
      }
    }
    isEnd = false;
    try {
      countDown.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  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 {
        isReading = true;
        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);
  }
}

实现思路:

一个线程将数据填入对列中,另外几个线程取对列中的数据,对于具体执行的方法,如果有返回值使用Function(没有用到,所以未实现),没有返回值用Consumer,这样通过一行代码就可以实现了。

应用场景,

1.for循环里面执行的速度很慢,数据很多的情况。

2.读取很大的文件,每一行为一条数据,需要多线程处理的情况。

猜你喜欢

转载自blog.csdn.net/york_2016/article/details/82900565