Task Scheduler
Priority dispatch, worker quotas, and expiring leases
Ownership and transitions
Priority order and lease ownership are distinct. A task returns to pending exactly at expiry; every reassignment receives a new token, so an old completion cannot finish a newer lease. Valid timed calls expire leases before answering even when the requested action fails. Validate duration before advancing time. A simple scan costs O(tasks) per dispatch/expiry; heaps can improve selection but require stale-entry checks.
Java boundaries
Use the supplied nested records as immutable value carriers. A record is only shallowly immutable: copy every retained mutable input and every outward list/map, including lists inside records. Absence is null; validation failures use IllegalArgumentException, missing-key purchase errors use NoSuchElementException, and invalid transaction boundaries use IllegalStateException. Java primitive types already exclude booleans from numeric inputs. Numeric values use long where signatures say long and all computations must fit signed 64-bit values. Calls in these service exercises are serialized.
A task ID identifies work; a lease token identifies one assignment of that work. Reusing the task ID does not authorize an old completion:
import java.util.*;
class Example {
public static void main(String[] args) {
record Lease(long taskId, long token, long expiresAt) {}
Lease first = new Lease(1, 7, 3);
Lease reassigned = new Lease(1, 8, 6);
assert first.taskId() == reassigned.taskId();
assert first.token() != reassigned.token();
assert first.expiresAt() <= 3;
}
}At timestamp 3 the first lease is expired, and its token cannot finish the reassigned task. Expiration reclaims worker capacity before the next valid timed operation is answered.
Design boundary
Priority ties use numeric IDs rather than lexically sorted ID strings. Unknown-worker dispatch is still a valid timed call and advances time; invalid lease duration does not. This scheduler records ownership and completion, but does not execute task code or provide distributed fencing outside its own state.
Put it into practice
Implement the exercise in a full-screen workspace, then run its tests.