Make 1 Thread等待一个Thread数组完成

时间:2016-03-25 15:33:25

标签: java arrays multithreading

我有一组名为workers的线程。还有另一个名为status的独立线程。

两个线程都访问共享的LinkedBlockingQueue。 Workers(线程数组)使用poll()来提取工作和状态每30秒报告一次队列的大小。

我在运行此问题时遇到的问题是从状态类中打印出以下内容:

  

载...

     

...预览

但预览应该在上传之前,也就是之前出现。 那么,我认为我的状态对象不是在等待第一批工人完成?

我想要这个:

  

... DOWNLOADING

     

...预览

     

载...

但事情有点不同步。

// start up the Status Object class.
        int downloadSize = filesToDownload.size();
        Thread statusThread = new Thread(new Status(filesToDownload, currentYear, downloadSize, "DOWNLOADING..."));
        statusThread.start();

        /**
         * download the files
         */

        Thread[] workers = new Thread[NUMBER_OF_THREADS];
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            workers[x] = new Thread(new S3ObjectDownloader(filesToDownload, currentYear));
            workers[x].start();
        }
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            try {
                workers[x].join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        /**
         * create previews
         */
        int previewSize = filesToPreview.size();
        statusThread = new Thread(new Status(filesToPreview, currentYear, previewSize, "PREVIEWING..."));
        statusThread.start();

        workers = new Thread[NUMBER_OF_THREADS];
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            workers[x] = new Thread(new Worker(filesToPreview, currentYear));
            workers[x].start();
        }
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            try {
                workers[x].join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        /**
         * upload previews to S3.
         */
        // we need the TransferManager for the uploads.
        TransferManager txManager = new TransferManager(new ClasspathPropertiesFileCredentialsProvider());
        statusThread = new Thread(new Status(filesToUpload, currentYear, filesToUpload.size(), "UPLOADING..."));
        statusThread.start();

        workers = new Thread[NUMBER_OF_THREADS];
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            workers[x] = new Thread(new S3ObjectUploader(filesToUpload, currentYear, txManager));
            workers[x].start();
        }
        for (int x = 0; x < NUMBER_OF_THREADS; x++) {
            try {
                workers[x].join();
            } catch (InterruptedException e) { 
                // TODO Auto-generated catch
                // block
                e.printStackTrace();
            }
        }

        // shutdown transfer manager
        txManager.shutdownNow();

这是Status.java

public class Status implements Runnable {

    private String conferenceYear;
    private Queue<String>queue;
    private int queueSize;
    private String jobeName;

    public Status(Queue<String> queue, String conferenceYear, int queueSize, String jobName){
        this.conferenceYear = conferenceYear;
        this.queue = queue;
        this.queueSize = queueSize;
        this.jobeName = jobName;
    }

    @Override
    public void run() {
        while(!queue.isEmpty()){
            try {
                float completion = (queue.size() * 1.0f) / this.queueSize;
                System.out.println(this.jobeName+" : "+this.conferenceYear+ " remaining..."+MessageFormat.format("{0,number,#.##%}",completion));
                TimeUnit.SECONDS.sleep(30);;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

Java有CountDownlatches来支持这种情况。

看一下CountDownLatch课程。

该链接还包含一个非常容易理解的示例实现。您可以创建锁存器以指示每个任务的开始和等待结束。