線程的同步synchronized,同步synchronized

來源:互聯網
上載者:User

線程的同步synchronized,同步synchronized
一個Java程式的多線程之間可以共用資料。當線程以非同步方式訪問共用資料時,有時候是不安全的或者不和邏輯的。比如賣火車票,同一時刻一個線程在讀取資料,另外一個線程在處理資料,當處理資料的線程沒有等到讀取資料的線程讀取完畢就去處理資料,必然得到錯誤的處理結果。
賣火車票Demo:

class MyThread implements Runnable{private int ticket = 5 ;// 假設一共有5張票public void run(){for(int i=0;i<100;i++){if(ticket>0){// 還有票try{Thread.sleep(300) ;// 加入延遲}catch(InterruptedException e){e.printStackTrace() ;}System.out.println("賣票:ticket = " + ticket-- );}}}};public class SyncDemo01{public static void main(String args[]){MyThread mt = new MyThread() ;// 定義線程對象Thread t1 = new Thread(mt) ;// 定義Thread對象Thread t2 = new Thread(mt) ;// 定義Thread對象Thread t3 = new Thread(mt) ;// 定義Thread對象t1.start() ;t2.start() ;t3.start() ;}};
運行有可能出現以下結果:.......賣票:ticket=1賣票:ticket=0賣票:ticket=-1
Java 程式設計語言提供了一種簡單的機制來防止發生這種覆蓋。每個對象在運行時都有一個關聯的鎖。這個鎖可通過為方法添加關鍵字synchronized 來獲得。同步有兩種方式:同步塊和同步方法
同步塊:對易發生衝突的語句塊加鎖
class MyThread implements Runnable{private int ticket = 5 ;// 假設一共有5張票public void run(){for(int i=0;i<100;i++){synchronized(this){// 要對當前對象進行同步if(ticket>0){// 還有票try{Thread.sleep(300) ;// 加入延遲}catch(InterruptedException e){e.printStackTrace() ;}System.out.println("賣票:ticket = " + ticket-- );}}}}};public class SyncDemo02{public static void main(String args[]){MyThread mt = new MyThread() ;// 定義線程對象Thread t1 = new Thread(mt) ;// 定義Thread對象Thread t2 = new Thread(mt) ;// 定義Thread對象Thread t3 = new Thread(mt) ;// 定義Thread對象t1.start() ;t2.start() ;t3.start() ;}};


同步方法:對易發生衝突的方法塊加鎖
class MyThread implements Runnable{private int ticket = 5 ;// 假設一共有5張票public void run(){for(int i=0;i<100;i++){this.sale() ;// 調用同步方法}}public synchronized void sale(){// 聲明同步方法if(ticket>0){// 還有票try{Thread.sleep(300) ;// 加入延遲}catch(InterruptedException e){e.printStackTrace() ;}System.out.println("賣票:ticket = " + ticket-- );}}};public class SyncDemo03{public static void main(String args[]){MyThread mt = new MyThread() ;// 定義線程對象Thread t1 = new Thread(mt) ;// 定義Thread對象Thread t2 = new Thread(mt) ;// 定義Thread對象Thread t3 = new Thread(mt) ;// 定義Thread對象t1.start() ;t2.start() ;t3.start() ;}};







java中 synchronized同步處理後 為何只有一個線程在運行?

.修改了一下.,,這樣應該沒問題
class TextThread implements Runnable {
private int tickets=20;
public void run()
{
for(int i=0;i<=20;i++) {
go();
}
}
public synchronized void go() {

if(tickets>0) {
try {
Thread.sleep(100);
}
catch(Exception x) {}
System.out.println(Thread.currentThread().getName()+" 售出票 "+tickets--);
}
}
}
 
教JAVA對於線程的同步機制synchronized

1,你首先要知道synchronized是什麼意思:只有當兩個線程對同一個對象進行synchronized同步時,才能產生同步的效果,否則,如果A線程對a對象同步,B線程對b對象同步,這是沒用的,必須A線程和B線程對同一個對象c進行同步。
synchronized如果修飾一個方法,那表示對這個方法所在的對象進行同步,所以如果用synchronized修飾方法,而你卻new了兩個不同的對象,當然不能實現同步了。而如果用synchronized來修飾"a",同樣內容的字串常量,在java裡面是同一個對象,因此就可以實現同步。

2,Thread類的start方法,還是調用的你傳給它的MyThread對象裡面的run方法,儘管有兩個Thread對象,但執行的都是my這個對象的run方法,當然可以同步了
 

聯繫我們

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