Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中使用多线程池读取文本文件_Java_Multithreading_Threadpool - Fatal编程技术网

在JAVA中使用多线程池读取文本文件

在JAVA中使用多线程池读取文本文件,java,multithreading,threadpool,Java,Multithreading,Threadpool,我想读取一个包含6行用逗号分隔的整数的文件。每次我使用线程池时,它们都读取相同的行。我希望每个线程快速排序一行,保存在一个文件中。在该方法之后,mergeSort将对它们进行排序 public class myclass2 extends myclass { public static void main(String [] arg){ final myclass obj=new myclass(); System.out.println("Enter number of

我想读取一个包含6行用逗号分隔的整数的文件。每次我使用线程池时,它们都读取相同的行。我希望每个线程快速排序一行,保存在一个文件中。在该方法之后,mergeSort将对它们进行排序

 public class myclass2 extends myclass {
 public static void main(String [] arg){
 final myclass obj=new myclass();

        System.out.println("Enter number of threads you want to instantiate\n");
        Scanner handle=new Scanner (System.in);
        final int no=handle.nextInt();
         ExecutorService threadPool = Executors.newFixedThreadPool(no);
         for (int i = 0; i < no; i++) 
         {
             System.out.println(i);
                threadPool.submit(new Runnable() {
                     public void run() {
                         obj.runProgram(no);
                     }
                 });
         }
         System.out.println("Before merge sort in myclass 2");
         obj.mergesort();
             // once you've submitted your last job to the service it should be shut down
             threadPool.shutdown();



        handle.close();
}
}
公共类myclass2扩展了myclass{
公共静态void main(字符串[]arg){
最终myclass obj=新myclass();
System.out.println(“输入要实例化的线程数\n”);
扫描仪手柄=新扫描仪(System.in);
final int no=handle.nextInt();
ExecutorService线程池=Executors.newFixedThreadPool(否);
for(int i=0;i
runProgram方法的代码如下所示。它从Data.txt读取数据,对其进行快速排序,然后将其保存到文件中

   public void runProgram(int no)
   {

    Scanner handleforfile=null;
    String recFromFile=null;
    FileWriter x=null;
    try {
        int check=0;
        check++;
        if(check<2)
        {
            File fileobj= new File("Data.txt");
            handleforfile=new Scanner(fileobj);

            FileWriter handle=new FileWriter("ThreadingData.txt",true);
            BufferedWriter bwhandle1=new BufferedWriter(handle);
            x=handle;
            handle.close();
        }
                 if(handleforfile.hasNext())
                 {
                    for(int i=0;i<check;i++)
                    {
                     recFromFile=handleforfile.nextLine();//reads the line req
                    }
                     System.out.println("Rec line from file"+recFromFile);//rec data from file
                     String[] numberStrs = recFromFile.split(",");//convert it into now int[]
                        int[] numbers = new int[numberStrs.length];
                        for(int ik = 0;ik < numberStrs.length;ik++)
                        {
                           numbers[ik] = Integer.parseInt(numberStrs[ik]);
                           System.out.println(numbers[ik]);

                        }

                        System.out.println("converted into int[]");
                        quickSort(numbers,0,numbers.length-1);
                        String temp=Arrays.toString(numbers);
                        System.out.println(temp);
                        BufferedWriter bwhandle=new BufferedWriter(x);
                        try {
                            bwhandle.write(temp.substring(1,temp.length()-1));//avoids brakets////////////////
                            bwhandle.write('\n');
                            //bwhandle.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            System.out.println("here in myclass");
                            e.printStackTrace();
                        }
                 }//end if
                 else
                 {
                     System.out.println("returning");
                     return;
                 }


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println("here in myclass2");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("here in myclass3");
        e.printStackTrace();
    }
public void运行程序(int-no)
{
扫描仪handleforfile=null;
字符串recFromFile=null;
FileWriter x=null;
试一试{
整数检查=0;
check++;

如果(检查这里的问题是,每个线程创建一个单独的scanner实例,指向文件,因此每个实例都从第一行开始

File fileobj= new File("Data.txt");
handleforfile=new Scanner(fileobj);

如果希望每个线程读取相应的行,则只创建一个scanner实例并协调线程访问。

您需要在主线程中读取文件,将其拆分为多行,将每行交给另一个线程进行排序,然后再将其合并

多线程读取文件是没有意义的,因为线程必须同步,在这种情况下,没有足够的数据使其值得

如果有大量的行块,可以让主线程定位块边界,然后告诉子线程从何处开始读取,但让主线程执行所有读取操作可能会更快(否则可能会导致文件读取两次)

这基本上是地图缩小


您正在考虑输入读取器阶段。请参见

我应该在哪里制作它们?