java并发编程知识点

JAVA并发编程的基础篇知识:

Excutor接口类:

子接口:

实现类:

此接口不要求一定异步(在单独的线程中)执行,采用这个接口的任务也可以在调用任务的线程中直接,同步地去进行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//不严格要求异步执行
public class LearningExecutor implements Executor {
@Override
public void execute(Runnable command) {
new Thread(command).start();
}
/**
*分别打印出来
* Main
* Thread-0
* Thread-1
*/
public static void main(String[] args) {
LearningExecutor learningExecutor=new LearningExecutor();
System.out.println(Thread.currentThread().getName());
learningExecutor.execute(()->{
System.out.println(Thread.currentThread().getName());
});
learningExecutor.execute(()->{
System.out.println(Thread.currentThread().getName());
});
}
}

一个复合执行器代码的实现:

许多 Executor 实现对任务的调度方式和时间施加某种限制。下面的执行器将任务提交序列化到第二个执行器

  • 它的作用是将多个提交的任务按顺序串行地执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class SerialExecutor implements Executor {
//提交的线程任务队列
final Queue<Runnable> tasks=new ArrayDeque<>();
//不允许改变的Executor
final Executor executor;

Runnable activie;

public SerialExecutor(Executor executor) {
this.executor = executor;
}

@Override
public synchronized void execute(Runnable r){
tasks.add(()->{
try{
r.run();
}finally {
scheduleNext();
}
});
if(activie==null){
scheduleNext();
}
}
//检测到队列不为空的话,执行加锁线程将其添加到可运行的工作队列中
protected synchronized void scheduleNext(){
if((activie= tasks.poll())!=null){
executor.execute(activie);
}
}
}


分析:

下面是针对上方内容的一个分析

以下是代码的运行机制:

  1. 初始化:

    • SerialExecutor 包含一个任务队列 tasks,底层委托的 executor(用于实际执行任务),以及一个当前正在运行的任务 active
    • 构造函数中传入一个底层 Executor 实例,任务会最终通过这个 Executor 执行。
  2. 提交任务(execute 方法):

    • 每次调用

      1
      execute

      方法时:

      • 新任务 r 会被包装为一个匿名任务,加入 tasks 队列。
      • 包装后的任务在运行时,会先执行原任务 r.run(),然后调用 scheduleNext() 来触发下一个任务。
      • 如果当前没有正在运行的任务(active == null),则调用 scheduleNext(),开始执行队列中的第一个任务。
  3. 调度下一个任务(scheduleNext 方法):

    • tasks 队列中取出下一个任务(poll 方法),将其设置为当前活跃任务 active
    • 将这个任务提交给底层的 executor
    • 如果队列为空,则 active 被置为 null,表示所有任务已完成。

java并发编程知识点
http://example.com/2025/01/03/interview/JUC/OverViewJUC/
作者
GENCO
发布于
2025年1月3日
许可协议