繁体   English   中英

多线程在完成处理之前返回数字(JAVA)

[英]Multithreading returns number before it finished processing (JAVA)

我写了下面这段代码来尝试理解多线程。 然而,结果并不是我所期望的。 似乎它在搜索完成执行之前返回了该值。 我怎样才能让它等到结果准备好然后才得到返回值?

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.concurrent.Executor;  
import java.util.concurrent.Executors;  
import java.util.concurrent.ThreadPoolExecutor;  
import java.util.concurrent.TimeUnit;  
import java.util.ArrayList;

//Finding the smallest number in an array
public class Main
{
    public static class Search implements Runnable {
        private int[] array;
        private int lowestNumber; 
        private int taskNumber;

        public Search(int[] array, int taskNumber){
            this.lowestNumber = 0;
            this.taskNumber = taskNumber;
            this.array = array;
        }

        public int getLowestNumber(){
            return lowestNumber;
        }

        protected void setLowestNumber(int lowestNumber){
            this.lowestNumber = lowestNumber;
        }

        protected void searchArrayLowestNumber(){
            int lowestValue = 0; 
            int arrayLength = array.length;

            for(int i = 0; i < arrayLength; i++){
                if( i == 0 ){
                    lowestValue = array[i];
                }
                if(array[i] < lowestValue){
                    lowestValue = array[i];
                } 
                System.out.println("array[i] lowestValue: " + lowestValue);
            }
            setLowestNumber(lowestValue);

        }

        public void run(){
                System.out.println("Accessing search...task number: " + taskNumber);
                searchArrayLowestNumber();

        }

    }

    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
        int[][] arrayA = {{12, 13, 1}, {10, 34, 1}};

        for (int i = 0; i <= 1; i++) 
        {
            int[] tempArray = new int[3];
            for(int j = 0; j < 3; j++){
                tempArray[j] = arrayA[i][j];
            }
            Search searchLowestNumber = new Search(tempArray, i);
            int number = searchLowestNumber.getLowestNumber();
             try{
                Long duration = (long) (Math.random() * 10);
                System.out.println("Lowest number for this thread " + i + " is " + number);
                TimeUnit.SECONDS.sleep(duration); 
             }catch (InterruptedException e) {
                e.printStackTrace();
             }

            executor.execute(searchLowestNumber);
        }
        executor.shutdown();
    }
}

目前的结果如下:

Lowest number for this thread 0 is 0                                                                                                                                               
Lowest number for this thread 1 is 0                                                                                                                                               
Accessing search...task number: 0                                                                                                                                                  
array[i] lowestValue: 12                                                                                                                                                           
array[i] lowestValue: 12                                                                                                                                                           
array[i] lowestValue: 1                                                                                                                                                            
Accessing search...task number: 1                                                                                                                                                  
array[i] lowestValue: 10                                                                                                                                                           
array[i] lowestValue: 10                                                                                                                                                           
array[i] lowestValue: 1 

我实际上希望两个线程最后都返回 1 。

你需要解决两件事首先你有你的打印语句System.out.println("array[i] lowestValue: " + lowestValue); 在您的 for 循环内但在您的 if 条件之外,因此它为数组中的每个元素lowestValue值的当前值,如果您希望仅在变量更新时打印它,您应该将它移到您的 if 语句中。

其次,你必须移动这个表达式System.out.println("Lowest number for this thread " + i + " is " + number); 在你调用executor.execute(searchLowestNumber);之后因为搜索实际上正在发生,否则您将要打印的数字是您在 class '0' 的构造函数中设置的数字,因为它当时尚未更新

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM