MST

星途 面试题库

面试题:Java多线程编程中线程中断机制在复杂任务链中的设计与实现

假设有一个复杂的多线程任务链,每个任务依赖前一个任务的结果,并且其中某些任务可能会被中断。请设计一种线程中断机制,使得当某个任务被中断时,整个任务链能够有序停止,同时确保已完成任务的资源得到合理回收,未执行任务不会启动。请详细描述设计思路、关键代码结构以及涉及到的多线程编程概念。
27.3万 热度难度
编程语言Java

知识考点

AI 面试

面试题答案

一键面试

设计思路

  1. 任务链表示:使用链表或队列来表示任务链,每个任务作为链中的一个节点。这样可以方便地按顺序执行任务,并在任务中断时进行相应处理。
  2. 中断标志:为每个任务设置一个中断标志位,当需要中断某个任务时,设置其标志位。同时,为整个任务链设置一个全局中断标志,用于快速判断是否需要停止整个任务链。
  3. 任务执行逻辑:每个任务在执行过程中,定期检查自己的中断标志和全局中断标志。如果发现标志被设置,则停止当前任务的执行,并清理已使用的资源。
  4. 资源回收:在任务停止时,确保释放所有已分配的资源,如文件句柄、网络连接等。可以通过在任务类中实现closedispose方法来进行资源清理。
  5. 线程协作:使用ConditionSemaphore等机制来协调线程间的通信,确保任务按顺序执行,并且在某个任务中断时,能够通知后续任务停止执行。

关键代码结构(以Java为例)

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Task {
    private Task nextTask;
    private boolean isInterrupted = false;
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public Task(Task nextTask) {
        this.nextTask = nextTask;
    }

    public void execute() {
        lock.lock();
        try {
            while (!isInterrupted) {
                // 执行任务逻辑
                System.out.println("Task is running");
                // 模拟任务执行
                Thread.sleep(1000);
                if (nextTask != null) {
                    nextTask.lock.lock();
                    try {
                        nextTask.condition.signal();
                    } finally {
                        nextTask.lock.unlock();
                    }
                }
                break;
            }
        } catch (InterruptedException e) {
            isInterrupted = true;
            Thread.currentThread().interrupt();
        } finally {
            lock.unlock();
        }
    }

    public void interrupt() {
        lock.lock();
        try {
            isInterrupted = true;
            if (nextTask != null) {
                nextTask.interrupt();
            }
        } finally {
            lock.unlock();
        }
    }
}

class TaskChain {
    private Task head;
    private boolean globalInterrupted = false;

    public TaskChain(Task head) {
        this.head = head;
    }

    public void start() {
        Thread thread = new Thread(() -> {
            Task currentTask = head;
            while (currentTask != null &&!globalInterrupted) {
                currentTask.lock.lock();
                try {
                    currentTask.execute();
                    if (currentTask.isInterrupted) {
                        globalInterrupted = true;
                        break;
                    }
                    currentTask.condition.await();
                } catch (InterruptedException e) {
                    globalInterrupted = true;
                    currentTask.interrupt();
                    Thread.currentThread().interrupt();
                } finally {
                    currentTask.lock.unlock();
                }
                currentTask = currentTask.nextTask;
            }
        });
        thread.start();
    }

    public void interrupt() {
        globalInterrupted = true;
        if (head != null) {
            head.interrupt();
        }
    }
}

涉及到的多线程编程概念

  1. 线程同步:通过LockCondition来实现线程间的同步,确保任务按顺序执行,并且在中断时能够通知其他线程。
  2. 中断处理:为每个任务设置中断标志,在任务执行过程中检查标志,以决定是否停止任务执行。同时,处理InterruptedException,确保线程在被中断时能够正确响应。
  3. 资源管理:在任务中断时,及时释放已分配的资源,避免资源泄漏。
  4. 任务调度:使用链表或队列来管理任务链,按顺序执行任务,并在任务中断时进行相应的调度调整。