Quick revision
The short answer
Setting MAX_PRIORITY is unlikely to help, because thread priority is a weak hint that HotSpot on Linux ignores by default, and because the job's problem is not CPU scheduling but shared capacity: job tasks queue behind request tasks in the same pool. We should give the job its own bounded executor so it has guaranteed threads, or limit request work during the job window, or run the job on separate instances, and first measure where the job spends its time, since it may be waiting on the database rather than on CPU.
What to remember
In this answer, we saw that MAX_PRIORITY is unlikely to help, because priorities are ignored by default on Linux and because the job's tasks wait for pool threads, which no thread priority affects. The real problem is shared capacity, so we measured queue wait, thread activity and database time first, and then chose isolation: a dedicated bounded executor, limiting request work during the window, or running the job on separate instances, together with fixing any slow queries we found.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Setting MAX_PRIORITY is unlikely to help, because thread priority is a weak hint that HotSpot on Linux ignores by default, and because the job's problem is not CPU scheduling but shared capacity: job tasks queue behind request tasks in the same pool. We should give the job its own bounded executor so it has guaranteed threads, or limit request work during the job window, or run the job on separate instances, and first measure where the job spends its time, since it may be waiting on the database rather than on CPU.
Why this question is asked
Batch work and online traffic sharing resources is one of the most common capacity problems in real systems, and a proposal like raise the priority sounds reasonable to someone who has not looked at how threads are scheduled. Interviewers use this scenario to see whether we can turn a vague performance complaint into a diagnosis, and whether we know which levers actually work. In this answer, we will explain why priority is the wrong lever, find the real bottleneck in a shared pool, measure before changing anything, and then compare three fixes with their costs.
1Why MAX_PRIORITY Is Unlikely to Work
A Java thread priority, a number from 1 to 10, is a hint to the operating system scheduler. On Linux, which is where most services run, HotSpot by default does not pass it to the OS at all, so every thread keeps the same OS priority whatever we set. Changing that requires special JVM flags and root privileges. Even where priorities are honoured, they only matter when threads compete for a CPU core, and they decide which ready thread runs next, not which task gets a thread.
And there is a more basic problem. A thread pool's threads are shared: one thread runs a job task, then a request task, then another job task. Raising the priority of the job's thread is not even possible, because the job does not have threads of its own. Setting priority inside the task would change the pool thread for whatever runs on it next, which is worse.
2Finding the Real Bottleneck in the Shared Pool
Suppose the pool has 50 threads and a queue. During the night, request traffic is lower but not zero, and the job submits 200,000 reconciliation tasks. Each job task waits in the same queue as request tasks. When a burst of requests arrives, the queue fills with request tasks, and job tasks wait behind them. When the job submits many tasks, requests wait behind them too, so latency also suffers.
So the two kinds of work are competing for pool threads, not for CPU. Priority acts on CPU scheduling of threads that already exist and are running; it does nothing for a task waiting in a queue for a free thread.
- Job submits a taskinto the shared pool's queue
- Is a pool thread free?YesTask startsNoTask waits behind request tasks
- Does the task need CPU or the database?YesCPU: scheduler decides, priority ignored on LinuxNoDatabase: waits for a connection and a query
- Task completesnext queued task, job or request
4 steps in order. Play them, or step through them one at a time.
Priority could only matter in the CPU branch, and on Linux it does not.
3Measure Before Changing Anything
Before choosing a fix, we find out where the job's time goes. The pool's metrics show the queue length and how long tasks wait before starting; if job tasks wait minutes in the queue, the pool is the bottleneck. A profile or a few thread dumps during the job show what running job threads are doing: if most are in socket reads inside the JDBC driver, the job is waiting on the database. Database metrics show slow queries, lock waits and connection pool usage.
Notice that in this example most of the time is in the database. If that is what we find, even a dedicated pool will only help partly; the fix may be an index, batching the queries, or reading in larger chunks. Measuring first keeps us from fixing the wrong thing.
4Fix 1: Give the Job Its Own Bounded Executor
The most direct fix is isolation, sometimes called the bulkhead pattern. The job gets its own executor with a fixed number of threads, so it always has that capacity and request bursts cannot take it away. The request pool keeps its own threads too, so the job cannot slow down users.
The size is chosen from what the database can handle: if the connection pool has 40 connections and requests need about 20 at night, 16 job threads leave headroom. The job's database connections should ideally come from a separate pool too, otherwise the competition just moves to connections.
5Fix 2 and Fix 3: Limit Request Work or Separate Instances
If the job must use more capacity during its window, we can deliberately limit request concurrency at night, for example with a semaphore or a smaller limit on the request pool, accepting slightly higher request latency in exchange for a predictable job. This is a business trade-off and should be agreed with whoever owns the latency target.
The strongest isolation is to run the job somewhere else: a separate deployment, a Kubernetes CronJob or a dedicated instance, ideally against a read replica if the reconciliation only reads. Then the job and the requests share nothing in the JVM, and we can size each independently. The cost is more infrastructure and making sure the job's writes still go to the primary.
| Option | Effect | Cost |
|---|---|---|
| Thread priority MAX | Usually none on Linux | False confidence |
| Dedicated bounded executor | Guaranteed threads for the job | Small: one more pool to size |
| Limit requests in job window | More capacity for the job | Higher night latency, needs agreement |
| Separate instances or replica | Full isolation | More infrastructure |
| Fix the slow query | Job needs far less time | Depends on what we find |
6Follow-up questions
Could we use a priority queue in the pool instead?
A PriorityBlockingQueue lets the pool pick job tasks before request tasks, which does change ordering, unlike thread priority. But it gives the job priority over users and can starve requests during the window. Isolation with a dedicated pool is usually easier to reason about.
What if the job and the requests use the same database connection pool?
Then isolating threads only moves the competition to connections. Give the job its own small connection pool, or a read replica, sized so that the database as a whole stays within its limits.
How would you know the fix worked?
Track the job's end time and the request latency percentiles during the window, before and after. The job should finish with a stable margin before morning while request p99 latency stays within its target.
7Conclusion
In this answer, we saw that MAX_PRIORITY is unlikely to help, because priorities are ignored by default on Linux and because the job's tasks wait for pool threads, which no thread priority affects. The real problem is shared capacity, so we measured queue wait, thread activity and database time first, and then chose isolation: a dedicated bounded executor, limiting request work during the window, or running the job on separate instances, together with fixing any slow queries we found.
Open with
I'd explain that raising the priority almost certainly won't change anything. On Linux, HotSpot ignores Java thread priorities by default, it needs special flags and root to map them, and even where they work they only affect which running thread gets the CPU.
Then, if they want more
Our problem is different. The job's tasks and the request tasks share one pool, so job tasks wait in the queue for a free thread, and a request burst can take all the threads. Also, the pool's threads are shared, so there's no job thread to raise the priority of.
Before changing anything I'd measure: queue wait and run time for job tasks, a few thread dumps during the job to see whether the threads are on CPU or in JDBC socket reads, and database metrics. Quite often the job is actually waiting on a slow query, and then the fix is an index or batching.
For the capacity part, my first choice is to give the job its own bounded executor, sized to what the database can handle, with its own connection pool if possible, so it has guaranteed capacity and can't hurt users.
Alternatives are limiting request concurrency during the job window, which is a business trade-off, or moving the job to separate instances or a read replica for full isolation. Then I'd check the job finishes with margin while request latency stays within target.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A nightly reconciliation job shares a thread pool with request processing and sometimes...": https://scrivio-interview-library.netlify.app/java/threads/d7bd1d435f/