← 返回 sofi 的题目列表Design a Multi-threaded Task Executor with Extensible Task Types (Semaphore-based)
类型:online_judge
Problem: Design and Implement an Extensible Multi-threaded Task Executor (Semaphore-based)
You are given a Java TaskExecutor skeleton and must implement a multi-threaded task executor using Semaphore (and other concurrency primitives if needed), following solid OOD/OOP principles (extensible, maintainable).
1) Task Abstraction
There are multiple task types (multiple Task subclasses) sharing one common interface, e.g.:
MultiplyTask: applies multiplication to the input; requires x threads/permits; does not need to return/notify when finished.
DivideTask: applies division to the input; requires y threads/permits; must return/notify the caller when finished.
Requirement:
Define a common Task interface (or abstract class) so that adding new task types (e.g., AddTask, SqrtTask) does not require major changes to the executor.
2) Methods to Implement in TaskExecutor
Given the following methods (names may match the interview skeleton):
initialization(): initialize internal state and concurrency components (semaphore, thread pool, queue, etc.).
acquire(int permits): acquire enough concurrency resources (permits) before running a task.
execution(Task task, Input input): submit and execute a task, correctly handling permit acquisition/release and concurrency limits.
3) Concurrency and Correctness
The executor has a fixed maximum concurrency capacity (e.g., at most N concurrent thread slots).
Each task must acquire the required number of permits before execution.
Permits must be released after completion.
Ensure thread safety under concurrent submissions.
4) Output/Return
For tasks that must return results (e.g., DivideTask), design the return mechanism: synchronous return, Future/Promise, callback, etc.
For fire-and-forget tasks (e.g., MultiplyTask), support non-blocking submission.
5) What to Provide
Provide:
Key class/interface design (high-level class diagram description is fine)
Threading model (how thread pool/queue/semaphore work together)
Core pseudocode or implementation snippets