JDBC第四天

來源:互聯網
上載者:User

單例模式:
 遵循單例模式的類,只能擷取唯一的一個對象
 實現:無參構造私人,提供共有的擷取唯一對象的方法
 JdbcUtil中getConnection() :Connection遵循單例模式
 if(conn == null){
  ....
  conn = DriverManager.getConnection(....);
 }
 return conn;
equals:
 public boolean equals(Object o){
  if(o == null){
   return false;
  }
  if(o == this)
   return true;
  if(!(o instanceof Person))
   return false;
  Person p = (Person)o;
  if(this.get***().equals(p.get***())...)
   return true;
  return false;
 }

Connection對象遵循單例模式,在多線程並發環境下,可能會出現資料不一致問題:
解決:
 使用synchronized控制線程並發
 synchronized可以修飾方法,和代碼塊
 修飾代碼塊時,需要執行代碼塊中的多線程爭搶的資源(臨界資源)
 使用synchronized注意:
  1 加鎖的粒度,粒度越大,效率越低,安全性越高
       粒度越小,效率越高,安全性越低
  2 臨界資源的確定
  3 線程之間通訊: wait/notify/notifyAll
   死結
線程局部變數:
 ThreadLocal
 將臨界資源複製,給每個線程一個自己獨立的副本資源
 實現:
  set(對象副本):
   給當前的線程一個資源的副本
  get() :資源副本
   擷取當前線程中所綁定的資源副本
 
   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.