为什么SynchronousQueue允许其他线程以随机顺序接收?

时间:2015-05-17 11:39:19

标签: java multithreading sorting

我正在尝试实现一个小程序,它将使用SynchronousQueue以升序排序整数数组/向量。

我接管的方法是创建一个线程池,其中每个线程只按一次按升序对元素进行排序,方法是迭代它们并比较位置上的元素 i < / em>和 i + 1 并交换它们。

所以我创建了一个执行此交换的类,并将迭代编号,两个SynchronousQueues和数组本身作为参数,如下所示:

class OrderAsc extends Thread {
int length;
int iteration;
SynchronousQueue<ArrayList> st, dr; 
ArrayList<Integer> vector;
boolean swapMade;

    OrderAsc( int iteration, SynchronousQueue<ArrayList> st, SynchronousQueue<ArrayList> dr, ArrayList<Integer> vector) throws InterruptedException{

        this.length = vector.size();
        this.st = st;
        this.dr = dr;
        this.vector = vector;
        this.iteration = iteration;
        this.swapMade = false;
    }

    // Rularea thread-ului
    @Override
    public void run(){

        if(iteration > 0){

            try {
                vector = st.take();
                System.out.println(iteration + " get " + vector);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
        }

        // System.out.println(iteration + " >>> " + vector);


        for(int i = 0; i < length-1; i++){
            if(vector.get(i) > vector.get(i+1)){

                int tmp = vector.get(i);
                vector.set(i, vector.get(i+1));
                vector.set(i+1, tmp);

                swapMade = true;
            }
        }

        if(swapMade){
            try {

                dr.put(vector);
                System.out.println(iteration + " put " + vector);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
        } else { 
            // Display result
            System.out.println(vector);
        }
        System.exit(0);

    }

}

在主类中,我正在创建此类对象的ArrayList,如下所示:

ArrayList<OrderAsc> orderFilter = new ArrayList<OrderAsc>(n+2);
            for(int i = 0; i < n + 2; i++){
                orderFilter.add(i, new OrderAsc(i, st, dr, vector));
                st = dr;
                dr = new SynchronousQueue<ArrayList>();                    

            }

            for(int i=0; i < n + 2; i++){
                orderFilter.get(i).setDaemon(true);
                orderFilter.get(i).start();
            }

考虑到 OrderAsc 对象的实现,第一个线程将对数据进行排序迭代,然后它将PUT输入 dr 队列结果。然后,被阻塞的下一个线程将等待第一个 TAKE 此结果,再次过滤它,然后将其发送到下一个,直到整个数组被排序。

出乎意料的是,似乎线程并不是一直在等待那些SynchronousQueues,并且由于某些原因,它们有时会随机地获取数据,如下所示:

1 get [5, 4, 3, 2, 1, 6]
0 put [4, 3, 2, 1, 5, 6]
1 put [4, 3, 2, 1, 5, 6]
2 get [4, 3, 2, 1, 5, 6]
BUILD SUCCESSFUL (total time: 5 seconds)

SynchronousQueue是否有不同的工作原理并且我错误地实现了它?

1 个答案:

答案 0 :(得分:2)

我尝试用一​​个同步队列重写你的程序...... 简而言之,线程0放入初始未排序的向量和 其余的线程以迭代方式开始排序。

import java.util.ArrayList;
import java.util.concurrent.SynchronousQueue;


class OrderAsc extends Thread {
    int length;
    int iteration;
    SynchronousQueue<ArrayList> st, dr; 
    ArrayList<Integer> vector;
    boolean swapMade;

    OrderAsc( int iteration, SynchronousQueue<ArrayList> st, SynchronousQueue<ArrayList> dr, ArrayList<Integer> vector) throws InterruptedException{
        this.length = vector.size();
        this.st = st;
        this.dr = dr;
        this.vector = vector;
        this.iteration = iteration;
        this.swapMade = false;
    }

    @Override
    public void run(){

        if ( iteration == 0 ){
            try {

                System.out.println("\n  " + iteration  + " put " + vector);

                st.put(vector);

                Thread.currentThread().interrupt();
                return;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else{

            try {
                vector = st.take();
                // System.out.println("\n Take in Thread : " + Thread.currentThread().getName());
                System.out.println(iteration + " take " + vector);
            } catch (InterruptedException ex) {
                System.out.println(ex);
            }

        } 


        for(int i = 0; i < vector.size() - 1; i++){

            if(vector.get(i) > vector.get(i+1)){
                int tmp = vector.get(i);
                vector.set(i, vector.get(i+1));
                vector.set(i+1, tmp);

                swapMade = true;
            }

        }


        if(swapMade){
                //System.out.println("\n Swap Done in  Thread : " + Thread.currentThread().getName());

            try {
                st.put(vector);

                System.out.println(iteration + " sorted " + vector);

            } catch (InterruptedException ex) {
                System.out.println(ex);
            }
        }  

        //System.out.println("\n Exiting Thread : " + Thread.currentThread().getName());
        // System.exit(0);

    }


    public static void main(String args[]){
        ArrayList<OrderAsc> orderFilter = new ArrayList<OrderAsc>(5);

        ArrayList<Integer> vector = new ArrayList<Integer>(6);
        vector.add(4);
        vector.add(1);
        vector.add(3);
        vector.add(5);
        vector.add(6);
        vector.add(2);


        System.out.println("\n Unsorted Array " + vector + "\n\n");

        int n = vector.size();

        //vector.add(1);
        //vector.add(5);

        SynchronousQueue<ArrayList> st1 = new SynchronousQueue<ArrayList>();
        SynchronousQueue<ArrayList> dr1 = new SynchronousQueue<ArrayList>();

        for(int i = 0; i < n ; i++){
            try {

                orderFilter.add(i, new OrderAsc(i, st1, dr1, vector));

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //st1 = dr1;
            //dr1 = new SynchronousQueue<ArrayList>();                    
        }

        for(int i=0; i < n - 1; i++){
            //orderFilter.get(i).setDaemon(true);
             orderFilter.get(i).start();
        }


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("\n Voila Thanks Threads and the  Sorted Array Is ");

        for(int i = 0; i < vector.size(); i++){
            System.out.print(" " + vector.get(i) +  " ");
        }   

    }

}