2

このコードの目的は、4つのスレッドを実行することです。これにより、4つのテキストファイルが開き、それらから単語が読み取られ、文字列配列に配置されます。

私が知っている主な問題:

1-並行関数をvoidrun関数に入れていません。その関数にパラメーターを渡せるようにしたいのですが、

2-文字列のグローバル配列を変更しているかどうかわからない

最初は主な方法です:

 public static void main(String[] args) throws IOException 
    {
         //declare the threads
         Thread thread1 =  new Thread(ReadFile("list1.txt", Global.array1,"thread1"));
         Thread thread2 =  new Thread(ReadFile("list2.txt", Global.array2,"thread2"));
         Thread thread3 =  new Thread(ReadFile("list3.txt", Global.array3,"thread1"));
         Thread thread4 =  new Thread(ReadFile("list4.txt", Global.array4,"thread2"));

         /*error message from netbeans: cannot find symbol
            symbol:   method ReadFile(java.lang.String,java.lang.String[])

            it says it for every delcaration of the thread*/


         thread1.start();  //putting the threads to work
         thread2.start();
         thread3.start();
         thread4.start();

         thread1.join();   //telling the threads to finish their work
         thread2.join();
         thread3.join();
         thread4.join();


         // merging the arrays into one
         List list = new ArrayList(Arrays.asList(Global.array1));
         list.addAll(Arrays.asList(Global.array2));
         list.addAll(Arrays.asList(Global.array3));
         list.addAll(Arrays.asList(Global.array4));
         Object[] theArray = list.toArray();

           -------------------------etc----------------------------

私の語彙が正しければ、これは「スレッドクラス」です

public class ReadFile implements Runnable
{
    public void run(){
            //I should get stuff here, that's my problem!!!!
    }


    private String path;
    Thread runner;

    public ReadFile(String filePath, String[] toArray, String threadName) throws IOException
    {
        String path = filePath;
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numOfLines = readLines();  
        toArray = new String[numOfLines];

        int i;
        for (i=0; i<numOfLines; i++)
        {
            toArray[i]= textReader.readLine();    //place next line into string array
        }

        textReader.close();
    }


   int readLines() throws IOException
    {
        FileReader fr = new FileReader(filePath);
        BufferedReader bf = new BufferedReader(fr);

        String aLine;
        int noOfLines = 0;

        while((aLine = bf.readLine()) != null)
        {
            noOfLines++;
        }
        bf.close();
        return noOfLines;
    }

    }

最後に、グローバル変数のクラスを作成しましたが、それが良いアイデアかどうかはわかりません。

public class Global 
{
    public static String[] array1;
    public static String[] array2;
    public static String[] array3;
    public static String[] array4;
}

あなたたちがどう思うか教えてください、どんな助けや説明やヒントも大歓迎です

4

3 に答える 3

7

最初にファイル読み取りスレッドを修正して、主な作業がrun()メソッドで発生するようにすると、次のようになります。

public class FileReader extends Thread {

  private final File file;
  private String[] lines;

  public FileReader(File file) {
    this.file = file;
  }

  @Override
  public void run() {
    // Read file here (populate `lines`)..
  }

  public String[] getLines() {
    return lines;
  }
}

main次に、次のように、これをメソッドで利用できます。

public static void main(String[] args) throws Exception {
  List<FileReader> threads = new ArrayList<FileReader>();

  threads.add(new FileReader(new File("foo1")));
  threads.add(new FileReader(new File("foo2")));
  threads.add(new FileReader(new File("foo3")));
  threads.add(new FileReader(new File("foo4")));

  for (FileReader t : threads) {
    t.start();
  }

  List<String> allLines = new ArrayList<String>();

  for (FileReader t : threads) {
    t.join();
    allLines.addAll(Arrays.asList(t.getLines()));
  }    

  // File lines now in allLines
}
于 2012-08-24T20:44:27.343 に答える
1

3つの間違い:

  1. ReadFileはを実装するクラスであるため、コンストラクターRunnableにインスタンスを渡す必要があります。Thread

    スレッドthread1=new Thread(new ReadFile( "list1.txt"、Global.array1)、 "thread1");

  2. なぜThread内部を作成するのReadFileですか?それを削除してください、追加のランナーは必要ありません!

  3. コンストラクターには、削除する必要のReadFileある追加のパラメーターthreadNameがあります。

于 2012-08-24T20:38:36.773 に答える
1

コンストラクターの署名ReadFile

public ReadFile(String filePath, String[] toArray, String threadName)

の呼び出しと一致しません(提供しているのは、mainのみです)。私はあなたが意味したと思います:StringString[]

Thread thread1 =  new Thread(new ReadFile("list1.txt", Global.array1, "thread1"));
Thread thread2 =  new Thread(new ReadFile("list2.txt", Global.array2, "thread2"));
Thread thread3 =  new Thread(new ReadFile("list3.txt", Global.array3, "thread1"));
Thread thread4 =  new Thread(new ReadFile("list4.txt", Global.array4, "thread2"));

ReadFileのコンストラクターがthreadName3番目のパラメーターとしてを使用するという事実におそらく混乱しましたが、Threadクラス自体にも同様のパラメーターがあります。

于 2012-08-24T20:34:18.003 に答える