<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # ReentrantLock ## lock用法 ReentrantLock,意思是“可重入鎖”。ReentrantLock是唯一實現了Lock接口的類,并且ReentrantLock提供了更多的方法。下面通過一些實例看具體看一下如何使用ReentrantLock。 例子1,lock()的正確使用方法 ~~~ package testThread; import java.util.ArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class TestThread { private ArrayList<Integer> arrayList = new ArrayList<Integer>(); public static void main(String[] args) { final TestThread testThread = new TestThread(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); } private void insert(Thread thread) { //注意這個地方 Lock lock = new ReentrantLock(); lock.lock(); try { System.out.println(thread.getName() + "得到了鎖"); for (int i = 0; i < 5; i++) { arrayList.add(i); } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(thread.getName() + "釋放鎖"); lock.unlock(); } } } ~~~ 各位朋友先想一下這段代碼的輸出結果是什么? ~~~ Thread-0得到了鎖 Thread-1得到了鎖 Thread-1釋放鎖 Thread-0釋放鎖 ~~~ 也許有朋友會問,怎么會輸出這個結果?第二個線程怎么會在第一個線程釋放鎖之前得到了鎖?原因在于,在insert方法中的lock變量是局部變量,每個線程執行該方法時都會保存一個副本,那么理所當然每個線程執行到lock.lock()處獲取的是不同的鎖,所以就不會發生沖突。 知道了原因改起來就比較容易了,只需要將lock聲明為類的屬性即可 ~~~ package testThread; import java.util.ArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class TestThread { private ArrayList<Integer> arrayList = new ArrayList<Integer>(); private Lock lock = new ReentrantLock(); public static void main(String[] args) { final TestThread testThread = new TestThread(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); } private void insert(Thread thread) { lock.lock(); try { System.out.println(thread.getName() + "得到了鎖"); for (int i = 0; i < 5; i++) { arrayList.add(i); } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(thread.getName() + "釋放鎖"); lock.unlock(); } } } ~~~ 這樣就是正確地使用Lock的方法了 ## tryLock ~~~ package testThread; import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import java.util.ArrayList; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class TestThread { private ArrayList<Integer> arrayList = new ArrayList<Integer>(); private Lock lock = new ReentrantLock(); public static void main(String[] args) { final TestThread testThread = new TestThread(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); new Thread() { public void run() { testThread.insert(Thread.currentThread()); } }.start(); } private void insert(Thread thread) { if (lock.tryLock()) { try { System.out.println(thread.getName() + "得到了鎖"); for (int i = 0; i < 5; i++) { arrayList.add(i); } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(thread.getName() + "釋放鎖"); lock.unlock(); } } else { System.out.println(thread.getName() + "獲取鎖失敗"); } } } ~~~ 輸出結果 ~~~ Thread-0得到了鎖 Thread-0釋放鎖 Thread-1得到了鎖 Thread-1釋放鎖 ~~~ ## lockInterruptibly lockInterruptibly()響應中斷的使用方法: ~~~ public class Test { private Lock lock = new ReentrantLock(); public static void main(String[] args) { Test test = new Test(); MyThread thread1 = new MyThread(test); MyThread thread2 = new MyThread(test); thread1.start(); thread2.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } //調用中斷方法來測試能否中斷等待中的線程 thread2.interrupt(); } public void insert(Thread thread) throws InterruptedException{ lock.lockInterruptibly(); //注意,如果需要正確中斷等待鎖的線程,必須將獲取鎖放在外面,然后將InterruptedException拋出 try { System.out.println(thread.getName()+"得到了鎖"); long startTime = System.currentTimeMillis(); for( ; ;) { if(System.currentTimeMillis() - startTime >= Integer.MAX_VALUE) break; //插入數據 } } finally { System.out.println(Thread.currentThread().getName()+"執行finally"); lock.unlock(); System.out.println(thread.getName()+"釋放了鎖"); } } } class MyThread extends Thread { private Test test = null; public MyThread(Test test) { this.test = test; } @Override public void run() { try { test.insert(Thread.currentThread()); } catch (InterruptedException e) { System.out.println(Thread.currentThread().getName()+"被中斷"); } } } ~~~ 運行之后,發現thread2能夠被正確中斷 ## ReadWriteLock ReadWriteLock也是一個接口,在它里面只定義了兩個方法
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看