線程同步--悲觀鎖,線程同步--

來源:互聯網
上載者:User

線程同步--悲觀鎖,線程同步--

在做資料庫訪問的時候,遇到了這樣的問題:兩個線程同時訪問同一對象中的方法,那麼就可能會引發資料不一致的問題,那麼我們需要做的,就是加上鎖。


第一種方案:Synchronized


Java中用來給對象和方法或者代碼加鎖的,當他鎖定一個方法或者一個代碼塊的時候,同一時刻最多隻有一個線程執行該代碼。當兩個並發線程訪問同一個對象object中的這個加鎖同步代碼塊時,一個時間內只有一個線程得到,另一個線程必須等待當前線程執行完這個代碼塊以後才能執行該代碼塊。


用法:Synchronized 加入方法上和Synchronized(this)兩種寫法含義是一樣的,都是對對象加鎖。


實現方法:


<span style="font-size:18px;"><p>public staticsynchronized int  generatate(StringtableName){//使用資料庫的悲觀鎖for updateString sql ="select value from t_table_id where table_name=?";Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;int value=0;try {conn = DbUtil.getConnection();DbUtil.beginTransaction(conn);pstmt = conn.prepareStatement(sql);pstmt.setString(1,tableName);rs = pstmt.executeQuery();if(!rs.next()){throw new RuntimeException();}value = rs.getInt("value");value++;modifyValueField(conn,tableName,value);}catch(SQLException e) {e.printStackTrace();}finally {DbUtil.close(rs);DbUtil.close(pstmt);DbUtil.close(conn);}return value;}</p></span>

或者我們也可以這樣寫:


<span style="font-size:18px;">public synchronized int  generatate(String tableName){synchronized(this){</span>


第二種方案:悲觀鎖


可以使用資料庫中的悲觀鎖,例如:select * from t_table_id where table_name? form update ,悲觀鎖是採用資料庫機制實現的,資料被鎖住之後,其他使用者將無法查看,直到鎖釋放,只有提交或復原事物鎖才會釋放。for update 語句只能放到 select 語句中,因為查詢時把資料鎖住才有意義。


我們封裝資料復原:


<span style="font-size:18px;">public static void beginTransaction(Connection conn){try{if(conn!= null){if(conn.getAutoCommit()){conn.setAutoCommit(false); //手動提交}}}catch(SQLException e){}}</span>

封裝事物的提交:


<span style="font-size:18px;">public static void commitTransaction(Connection conn){try{if(conn!= null){if(!conn.getAutoCommit()){conn.commit();}}}catch(SQLException e){}}</span>

封裝事物復原:


<span style="font-size:18px;">public static void rollbackTracsaction(Connection conn){try{if(conn!= null){if(!conn.getAutoCommit()){conn.rollback();}}}catch(SQLException e){}}</span>

封裝事物重設:


<span style="font-size:18px;">public static void ressetConnection(Connection conn){try{if(conn!= null){if(conn.getAutoCommit()){conn.setAutoCommit(false);}else{conn.setAutoCommit(true);}}}catch(SQLException e){}}</span>


測試使用:


<span style="font-size:18px;">public static int  generatate(String tableName){//使用資料庫的悲觀鎖for updateString sql ="select value from t_table_id where table_name=? for update";Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;int value=0;try {conn = DbUtil.getConnection();DbUtil.beginTransaction(conn);pstmt = conn.prepareStatement(sql);pstmt.setString(1,tableName);rs = pstmt.executeQuery();if(!rs.next()){throw new RuntimeException();}value = rs.getInt("value");value++;modifyValueField(conn,tableName,value);//提交事物DbUtil.commitTransaction(conn);}catch(SQLException e) {e.printStackTrace();//復原事物DbUtil.rollbackTracsaction(conn);}finally {DbUtil.close(rs);DbUtil.close(pstmt);DbUtil.ressetConnection(conn);//儲值到Connection的狀態DbUtil.close(conn);}return value;}</span>

總結:


資料庫中的資料無價,悲觀鎖,有強烈的獨佔和排他的特性,對資料被外界修改保持保守態度.在這個資料處理過程,將資料處於鎖定狀態,這就是悲觀鎖的實現.


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.