如果不同的顺序,如何处理多个字符串,否则?

时间:2016-06-02 14:04:52

标签: java multithreading synchronization executorservice blockingqueue

下面是一个简单的代码,只尝试对一种字符串类型使用相同的executorService,这不能解决 处理多个字符串的我的问题,除非它们属于同一类型,如果它们属于同一类型,则线程必须等待处理前一个字符串。
另外,请建议我是否可以利用阻塞队列来实现这个,以及如何实现?

我能想到的另一种方法,但无法实施 - 将已选择一种类型的所有线程放入队列中等待,以便按顺序处理它们。同时其他类型被其他线程拾取。

  

输入A < -Thread1< -Thread2< -Thread3< -Thread8
   B型< -Thread4< -Thread5
  键入C < -Thread9 ...依此类推。

import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SOS {
    volatile static int i = 0;

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        ExecutorService executorService3 = Executors.newSingleThreadExecutor();
        ExecutorService[] e = {executorService1, executorService2, executorService3};
        String[] FeedList = {"A", "A", "A", "B", "B", "C", "C", "A", "C", "C", "C", "B", "B", "A", "A", "B", "B", "A",
            "A", "C"};



        final long startTime = System.currentTimeMillis();
        int k = 0;
        while (i < FeedList.length) {
            Callable<String> callable = new MyTask(FeedList, i);

            k = hash(FeedList, i, new RandomGen().Types());

            Future<String> success = e[k].submit(callable);
            if (!success.get().contains("Success"))
                System.out.println("Failure");
            i++;

        }

        e[0].shutdown();
        e[1].shutdown();
        e[2].shutdown();
        final long endTime = System.currentTimeMillis();
        System.out.println("Total execution time: " + (endTime - startTime));
    }

    private static int hash(String[] FeedList, int n, int numOfType) throws InterruptedException {

        HashMap<String, Integer> h = new HashMap<String, Integer>();
        int k = 0;

        for (int i = 0; i < FeedList.length; i++) {
            if (!h.containsKey(FeedList[i])) {
                k++;
                if (k >= numOfType)
                    k = 0;
                h.put(FeedList[i], k);

            }
            if (h.containsKey(FeedList[i]))
                Thread.sleep(1);

        }

        return h.get(FeedList[n]);
    }
}

1 个答案:

答案 0 :(得分:0)

您在提交后立即致电get,等待执行完成。这意味着执行程序不会并行执行。

您应首先制作所有Futures<...>,然后再拨打get

List<Future<String>> list = new ArrayList<>();

while (i < FeedList.length) {
    Callable<String> callable = new MyTask(FeedList, i);

    k = hash(FeedList, i, new RandomGen().Types());

    Future<String> success = e[k].submit(callable);
    list.add(success);            

    i++;    
}

for(Future<String> f : list) {
    if (!f.get().contains("Success"))
        System.out.println("Failure");
}

我也没有看到i volatile的重点,因为它只能从一个线程访问。在将其传递给MyTask的构造函数时,它会被复制。