2012-07-29 70 views
0

我有一個調用兩個類的方法的Java代碼。像以下,Java線程代碼問題

import java.io.*; 
class Example 
{ 
public static void main(String args[]) 
    { 
    try{ 
FileOutputStream fos = new FileOutputStream("1.dat"); 
DataOutputStream dos = new DataOutputStream(fos); 

for(int i =0 ; i < 100 ; i++){ 
dos.writeInt(i); 
} 
dos.close(); 

FileOutputStream fos1 = new FileOutputStream("2.dat"); 
DataOutputStream dos1 = new DataOutputStream(fos1); 

for(int i =100 ; i < 200 ; i++){ 
dos1.writeInt(i); 
} 
dos1.close(); 


Exampless ex = new Exampless(); 
ex.createArray(0); 
ex.ReadData("1.dat"); 
ex.ReadData("2.dat"); 

    }catch (Exception e){ 
    System.err.println("Error: " + e.getMessage()); 
    } 
    } 
} 

class Exampless{ 

public static int []arr = new int [100] ; 
void createArray(int z){ 
    for(int i =z ; i < z+100 ; i++) 
     arr[i-z] = i ; 
} 
public synchronized void ReadData(String name){ 
    try{ 
int cnt = 0; 
FileInputStream fin = new FileInputStream(name); 
DataInputStream din = new DataInputStream(fin); 
for(int i = 0 ; i < 100 ; i++){ 
int c = din.readInt(); 
if(c == arr[i]) 
cnt++ ; 
} 

System.out.println("File name: " + name + " No. of Matches: " + cnt) ; 
    }catch (Exception e){ 
    System.err.println("Error: " + e.getMessage()); 
    } 
    } 
} 

在第一方法中,代碼將創建一個共享的陣列和在第二方法中,將其與一個文件進行比較。

現在,我想以並行方式運行這兩個ReadData()方法,使用多個線程。任何人都可以幫助我做到這一點。可能與一些代碼修改。

+2

你們是不是並行運行一次在兩個獨立的數據集的工作(以更好地利用多核處理器,例如),或者是你想獲得兩個線程在一組數據上工作(這更麻煩)? – Bobulous 2012-07-29 18:11:19

+0

哪兩個?你的意思是'createArray'和'ReadData'?還是你想用不同的參數多次調用'ReadData'?在後一種情況下,我會建議同步對數組「arr」的訪問。例如。使用[ReadWriteLock](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReadWriteLock.html)。 – 2012-07-29 18:11:54

+0

@ user1515834,我想用它來更好地使用多核處理器。正如你所看到的,不需要同步。 – Arpssss 2012-07-29 18:12:53

回答

2
import java.io.*; 
public class Example{ 
public static void main(String args[]) { 
    try { 
     FileOutputStream fos = new FileOutputStream("1.dat"); 
     DataOutputStream dos = new DataOutputStream(fos); 

     for (int i = 0; i < 100; i++) { 
      dos.writeInt(i); 
     } 
     dos.close(); 

     FileOutputStream fos1 = new FileOutputStream("2.dat"); 
     DataOutputStream dos1 = new DataOutputStream(fos1); 

     for (int i = 100; i < 200; i++) { 
      dos1.writeInt(i); 
     } 
     dos1.close(); 

     Exampless.createArray(0); //static method call to set the static arr variable 
     Exampless ex1 = new Exampless("1.dat"); 
     Exampless ex2 = new Exampless("2.dat"); 
     Thread t1 = new Thread(ex1); 
     Thread t2 = new Thread(ex2); 
     t1.start(); //calls the run method in ex1 in a new thread 
     t2.start(); 

    } catch (Exception e) { 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 
} 

class Exampless implements Runnable { 

public static int[] arr = new int[100]; 
public String _name; 

public Exampless(String name) { 
    this._name = name; 
} 

static void createArray(int z) { 
    for (int i = z; i < z + 100; i++) { 
     arr[i - z] = i; 
    } 
} 

@Override 
public void run() { 
    try { 
     int cnt = 0; 
     FileInputStream fin = new FileInputStream(_name); 
     DataInputStream din = new DataInputStream(fin); 
     for (int i = 0; i < 100; i++) { 
      int c = din.readInt(); 
      if (c == arr[i]) { 
       cnt++; 
      } 
     } 
     System.out.println("File name: " + _name + " No. of Matches: " + cnt); 
    } catch (Exception e) { 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 

} 
0

使類實現「可運行」,創建2個實例並運行它們。

+0

但是,如果我讓類實現「runnable」,那麼它將同時運行這兩種方法。我只想要一個並行運行。 – Arpssss 2012-07-29 18:26:34

0

只要您在另一個線程中調用ReadData時不修改陣列,則可以不進行同步。
更好,更健壯的方式是同步對陣列的訪問。

import java.io.*; 
class Exampless { 

    private static int[] arr = new int[100]; 
    private static ReadWriteLock lock = new ReentrantReadWriteLock(); 

    void createArray(int z) { 
     lock.writeLock().lock(); 
     try { 
     for (int i = z; i < z + 100; i++) 
      arr[i - z] = i; 
     } finally { 
      lock.writeLock().unlock(); 
     } 
    } 

    void ReadData(String name) { 
     lock.readLock().lock(); 
     try { 
      int cnt = 0; 
      FileInputStream fin = new FileInputStream(name); 
      DataInputStream din = new DataInputStream(fin); 
      for (int i = 0; i < 100; i++) { 
       int c = din.readInt(); 
       if (c == arr[i]) 
        cnt++; 
      } 

      System.out 
        .println("File name: " + name + " No. of Matches: " + cnt); 
     } catch (Exception e) { 
      System.err.println("Error: " + e.getMessage()); 
     } finally { 
      lock.readLock().unlock(); 
     } 
    } 
} 
+0

這是並行運行嗎? – Arpssss 2012-07-29 18:21:29

+0

您仍然需要創建[Thread](http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html),但您可以安全地多次調用ReadData方法通過不同的線程。沒有同步化,它會來[Race Condition](http://de.wikipedia.org/wiki/Race_Condition)s。 – 2012-07-29 18:38:28