溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中怎么利用同步鎖實現(xiàn)多線程

發(fā)布時間:2021-06-26 17:00:22 來源:億速云 閱讀:156 作者:Leah 欄目:移動開發(fā)

Android中怎么利用同步鎖實現(xiàn)多線程,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

一、同步機制關(guān)鍵字synchronized

對于Java來說,最常用的同步機制就是synchronized關(guān)鍵字,他是一種基于語言的粗略鎖,能夠作用于對象、函數(shù)、class。每個對象都只有一個鎖,誰能夠拿到這個鎖誰就有訪問權(quán)限。當(dāng)synchronized作用于函數(shù)時,實際上鎖的也是對象,鎖定的對象就是該函數(shù)所在類的對象。而synchronized作用于class時則是鎖的這個Class類,并非具體對象。

public class SynchronizedClass {
  public synchronized void syncMethod(){
    //代碼
  }

  public void syncThis(){
    synchronized (this){
      //代碼
    }
  }

  public void syncClassMethod(){
    synchronized (SynchronizedClass.class){
      //代碼
    }
  }

  public synchronized static void syncStaticMethod(){
    //代碼
  }
}

上面演示了同步方法、同步塊、同步class對象、同步靜態(tài)方法。前2種鎖的是對象,而后兩種鎖的是class對象。對于class對象來說,它的作用是防止多個線程同時訪問添加了synchronized鎖的代碼塊,而synchronized作用于引用對象是防止其他線程訪問同一個對象中synchronized代碼塊或者函數(shù)。

二、顯示鎖———-ReentrankLock和Condition

ReentrankLock 和內(nèi)置鎖synchronized相比,實現(xiàn)了相同的語義,但是更具有更高的靈活性。

(1)獲得和釋放的靈活性。
(2)輪訓(xùn)鎖和定時鎖。
(3)公平性。

基本操作:

lock(): 獲取鎖

tryLock(): 嘗試獲取鎖

tryLock(long timeout,TimeUnit unit): 嘗試獲取鎖,如果到了指定的時間還獲取不到,那么超時。

unlock(): 釋放鎖

newCondition(): 獲取鎖的 Condition

使用ReentrantLock的一般組合是 lock、tryLock、與unLock成對出現(xiàn),需要注意的是,千萬不要忘記調(diào)用unlock來釋放鎖,負責(zé)可能引發(fā)死鎖的問題。ReentrantLock的常用形式如下所示:

public class ReentrantLockDemo {
  Lock lock = new ReentrantLock();

  public void doSth(){
    lock.lock();
    try {
      //執(zhí)行某些操作
    }finally {
      lock.unlock();
    }
  }
}

需要注意的是,lock必須在finally開中釋放,否則,如果受保護的代碼拋出異常,鎖就可能永遠得不到釋放??!

ReentrantLock類中還有一個重要的函數(shù)newCondition(),該函數(shù)用戶獲取Lock()上的一個條件,也就是說Condition與Lock綁定。Condition用于實現(xiàn)線程間的通信,他是為了解決Object.wait(),nofity(),nofityAll() 難以使用的問題。
Condition的方法如下:

await() : 線程等待

await(int time,TimeUnit unit) 線程等待特定的時間,超過的時間則為超時。

signal() 隨機喚醒某個等待線程

signal() 喚醒所有等待中的線程

示例代碼:

public class MyArrayBlockingQueue<T> {

//  數(shù)據(jù)數(shù)組
  private final T[] items;

  private final Lock lock = new ReentrantLock();

  private Condition notFull = lock.newCondition();
  private Condition notEmpty = lock.newCondition() ;

//  頭部索引
  private int head;
//  尾部索引
  private int tail ;
//  數(shù)據(jù)的個數(shù)
  private int count;

  public MyArrayBlockingQueue(int maxSize) {
    items = (T[]) new Object[maxSize];
  }

  public MyArrayBlockingQueue(){
    this(10);
  }

  public void put(T t){
    lock.lock();
    try {
      while(count == getCapacity()){
        System.out.println("數(shù)據(jù)已滿,等待");
        notFull.await();
      }
      items[tail] =t ;
      if(++tail ==getCapacity()){
        tail = 0;
      }
      ++count;
      notEmpty.signalAll();//喚醒等待數(shù)據(jù)的線程
    } catch (InterruptedException e) {
      e.printStackTrace();
    }finally {
      lock.unlock();
    }
  }

  public int getCapacity(){
    return items.length ;
  }

  public T take(){
    lock.lock();
    try {
      while(count ==0){
        System.out.println("還沒有數(shù)據(jù),等待");
        //哪個線程調(diào)用await()則阻塞哪個線程
        notEmpty.await();
      }
      T ret = items[head];
      items[head] = null ;
      if(++head == getCapacity()){
        head =0 ;
      }
      --count;
      notFull.signalAll();
      return ret ;
    } catch (InterruptedException e) {
      e.printStackTrace();
    }finally {
      lock.unlock();
    }
    return null ;
  }

  public int size(){
    lock.lock();
    try {
      return count;
    }finally {
      lock.unlock();
    }
  }

  public static void main(String[] args){
    MyArrayBlockingQueue<Integer> aQueue = new MyArrayBlockingQueue<>();
    aQueue.put(3);
    aQueue.put(24);
    for(int i=0;i<5;i++){
      System.out.println(aQueue.take());
    }

    System.out.println("結(jié)束");
  }
}

執(zhí)行結(jié)果:

3
24
還沒有數(shù)據(jù),等待

三、信號量 Semaphore

Semaphore是一個計數(shù)信號量,它的本質(zhì)是一個“共享鎖”。信號量維護了一個信號量許可集,線程可以通過調(diào)用acquire()來獲取信號量的許可。當(dāng)信號量中有可用的許可時,線程能獲取該許可;否則線程必須等待,直到可用的許可為止。線程可以通過release()來釋放它所持有的信號量許可。

示例:

public class SemaphoreTest {
  public static void main(String[] args){
    final ExecutorService executorService = Executors.newFixedThreadPool(3);
    final Semaphore semaphore = new Semaphore(3);
    List<Future> futures = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      Future<?> submit = executorService.submit(new Runnable() {
        @Override
        public void run() {
          try {
            semaphore.acquire();
            System.out.println(" 剩余許可: " + semaphore.availablePermits());
            Thread.sleep(3000);
            semaphore.release();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      });
      futures.add(submit);
    }
  }
}

看完上述內(nèi)容,你們掌握Android中怎么利用同步鎖實現(xiàn)多線程的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI