Skip to content
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

Replace usage of Threads and priorities with thread pool #2304

Merged
merged 17 commits into from
Dec 2, 2016

Conversation

lenhard
Copy link
Member

@lenhard lenhard commented Nov 21, 2016

The current priority-based thread handling in JabRef is bad for two reasons:

  • Using and managing threads directly is bad practice. Since Java 1.6, there is ExecutorServices for this.
  • Relying on thread priorities is highly JVM-dependent and not portable. It is quite pointless for us to do this.

This PR replaces the manual thread-handling with a second thread pool for all threads that were low-priority before. The main (only) difference to other JabRef threads is that low-priority threads sometimes run forever and need to be interrupted when JabRef should shut down. The new structure keeps this.

  • Manually tested changed features in running JabRef

@lenhard lenhard added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Nov 21, 2016
Copy link
Member

@Siedlerchr Siedlerchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me, however I have no practical experience with that ExecutorService. So I can't say anyhting about that

@@ -61,8 +61,7 @@ public static void startBackgroundTasks() {
Globals.streamEavesdropper = StreamEavesdropper.eavesdropOnSystem();

Globals.fileUpdateMonitor = new FileUpdateMonitor();
JabRefExecutorService.INSTANCE.executeWithLowPriorityInOwnThread(Globals.fileUpdateMonitor,
"FileUpdateMonitor");
JabRefExecutorService.INSTANCE.executeInterruptableTask(Globals.fileUpdateMonitor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really liked the fact that a single thread was running in the background and I could find this thread when looking at the thread dump during a debug session. Maybe use a single threaded executor service just for this file update monitor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a problem. Maybe it makes sense for the specific threads, which are very prominent. Using an executor makes lifecycle management a little nicer at least.

I have to look into the remote thread as well, I guess it makes sense for that one, too.

@lenhard lenhard removed the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Nov 21, 2016
@lenhard lenhard mentioned this pull request Nov 21, 2016
6 tasks
@lenhard
Copy link
Member Author

lenhard commented Nov 22, 2016

So, what started out as a simple improvement ran into some issues. This is all due to the, in my point of view, quite unfortunate implementation of the remote functionality. This is heavily tied to being managed in a thread specifically and the lifecycle heavily depends on that. I was not successful in refactoring it towards a runnable within a reasonable amount of time and reverted that part of the PR.

The rest is no problem: Also the former low-priority threads are now managed in a thread pool as opposed to a custom thread list and in case someone ever wants to do a manual prioritization it would be a one-liner to add it back. Moreover, threads can be named and are named as before. The remote thread is kept as is and also managed by JabRefExecutorService, but with a little stricter requirements: There will never be more than one thread of this type in a JabRef instance, which was not ensured before.

I have installed the compiled version from builds.jabref, played around a little, and everything seems fine. I would appreciate it if someone else would also do some testing (no visible error should happen and JabRef should terminate in an orderly fashion) and then this is good to go from my side.

@lenhard lenhard added type: enhancement status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Nov 23, 2016

public Thread thread;
private Runnable task;
Copy link
Contributor

@simonharrer simonharrer Nov 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final for both fields

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

@simonharrer
Copy link
Contributor

I do remember the pain with RemoteThread. Hm, would something like this work? https://techblog.bozho.net/interrupting-executor-tasks/

@lenhard
Copy link
Member Author

lenhard commented Nov 26, 2016

I would just leave the RemoteThread as it is in this PR now. The problem is that the whole remote functionality is dependent on this being a RemoteThread and it also depends on various lifecycle methods of the Thread class itself (calls for Thread.State, etc.). This just won't work as soon as you use an ExecutorService and changing it would mean rewriting the remote functionality. Purely for a refactoring, this is not worth it imho.

@lenhard
Copy link
Member Author

lenhard commented Dec 2, 2016

We checked this in the devcall and I will merge it now. The build breaks at the moment because of the BibLatexEntryTypes, but that is also the case for master, so it does not prevent merging.

@lenhard lenhard merged commit a23245f into master Dec 2, 2016
@lenhard lenhard deleted the improve-threading branch December 2, 2016 12:35
Siedlerchr added a commit that referenced this pull request Dec 2, 2016
* upstream/master:
  Ignore failing test
  Replace usage of Threads and priorities with thread pool (#2304)
  Class variable declarations and method declarations are now separated by one line
  Disable joining of wrapped lines
  Installer Code Signing #1879 (#2320)
  Add bibtex key deviation check (#2328)
  Update mockito-core (2.2.21 -> 2.2.26) and wiremock (2.3.1 -> 2.4.1)
  Fix opening of preference dialog with Java 9 (#2329)
  Add longer explanation for ID-based entry generation. (#2330)
  Add DOI integrity check (#2327)
  New strings translated (#2325)
  Fix exporting via commandline in no gui mode (#2316)
  Cleanup EMACS code (#2317)
  Update mockito-core from 2.2.15 to 2.2.21
  Fix typo in comment
  Updated JabRef_tr.properties (#2315)

# Conflicts:
#	CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers type: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants