M
M
Maxim2015-09-12 17:52:50
Java
Maxim, 2015-09-12 17:52:50

How to pass an array to a stream?

Tell me how to pass the arr array filled with random numbers to the stream, to calculate the sum ...

public class ff {
    public static void main(String[] args) throws InterruptedException {
 
        new Threard1().list();
 
        long timer = -System.currentTimeMillis();
 
        Runnable r1 = new Threard1();
        Thread t1 = new Thread(r1);
 
        t1.start();
 
        t1.join();
 
        System.out.print("\n\n" + "mainSum: " + new Threard1().getSum());
 
        timer += System.currentTimeMillis();
        System.out.println("\n" + "Time: " + timer + "\n");
        }
}

import java.util.Random;

public class Threard1 implements Runnable{
    private String title = "One";
    private int delay=0;
    private int sizeArr=5;
    int sum;
    public int arr[] = new int[sizeArr];
 
    public void list () {
 
        Random rand = new Random();
        for (int i = 0; i < sizeArr; i++) {
            arr[i] = rand.nextInt(10);
            System.out.print("\n" + title + ": " + arr[i]);
        }
    }
 
 
    public void run() {
        for (int i = 0; i < sizeArr; i++) {
            sum = sum + arr[i];
        }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
 
    public int getSum () {
        return sum;
    }
 
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question