国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java Synchronized 關鍵字

2019-11-14 22:39:52
字體:
來源:轉載
供稿:網友
java Synchronized 關鍵字本文內容
  • Synchronized 關鍵字
  • 示例
  • Synchronized 方法
  • 內部鎖(Intrinsic Locks)和 Synchronization
  • 參考資料
下載 Demo Synchronized 關鍵字

Java 語言提供兩個基本的同步機制:synchronized 方法(synchronized methods )和 synchronized 語句(synchronized statements)。

示例

先大概說一下 Java Synchronized 關鍵字,當它用來修飾一個方法或者一個代碼塊的時候,能夠保證在同一時刻只有一個線程執行該段代碼。

  • 當兩個線程訪問同一個對象 synchronized(this) 代碼塊時,只能有一個線程執行,另一個線程必須等待這個線程執行完后才能執行;
  • 但是,當一個線程訪問一個對象 synchronized(this) 同步代碼塊時,另一個線程仍能訪問該對象的非 synchronized(this) 代碼塊;
  • 尤其是,當一個線程訪問一個對象的一個 synchronized(this) 代碼塊時,其他線程對該對象中其他所有 synchronized(this) 代碼塊的訪問也將被阻塞;
  • 以上規則對其它對象鎖同樣適用。

如下代碼所示:

package cn.db.syncdemo;
public class NewClass {
    /**
     * 同步方法
     */
    public void synchronizedMethod() {
        synchronized (this) {
            int i = 5;
            while (i-- > 0) {
                System.out.PRintln(Thread.currentThread().getName() + " : " + i
                        + " synchronized method");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    System.out.println(e.toString());
                }
            }
        }
    }
    /**
     * 同步方法 2
     */
    public void synchronizedMethod2() {
        synchronized (this) {
            int i = 5;
            while (i-- > 0) {
                System.out.println(Thread.currentThread().getName() + " : " + i
                        + " synchronized method 2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println(e.toString());
                }
            }
        }
    }
    /**
     * 非同步方法
     */
    public void nonSynchronizedMethod() {
        int i = 5;
        while (i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i
                    + " nonSynchronized method");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println(e.toString());
            }
        }
    }
    public static void main(String[] args) {
        final NewClass mClass = new NewClass();
        // t1 和 t2 都要訪問同一個同步方法 synchronizedMethod
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                mClass.synchronizedMethod();
            }
        }, "Thread 1");
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                mClass.synchronizedMethod();
            }
        }, "Thread 2");
        // t3 要訪問另一個同步方法 synchronizedMethod2
        Thread t3 = new Thread(new Runnable() {
            public void run() {
                mClass.synchronizedMethod2();
            }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁城县| 庆云县| 敦化市| 海林市| 博爱县| 龙口市| 明溪县| 青铜峡市| 阳高县| 崇义县| 岑巩县| 海阳市| 仙游县| 新化县| 孟村| 潜江市| 大田县| 芮城县| 晋州市| 宜君县| 铜梁县| 抚松县| 晋中市| 蒙自县| 新巴尔虎右旗| 万山特区| 福建省| 内乡县| 岢岚县| 宁晋县| 扶沟县| 沛县| 锡林浩特市| 淄博市| 伽师县| 开江县| 阿克陶县| 慈利县| 当涂县| 潜山县| 株洲县|