Adding multi-threading possibility to a single-threaded all-files-in-directory iterator utility function

aliteralmind

I have a function that serially (single-threaded-ly) iterates through a directory of files, changing all tab indentation to three-space indentation.

I'm using it as my first attempt at multi-threading. (Am most of the way through Java Concurrency in Practice...surprised it's eight years old now.)

In order to keep it's current single-threaded functionality, but add in the additional possibility of multi-threading, I'm thinking of changing the function to accept an additional Executor parameter, where the original single-threaded function would now be a call to it, passing in a single threaded executor.

Is this an appropriate way to go about it?

fps

One way is as @Victor Sorokin suggests in his answer: wrap the processing of every file in a Runnable and then either submit to an Executor or just invoke run() from the main thread.

Another possibility is to always do the same wrapping in a Runnable and submit it to an always-given Executor.

Whether processing of each file is executed concurrently or not would depend on the given Executor's implementation.

For parallel processing, you could invoke your function passing it i.e. a ThreadPoolExecutor as an argument, whereas for sequential processing you could pass in a fake Executor, i.e. one that runs submitted tasks in the caller thread:

public class FakeExecutor implements Executor {

    @Override
    public void execute(Runnable task) {
        task.run();
    }
}

I believe this way is the most flexible approach.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modification of Utility Method to Support Multi-Threading

From Dev

How to rename all files in a directory adding a prefix

From Dev

Multi-Threading Function

From Dev

append content of all files in a directory to a single file

From Dev

how to perform function on all files in a directory or just a single file based on args, in python

From Dev

Optimize a multi-utility command to a single-utility command

From Dev

Multi-Threaded program runs slower than single threaded

From Dev

Is Java single-threaded or multi-threaded by default?

From Dev

How to write Kafka consumers - single threaded vs multi threaded

From Dev

Is Java single-threaded or multi-threaded by default?

From Dev

Multi threaded single file writing in Java

From Dev

Single vs Multi-threaded JMS Producer

From Dev

Executing multiple database calls - single or multi threaded?

From Dev

Multi threaded single file writing in Java

From Dev

MATLAB - iterate function on all files in a directory

From Dev

How to rename all files in a directory adding prefix of current unix date

From Dev

How to rename all files in a directory adding prefix of current unix date

From Dev

Adding all cpp files name in a single variable in batch file

From Dev

Synchronized multi-threading vs single thread

From Dev

Why is my multi-threading slower than my single threading?

From Dev

Rewrite any sub-domain and all files to a single directory

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

Apply Linux command recursively to all files in directory of single (.sh) type

From Dev

Adding a directory of files to PATH

From Dev

I Want to execute this function as a thread for all the values in List via multi threading please guide me

From Dev

How to get a multi-threaded dot() function?

From Dev

Random function in multi-threaded c program

From Dev

Multi threaded function already defined in .obj

Related Related

  1. 1

    Modification of Utility Method to Support Multi-Threading

  2. 2

    How to rename all files in a directory adding a prefix

  3. 3

    Multi-Threading Function

  4. 4

    append content of all files in a directory to a single file

  5. 5

    how to perform function on all files in a directory or just a single file based on args, in python

  6. 6

    Optimize a multi-utility command to a single-utility command

  7. 7

    Multi-Threaded program runs slower than single threaded

  8. 8

    Is Java single-threaded or multi-threaded by default?

  9. 9

    How to write Kafka consumers - single threaded vs multi threaded

  10. 10

    Is Java single-threaded or multi-threaded by default?

  11. 11

    Multi threaded single file writing in Java

  12. 12

    Single vs Multi-threaded JMS Producer

  13. 13

    Executing multiple database calls - single or multi threaded?

  14. 14

    Multi threaded single file writing in Java

  15. 15

    MATLAB - iterate function on all files in a directory

  16. 16

    How to rename all files in a directory adding prefix of current unix date

  17. 17

    How to rename all files in a directory adding prefix of current unix date

  18. 18

    Adding all cpp files name in a single variable in batch file

  19. 19

    Synchronized multi-threading vs single thread

  20. 20

    Why is my multi-threading slower than my single threading?

  21. 21

    Rewrite any sub-domain and all files to a single directory

  22. 22

    How can I move all files in subdirectories recursively to a single directory?

  23. 23

    How can I move all files in subdirectories recursively to a single directory?

  24. 24

    Apply Linux command recursively to all files in directory of single (.sh) type

  25. 25

    Adding a directory of files to PATH

  26. 26

    I Want to execute this function as a thread for all the values in List via multi threading please guide me

  27. 27

    How to get a multi-threaded dot() function?

  28. 28

    Random function in multi-threaded c program

  29. 29

    Multi threaded function already defined in .obj

HotTag

Archive