文章

对线面试官 今天来聊聊多线程基础

1
2
3
4
5
6
7
8
9
10
// 请求直接交给线程池来处理
public void push(PushParam pushParam) {
  try {
    pushServiceThreadExecutor.submit(() -> {
      handler(pushParam);
    });
  } catch (Exception e) {
    logger.error("pushServiceThreadExecutor error, exception{}:", e);
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
public class UnsafeCountingServlet extends GenericServlet implements Servlet {
    private long count = 0;

    public long getCount() {
        return count;
    }

    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        ++count;
        // To something else...
    }
}

文章以纯面试的角度去讲解,所以有很多的细节是未铺垫的。

文章中写到的处理线程安全的思路每一项技术都可以写出一篇文章,AQS、Synchronized、Atomic…周末肝起来!下周再来给大家安排!

本文由作者按照 CC BY 4.0 进行授权