Advertisement
Guest User

thread process matrix

a guest
Jun 2nd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. //(c)Terminator
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5.  
  6. final class ThreadRow extends Thread {
  7.     private float[] row;
  8.     private int   index;
  9.    
  10.     public ThreadRow(float[] _row){
  11.         row = _row;
  12.         this.start();
  13.     }
  14.    
  15.     @Override
  16.     public void run(){
  17.         index = 0;
  18.         for(int i = 1; i < row.length; ++i){
  19.             if(row[i] > row[index])
  20.                 index = i;
  21.         }
  22.     }
  23.    
  24.     public float Max(){
  25.         return row[index];
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. public class MyApp {
  32.     public static void main(String[] args) {
  33.         String  fn   = "a.txt";
  34.        
  35.         ThreadRow[] trows = null;
  36.         float[][]     mat = null;
  37.        
  38.         try {
  39.             Scanner scan = new Scanner(new File(fn));
  40.             int rows = scan.nextInt(); // считываем кол-во строк
  41.             int cols = scan.nextInt(); // считывает кол-во столбцов
  42.            
  43.             trows = new ThreadRow[rows];
  44.             mat   = new float[rows][cols];
  45.            
  46.             // читаем матрицу
  47.             for(int r = 0; r < rows; ++r){
  48.                 for(int c = 0; c < cols; ++c)
  49.                     mat[r][c] = scan.nextFloat();
  50.             }
  51.             scan.close();
  52.             scan = null;
  53.  
  54.             // запуск потоков с раздачей по строке матрице
  55.             for(int i = 0; i < trows.length; ++i)
  56.                 trows[i] = new ThreadRow(mat[i]);
  57.            
  58.             // выводим результат работы
  59.             for(int i = 0; i < trows.length; ++i){
  60.                 trows[i].join();
  61.                 System.out.println("Номер строки №" + i + "\t\tмаксимум: " + trows[i].Max());
  62.                 trows[i] = null;
  63.             }
  64.             mat   = null;
  65.             trows = null;
  66.            
  67.         } catch(Exception e){
  68.             e.printStackTrace();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement