Advertisement
BearWulf

multiply large arrays with 2 threads

Jul 31st, 2021
1,575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.19 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.  
  4.     public static void main(String[] args) {
  5.         int N = 5000;
  6.         int[] list1 = new int[N];
  7.         int[] list2 = new int[N];
  8.         int total;
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.         for (int k = 0; k <= N-1; k++)  list1[k] = scan.nextInt();
  12.         for (int k = 0; k <= N-1; k++)  list2[k] = scan.nextInt();
  13.  
  14.         MyRunnable runnable1 = new MyRunnable(list1);
  15.         Thread thread1 = new Thread(runnable1);
  16.  
  17.         MyRunnable runnable2 = new MyRunnable(list2);
  18.         Thread thread2 = new Thread(runnable2);
  19.  
  20.         thread1.start();
  21.         thread2.start();
  22.  
  23.         total = runnable1.getTotal() * runnable2.getTotal();
  24.  
  25.         System.out.println("total is : " + total);
  26.     }
  27. }
  28. //*****************************
  29. //"MyRunnable" file
  30.  
  31. public class MyRunnable implements Runnable{
  32.     private int[] list;
  33.     private int total = 1;
  34.  
  35.     public MyRunnable(int[] list){
  36.         super();
  37.         this.list = list;
  38.     }
  39.     public int getTotal(){
  40.         return this.total;
  41.     }
  42.     @Override
  43.     public void run() {
  44.         for (int i : this.list) {
  45.             this.total *= i;
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement