【问题标题】:Threads that read from text files concurently and place data in arrays并发读取文本文件并将数据放入数组的线程
【发布时间】:2012-08-24 20:14:48
【问题描述】:

这段代码的目标是运行四个线程,这将打开四个文本文件,从中读取单词,然后将它们放入一个字符串数组中,

我知道的主要问题:

1- 我没有将并发函数放在 void run 函数中,我希望能够将参数传递给该函数

2- 我不确定我是否正在修改全局字符串数组

首先是main方法:

 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;
}

请让我知道你们的想法,任何帮助或解释或提示将不胜感激

【问题讨论】:

  • 您的问题和疑问具体是什么?
  • 不工作如何?您需要更具体并提供更多信息,以便我们能够为您提供帮助。
  • 您错误地在线程的构造函数中读取文件,而不是在run() 方法中。
  • @Ryan 幸运抽奖。我添加了一个扩展我的评论的答案。
  • @Ryan 你应该真的使用executors

标签: java arrays multithreading file-io global-variables


【解决方案1】:

如果我们先修复你的文件读取线程,让主要工作发生在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
}

【讨论】:

    【解决方案2】:

    三个错误:

    1. ReadFile 是一个实现Runnable 的类,因此您应该将一个实例传递给Thread 构造函数:

      线程thread1 = new Thread(new ReadFile("list1.txt", Global.array1),"thread1");

    2. 为什么在ReadFile 内创建Thread?删除它,不需要额外的跑步者!

    3. ReadFile 构造函数有一个额外的参数threadName 必须删除。

    【讨论】:

    • 1.这就是他正在做的事情。 2. 我同意这一点。
    • 1.不,他错过了 ReadFile 之前的关键字 new!
    【解决方案3】:

    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 的构造函数将threadName 作为第三个参数这一事实感到困惑,但Thread 类本身也有类似的参数。

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多