线程按顺序处理类似的字符串

时间:2016-05-30 07:48:37

标签: java multithreading data-structures synchronization executorservice

我遇到的问题是从队列中读取字符串,这些字符串会不断更新(为简单起见,只假设字符串列表):

ArrayList<int []> userPoints = new ArrayList<>();

/**
 * Method to Crop the image
 *
 * @param width             Width of the output image, in mm.
 * @param height            Height of the output image, in mm.
 */
private void cropImage(int width, int height) {
    Paint paint = new Paint();
    paint.setAntiAlias(false);
    paint.setFilterBitmap(false);

    int output_width_start = 0;
    int output_width_end = (width * 5) / 2;
    int output_height_start = 0;
    int output_height_end = (height * 5) / 2;

    Matrix matrix = new Matrix();
    matrix.setPolyToPoly(
            // where to take it from
            new float[]{
                    userPoints.get(0)[0], userPoints.get(0)[1],
                    userPoints.get(1)[0], userPoints.get(1)[1],
                    userPoints.get(2)[0], userPoints.get(2)[1],
                    userPoints.get(3)[0], userPoints.get(3)[1]},
            0,
            // what it should be
            new float[]{
                    output_width_start, output_height_start,
                    output_width_end, output_width_start,
                    output_width_end, output_height_end,
                    output_height_start, output_height_end},
            0, 4);

    Uri image_uri = Uri.fromFile(file); // get where the temp is stored
    Bitmap bitmap = BitmapFactory.decodeFile(image_uri.getPath());
    canvasRectangle.drawBitmap(bitmap, matrix, paint);
}

private void scaleImage(ImageView image) {
    Matrix matrix = image.getImageMatrix();
    RectF drawableRect = new RectF(userPoints.get(0)[0], userPoints.get(0)[1], 400, 300);
    RectF viewRect = new RectF(0, 0, imageViewPhoto.getWidth(), imageViewPhoto.getHeight());
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    imageViewPhoto.setImageMatrix(matrix);
}

我希望使用一种数据结构来存储正在处理哪种类型的字符串,并且只允许一个线程处理类似的字符串String[] hostList = {"R1","R2","R3","T1","T2","T3","T4","R4","K1"};

因此,所有类似的项目都是按顺序处理的,其他线程会拾取不同的项目。

这需要线程之间在是否选择字符串之间进行同步,或者让它在处理该字符串类型的线程上等待。

请告诉我可以使用的数据结构,以及如何使用(Type R/K/T)实现此目的。

3 个答案:

答案 0 :(得分:0)

您可能希望一个线程从列表中删除项目,检查类型,然后将项目排队到特定类型的队列。然后,每种类型都有自己的队列,以及自己的线程来处理该队列上的项目。某种类型的BlockingQueue,也许是LinkedBlockingQueue,可能是一种很好的队列,因为它是线程安全的。

如果您特别想使用ExecutorService,并且仍想分离不同类型项的处理,那么每个类型需要一个ExecutorService,并且从列表中删除项的线程可以提交项目到适当的ExecutorService。如果您希望每个类型都有一个包含多个线程的池,那么这将更有意义。

答案 1 :(得分:0)

我会使用BlockingQueues和一个额外的线程来解决这个问题。

一个BlockingQueue包含所有字符串,第二个包含需要顺序处理的字符串,第三个包含其余字符串。 额外的线程在一般队列的take方法上循环,一旦它取出一个项目,它就会存储到其他一个队列中,而这些队列又被相应的线程取出。

另一种解决方案是拥有两个执行程序服务,一个是通过调用Executors.newFixedThreadPool(1)创建的,另一个是按照您想要的方式创建的。然后排序线程将在这些ExecutorServices上调用ExecutorService#Submit,而不是排序到队列中。

答案 2 :(得分:0)

您可以运行多个ExecutorService(某些确定数量)并拥有自己的队列来从中选择数据。现在,另一个线程可以读取传入的数据并在输入类型上应用一个良好的分布式哈希函数,该函数返回将处理它的ExecutorService号。该项目应提交给相应的ExecutorService进行处理。哈希函数必须确保相同类型的输入数据返回相同的executorService编号。这将确保顺序处理相似类型的数据。

相关问题