Parallel file processing in Scala

后端 未结 6 713
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 03:17

Suppose I need to process files in a given folder in parallel. In Java I would create a FolderReader thread to read file names from the folder and a pool of

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 03:46

    Ideally you should use two actors. One for reading the list of files, and one for actually reading the file.

    You start the process by simply sending a single "start" message to the first actor. The actor can then read the list of files, and send a message to the second actor. The second actor then reads the file and processes the contents.

    Having multiple actors, which might seem complicated, is actually a good thing in the sense that you have a bunch of objects communicating with eachother, like in a theoretical OO system.

    Edit: you REALLY shouldn't be doing doing concurrent reading of a single file.

提交回复
热议问题