java多线程小实现

在网上看到一道网试题,手痒痒实现了一下,呵呵。

使用Java多线程实现下述算法:
   输入:整数组成的m*n的矩阵A。(m=100000, n=10000)
   输出:一个数列B,数列B中的每一项为矩阵A中对应列数字之和。

 

 1  package  com.base.multithread;
 2 
 3  import  java.io.IOException;
 4  import  java.util.ArrayList;
 5  import  java.util.List;
 6  import  java.util.Scanner;
 7 
 8  public   class  PrintMatrix {
 9 
10       public   static   volatile   int  count  =   0 ;
11 
12       class  ComputeRunnable  implements  Runnable
13      {
14           private   int [][] values  =   null ;
15           private   int [] results  =   null ;
16           private   int  colIndex  =   0 ;
17          
18           public   void  init( int [][] values, int  colIndex, int [] results)
19          {
20               this .values  =  values;
21               this .colIndex  =  colIndex;
22               this .results  =  results;
23              
24          }
25 
26           public   void  run() {
27               int  sum  =   0 ;
28               for ( int  i  =   0  ;i  <  values.length;i ++ )
29              {
30                  sum  =  sum  +  values[i][colIndex];
31              }
32              results[colIndex]  =  sum;
33              count ++ ;
34          }
35          
36      }
37       /**
38       *  @param  args
39       *  @throws  IOException 
40       *  @throws  InterruptedException 
41        */
42       public   static   void  main(String[] args)  throws  IOException, InterruptedException {
43           int  m  =   0 ;
44           int  n  =   0 ;
45          PrintMatrix pm  =   new  PrintMatrix();
46          System.out.print( " Print input m(matrix row): " );
47          Scanner scanner  =   new  Scanner(System.in);
48          String srow  =  scanner.nextLine();
49          System.out.print( " Print input n(matrix col): " );
50          String scol  =  scanner.nextLine();
51          m  =   new  Integer(srow).intValue();
52          n   =   new  Integer(scol).intValue();
53           int [][] arrys  =   new   int [m][n];
54          System.out.println( " please input  "   +  m  + "  *  "   +  n  + "  matrix,each column with space split: " );
55          List < String >  lines  =   new  ArrayList < String > ();
56           int  index  =   0 ;
57           while (scanner.hasNextLine())
58          {
59              lines.add(scanner.nextLine());
60              index ++ ;
61               if (index == m)
62                   break ;
63          }
64           // 未检查输入正确性
65           for int  i  =   0  ;i  <  m;i ++ )
66          {
67              String line  =  lines.get(i);
68              String[] lineArray  =  line.split( "   " );
69               for int  j  =   0  ;j  <  n;j ++ )
70              {
71                  arrys[i][j]  =   new  Integer(lineArray[j]).intValue();
72              }
73          }
74           int [] results  =   new   int [n];
75           for int  i  =   0 ;i  <  n ;i ++ )
76          {
77              ComputeRunnable r  =  pm. new  ComputeRunnable();
78              r.init(arrys,i,results);
79              Thread t  =   new  Thread(r);
80              t.start();
81          }
82           while ( true )
83          {
84               if (count  ==  n)
85                   break ;
86          }
87           for ( int  i  =   0 ; i  <  n;i ++ )
88              System.out.println( " | "   +  results[i]  +   " | " );
89      }
90      
91      
92  }
93 

 

 

你可能感兴趣的:(java多线程)