線程同步控制的幾種方式,線程同步幾種

來源:互聯網
上載者:User

線程同步控制的幾種方式,線程同步幾種

在多線程對同一個函數進行調用時,如果不控制號鎖的機制,往往就會產生資料混亂的情況,我們通常在控制線程的同步問題時,通常採用的方法:

1:使用synchronized進行控制訪問。

2:使用ReentrantLock,進行上鎖,方法體結束時進行解鎖,因此得到控制。

下面我分別通過一個執行個體,來詳細說明一下,上鎖的方法,如果去掉鎖機制之後,你會探索資料會出現混亂的情況。

import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class TraditionalThreadSynchronized {       public static void main(String[] args) {   TraditionalThreadSynchronized tt = new TraditionalThreadSynchronized();   tt.init();}              public void init(){       final Outeputer out = new Outeputer();            new Thread(new Runnable() {@Overridepublic void run() {while(true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}out.output2("robin222222222222222");}}}).start();                        new Thread(new Runnable() { @Overridepublic void run() {while(true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}out.output2("Tcent111111111");}}}).start();       }       static class Outeputer{       Lock lock = new ReentrantLock();       public void output(String name){       synchronized (Outeputer.class) {       int len = name.length();           for(int i=0;i<len;i++){           System.out.print(name.charAt(i));           }           System.out.println();}             }       public  void  output2(String name){         //lock.lock();//這裡自行進行測試       int len = name.length();           for(int i=0;i<len;i++){           System.out.print(name.charAt(i));           }           System.out.println();     // lock.unlock();       }       public void  output3(String name){   int len = name.length();       for(int i=0;i<len;i++){       System.out.print(name.charAt(i));       }       System.out.println();     }       }}


線程同步有幾種方法?

wait():使一個線程處於等待狀態,並且釋放所持有的對象的lock。

sleep():使一個正在啟動並執行線程處於睡眠狀態,是一個靜態方法,調用此方法要捕捉InterruptedException異常。

notify():喚醒一個處於等待狀態的線程,注意的是在調用此方法的時候,並不能確切的喚醒某一個等待狀態的線程,而是由JVM確定喚醒哪個線程,而且不是按優先順序。

Allnotity():喚醒所有處入等待狀態的線程,注意並不是給所有喚醒線程一個對象的鎖,而是讓它們競爭。
 
為何要線程同步,說出線程同步的幾種方法

線程有可能和其他線程共用一些資源,比如,記憶體,檔案,資料庫等。
當多個線程同時讀寫同一份共用資源的時候,可能會引起衝突。這時候,我們需要引入線程“同步”機制,即各位線程之間要有個先來後到,不能一窩蜂擠上去搶作一團。
線程同步的真實意思和字面意思恰好相反。線程同步的真實意思,其實是“排隊”:幾個線程之間要排隊,一個一個對共用資源進行操作,而不是同時進行操作。

線程同步的方法
(1)wait():使一個線程處於等待狀態,並且釋放所持有的對象的lock。
(2)sleep():使一個正在啟動並執行線程處於睡眠狀態,是一個靜態方法,調用此方法要捕捉
InterruptedException異常。
(3)notify():喚醒一個處於等待狀態的線程,注意的是在調用此方法的時候,並不能確切的
喚醒某一個等待狀態的線程,而是由JVM確定喚醒哪個線程,而且不是按優先順序。
(4)notityAll ():喚醒所有處入等待狀態的線程,注意並不是給所有喚醒線程一個對象的鎖,
而是讓它們競爭
 

聯繫我們

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