简体   繁体   中英

Return value from threads in java

The problem is like this. I need to process a large file multiple times. So I think threads will help me increase the time. So far I have done this

The class that implements Runnable :

public class ProcessFile() implements Runnable {
       private int max;
       private int i;
       private int j;

       public ProcessFile(int i, int j) {
             this.i = i;
             this.j = j;
       }
       public void run() {
       //Perform operations on the file
       }
       public int getMaximum() {
             return this.max;
       }
 }

and the class from which I call the thread:

public calss Start {
     public static void main(String[] args) {
        for (int i=0;i<=10;i++) {
             for (int j=0;j<=5;j++) {
                  ProcessFile pf = new ProcessFile(i,j);
                  new Thread(pf).start();
                  int maximum = pf.getMaximum();
                  if (max > currentNr)
                       System.out.println("max is = " + max); 
             }
        }
     }
}

In the code above I intended to compute the maximum each time from the text file, and then return it back in the main program and then process this value. The code above doesn't seem to work since no value is displayed.

EDIT. Since I create the threads in the second loop I want to create 5 threads. Each of them performs the same operations on the file but under different criteria. Also each of this threads should return a value, that should be processed one at a time in the method that created the threads.

You have to wait for the thread to complete the job. Here you run a thread and then immediately call pf.getMaximum() in the main thread.

I would suggest using java.util.concurrent.Future to get results of a thread execution, and the Executors framework to create threads.

You can also use Thread.join() on each thread, and then check the results when all your threads have finished.

UPDATE

How about this if you don't want to make any significant changes?

The runnable:

public class ProcessFile implements Runnable {

    private ProcessFileListener listener;
    private int max;
    private int i;
    private int j;

    public ProcessFile(ProcessFileListener listener, int i, int j) {
        this.i = i;
        this.j = j;
        this.listener = listener;
    }

    public void run() {
        //Perform operations on the file
        listener.done(this);
    }

    public int getMaximum() {
        return this.max;
    }
}

The listener:

public interface ProcessFileListener {
    void done(ProcessFile process);
}

The main:

public class Start implements ProcessFileListener {

    private int max = Integer.MIN_VALUE;

    @Override
    public synchronized void done(ProcessFile pf) {

        int maximum = pf.getMaximum();
        if (maximum > max) {
            max = maximum;
            System.out.println("max is = " + max);
        }
    }

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

        ProcessFileListener listener = new Start();

        for (int i = 0; i <= 10; i++) {
            for (int j = 0; j <= 5; j++) {
                ProcessFile pf = new ProcessFile(listener, i, j);
                new Thread(pf).start();
            }
        }
    }
}

Notice that the method done() is synchronized , ie it's thread-safe. That is because you might need to access the same variable ( max in this case) from multiple threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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