-
Notifications
You must be signed in to change notification settings - Fork 0
The second multithreading task #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import java.io.File; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ForkJoinTask; | ||
| import java.util.concurrent.RecursiveTask; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| public class FileCounterTask extends RecursiveTask<Long> { | ||
|
|
||
| /** | ||
| * Current processed file | ||
| */ | ||
| protected final File mFile; | ||
| /** | ||
| * All documents in folder | ||
| */ | ||
| protected final AtomicLong mDocumentCount; | ||
| /** | ||
| * Directories count | ||
| */ | ||
| protected final AtomicLong mFolderCount; | ||
| FileCounterTask(File file, AtomicLong mDocumentCount, AtomicLong mFolderCount) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the constructor has package visibility? |
||
| this.mFile = file; | ||
| this.mDocumentCount = new AtomicLong(0); | ||
| this.mFolderCount = new AtomicLong(0); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @return files number | ||
| */ | ||
| public AtomicLong documentCount() { | ||
| return mDocumentCount; | ||
| }; | ||
|
|
||
| /** | ||
| * | ||
| * @return directories number | ||
| */ | ||
| public AtomicLong folderCount() { | ||
| return mFolderCount; | ||
| } | ||
|
|
||
| @Override | ||
| protected Long compute() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method deserve to be split up on smaller logical chunks. So you can clearly see the logical structure. |
||
| if (mFile.isFile()) { | ||
| mDocumentCount.incrementAndGet(); | ||
| } else { | ||
| mFolderCount.incrementAndGet(); | ||
| } | ||
|
|
||
| List<ForkJoinTask<Long>> forkJoinTaskList = new ArrayList<>(); | ||
| for(File file: Objects.requireNonNull(mFile.listFiles())) { | ||
| //the task for each File | ||
| forkJoinTaskList.add( | ||
| new FileCounterTask(file, mDocumentCount, mFolderCount).fork()); | ||
| } | ||
|
|
||
| long sum = 0; | ||
|
|
||
| for (ForkJoinTask<Long> forkJoinTask: forkJoinTaskList) { | ||
| sum += forkJoinTask.join(); | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you define the members as
protectedAlso I would recommend to avoid Hungarian notation in Java.
Also that is bad practice according to Clean Code principles.
Instead try to give is a good and meaningful name consists from normal English words.