多线程编程注意点(持续更新)
生活随笔
收集整理的這篇文章主要介紹了
多线程编程注意点(持续更新)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1.?對象逸出(Escape)
? ? 構造函數中,當前對象還沒初始化完成就暴露this給外部
package com.chstudy.unsafe;import java.util.ArrayList; import java.util.List;public class EventEscape {private final List<Event> listOfEvents;public EventEscape(EventSource source) {source.registerListener(new EventListener() {@Overridepublic void onEvent(Event e) {doSomething(e);}});listOfEvents = new ArrayList<>();}void doSomething(Event e) {listOfEvents.add(e);}interface Event {}interface EventSource {void registerListener(EventListener listener);}interface EventListener {void onEvent(Event event);} }????二、并發使用的類,對成員變量的訪問需要同步
package com.chstudy.unsafe;import javax.servlet.*; import javax.servlet.annotation.WebServlet; import java.io.IOException;@WebServlet(urlPatterns = "/count.do") public class UnsafeCountServlet extends GenericServlet implements Servlet {private Long count = 0L;public Long getCount() {return count;}@Overridepublic void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}count++;res.getWriter().write(count+"");res.flushBuffer();}}三、JVM 參數,打印指定方法的匯編指令
-XX:+UnlockDiagnosticVMOptions -Xcomp -XX:+PrintAssembly -XX:CompileCommand=compileonly,*GoalNotifier.setGoal-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly:開啟JIT反匯編
-Xcomp:讓虛擬機以編譯模式執行代碼,使得JIT編譯可以立即觸發 -XX:CompileCommand=compileonly,*GoalNotifier.setGoal:只反匯編GoalNotifier的setGoal方法
四、
轉載于:https://my.oschina.net/chen1988/blog/1845807
總結
以上是生活随笔為你收集整理的多线程编程注意点(持续更新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 购物商城Web开发第一天
- 下一篇: Scrayp-集成scrapy_redi