Java background thread: low priorty

373
August 20, 2017, at 2:15 PM

I have a web-app which has a background thread. This thread, which is a Executors.newSingleThreadExecutor is given tasks that are considered low priority: I don't care when they get done. Often the submitted tasks are a type of logging to a remote DB.

I have read many times that one should not change the actual priority of a thread using Thread.setPriority. That being the case, I don't want my background tasks to interfere with more important tasks of my server. What strategies can be used?

Here is what I am thinking about:

I am considering putting Thread.sleep(1) or yeild() at the beginning of the run() method of the background tasks. This would allow other threads to jump ahead if they have something to do at that moment. But, this seems hackish. Suggestions?

Answer 1

Since the executor is single-threaded it can only occupy one core. As long as your server has N cores those background tasks will never take up more than 1/N of the server load, assuming they do not spawn new threads or dispatch to other thread pools on their own.

Rent Charter Buses Company
READ ALSO
How can I optimize my 2d java game rendering?

How can I optimize my 2d java game rendering?

My problem is, I have a game with a random generated map and it has only 30-40 fps because of the number of blocks(You can imagine my game like a 2d minecraft)

387
how to know when a select finds 0 rows mysql, getFetchSize() not working

how to know when a select finds 0 rows mysql, getFetchSize() not working

I am trying to make a username only register if that name is not taken, using JDBC connection and checking on SQL DatabaseI have the code that checks for the SELECT * FROM user WHERE username = 'jessica';

307
java getting stack overflow-program is not recursive

java getting stack overflow-program is not recursive

I have been working on my "Random Trivia" program and I keep getting stack overflow with NO recursiveness, at least I cannot find any, here is the code to Guijava, the main class(and only class)

221