将实用程序类转换为多线程类

时间:2021-12-26 00:56:16

I have a utility class in Java which is accessing a big file system to access a file. Some files are huge so whats happening is that the Utility class is talking a lot of time to access these files and i am facing a performance issue here. I plan to implement Multithreading to improve performance but i am bit confused as to how i need to do that. below is the structure of the Utility class.

我在Java中有一个实用程序类,它访问一个大文件系统来访问文件。有些文件是巨大的,所以发生的事情是Utility类正在谈论大量时间来访问这些文件,我在这里遇到性能问题。我计划实现多线程以提高性能,但我对如何做到这一点感到困惑。下面是Utility类的结构。

public class Utility {

     public static void Method1(ArrayList values){
          //do some processing
          for(int i=0; i< values.size();i++){
             ArrayList<String> details= MethodAccessFileSystem();

             CreateFileInDir(details);
        } 
     }
     public ArrayList<String> MethodAccessFileSystem(){
        //Code to access the file system. This is taking hell lot of time.
     }
     public void CreateFileInDir(ArrayList<String> values){
       //Do some processing here. 
     }
}

I used to call this Utilty class in a standalone class using the following syntax

我曾经使用以下语法在独立类中调用此Utilty类

       Utility.Method1(values); //values is an ArayList.

Now i need to convert the above code into a Multithreaded code. I know how to create a thread by extending Thread class or implementing a Runnable. I have a basic idea about that.

现在我需要将上面的代码转换为多线程代码。我知道如何通过扩展Thread类或实现Runnable来创建线程。我有一个基本的想法。

But what i need to know is should i convert this whole Utilty class to implement Runnable. or should parts of the Utilty class needs to seperated and made as Runnable task. My issue is with the for() loop as these methods are called in loop. if i separate out MethodAccessFileSystem() and make it as a task will this work. If MethodAccessFileSystem() is taking a time then will the JVM automaticaly start another thread if i use a Threadpoolexecutor to schedule a fixed number of threads. Should i need to suspend this method or it is not required or JVM will take care. The main issue is with the For loop.

但我需要知道的是我应该将整个Utilty类转换为实现Runnable。或者应该将Utilty类的某些部分分离并制作为Runnable任务。我的问题是for()循环,因为这些方法是在循环中调用的。如果我分离出MethodAccessFileSystem()并将其作为一个任务,这将是有效的。如果MethodAccessFileSystem()需要一段时间,那么如果我使用Threadpoolexecutor来安排固定数量的线程,那么JVM会自动启动另一个线程。我是否需要暂停此方法或不需要它或JVM会照顾。主要问题是For循环。

At the end what i need is that the Utility class should be Multithreaded and the call to method should be the same as the above.

最后我需要的是Utility类应该是多线程的,并且对方法的调用应该与上面相同。

     Utility.Method1(values); //values is an ArayList.

I am thinking as to how i can implement that. Can you please help me with this and provide your suggestions and feedback on the design changes that need to be made.

我在想如何实现这一点。能否帮助我解决这个问题,并就需要进行的设计变更提供建议和反馈。

Thanks Vikeng

2 个解决方案

#1


0  

From your class According to me the chunk of work which fits in Parallelism principle is below loop.

从你的班级来看,根据我的说法,适合并行性原理的工作块是在循环之下。

// do some processing
    for (int i = 0; i < values.size(); i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                ArrayList<String> details = MethodAccessFileSystem();
                CreateFileInDir(details);
            }
        });

    }

#2


0  

Before you make the change make sure that multiple threads will help. Run the method and as best you can check CPU and disk i/o activity. Also check to see if there's any garbage collection going on.

在进行更改之前,请确保多个线程有所帮助。运行该方法,您可以检查CPU和磁盘I / O活动。还要检查是否有垃圾收集正在进行中。

If any of those conditions exist then adding threads really won't help. You'll have to address that specific condition in order to get any throughput improvements.

如果存在任何这些条件,那么添加线程确实无济于事。您必须解决该特定条件才能获得任何吞吐量改进。

Having said that the trick to making the code thread safe is to not have any instance variables on the class that are used to hold state during the method execution. For each existing instance variable, you need to decide whether to make it a local variable declared within the method or a method parameter.

已经说过使代码线程安全的技巧是在类中没有用于在方法执行期间保持状态的任何实例变量。对于每个现有的实例变量,您需要决定是否将其作为在方法或方法参数中声明的局部变量。

#1


0  

From your class According to me the chunk of work which fits in Parallelism principle is below loop.

从你的班级来看,根据我的说法,适合并行性原理的工作块是在循环之下。

// do some processing
    for (int i = 0; i < values.size(); i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                ArrayList<String> details = MethodAccessFileSystem();
                CreateFileInDir(details);
            }
        });

    }

#2


0  

Before you make the change make sure that multiple threads will help. Run the method and as best you can check CPU and disk i/o activity. Also check to see if there's any garbage collection going on.

在进行更改之前,请确保多个线程有所帮助。运行该方法,您可以检查CPU和磁盘I / O活动。还要检查是否有垃圾收集正在进行中。

If any of those conditions exist then adding threads really won't help. You'll have to address that specific condition in order to get any throughput improvements.

如果存在任何这些条件,那么添加线程确实无济于事。您必须解决该特定条件才能获得任何吞吐量改进。

Having said that the trick to making the code thread safe is to not have any instance variables on the class that are used to hold state during the method execution. For each existing instance variable, you need to decide whether to make it a local variable declared within the method or a method parameter.

已经说过使代码线程安全的技巧是在类中没有用于在方法执行期间保持状态的任何实例变量。对于每个现有的实例变量,您需要决定是否将其作为在方法或方法参数中声明的局部变量。