亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

Java線程使用同步鎖交替執行打印奇數偶數的方法

發布時間:2020-10-01 21:19:59 來源:腳本之家 閱讀:219 作者:boonya 欄目:編程語言

對同一個對象進行多線程操作時,如何保證線程執行結果的一致性?我們需要對線程操作對象加同步鎖。(這是一道面試題)

需求描述

1-20個數字

A線程打印奇數:1,3,5,7,9,11,13,15,17,19

B線程打印偶數:2,4,6,8,10,12,14,16,18,20

C線程在AB兩個線程執行完了之后打印結果:“success”。

線程代碼實現

Num.java

package com.boonya.thread.test;
 
/**
 * @ClassName: Num
 * @Description: TODO(加鎖計算對象)
 * @author: pengjunlin
 * @motto: 學習需要毅力,那就秀毅力
 * @date 2019-01-14 22:47
 */
public class Num {
  int value=1;
}

AThread.java

package com.boonya.thread.test;
 
/**
* @ClassName: AThread
* @Description: TODO(奇數線程)
* @author: pengjunlin
* @motto: 學習需要毅力,那就秀毅力
* @date 2019-01-14 22:26
*/
public class AThread implements Runnable{
 
  Num num;
 
  public AThread(Num num){
    this.num=num;
  }
 
  public void run() {
    while (num.value<20){
      synchronized (num){
        if(num.value%2!=0){
          System.out.println("AThread:"+ num.value);
          num.value++;
        }
      }
    }
 
  }
}

BThread.java

package com.boonya.thread.test;
 
/**
 * @ClassName: BThread
 * @Description: TODO(偶數線程)
 * @author: pengjunlin
 * @motto: 學習需要毅力,那就秀毅力
 * @date 2019-01-14 22:26
 */
public class BThread implements Runnable {
 
    Num num;
 
    public BThread(Num num){
      this.num=num;
    }
 
    public void run() {
      while (num.value<=20){
        synchronized (num){
          if(num.value%2==0){
            System.out.println("BThread:"+ num.value);
            num.value++;
          }
        }
      }
    }
 }

CThread.java

package com.boonya.thread.test;
 
/**
 * @ClassName: CThread
 * @Description: TODO(等待結果線程)
 * @author: pengjunlin
 * @motto: 學習需要毅力,那就秀毅力
 * @date 2019-01-14 22:26
 */
public class CThread implements Runnable {
 
    Num num;
 
    public CThread(Num num){
      this.num=num;
    }
 
    public void run() {
      while (num.value<20){
        try {
          Thread.sleep(1);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("CThread:success!");
    }
  }

MainTest.java

package com.boonya.thread.test;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @ClassName: MainTest
 * @Description: TODO(線程測試)
 * @author: pengjunlin
 * @motto: 學習需要毅力,那就秀毅力
 * @date 2019-01-14 21:54
 */
public class MainTest {
 
  public static void main(String[] args) {
    Num num=new Num();
    Thread a=new Thread(new AThread(num));
    Thread b=new Thread(new BThread(num));
    Thread c=new Thread(new CThread(num));
    a.start();
    b.start();
    c.start();
  }
}

測試結果

"C:\Program Files\Java\jdk1.8.0_121\bin\java" "-javaagent:C:\Users\boonya\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=51911:C:\Users\boonya\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\boonya\Desktop\JavaAlgorithm\target\test-classes;C:\Users\boonya\Desktop\JavaAlgorithm\target\classes" com.boonya.thread.test.MainTest
AThread:1
BThread:2
AThread:3
BThread:4
AThread:5
BThread:6
AThread:7
BThread:8
AThread:9
BThread:10
AThread:11
BThread:12
AThread:13
BThread:14
AThread:15
BThread:16
AThread:17
BThread:18
AThread:19
BThread:20
CThread:success!
 
Process finished with exit code 0

注意:synchronized 作為方法塊使用時需要只能對對象加鎖,不能是常用數據類型。

AB線程改進:使用線程等待和通知

AThread.java

package com.boonya.thread.test;
 
/**
* @ClassName: AThread
* @Description: TODO(奇數線程)
* @author: pengjunlin
* @motto: 學習需要毅力,那就秀毅力
* @date 2019-01-14 22:26
*/
public class AThread implements Runnable{
 
  Num num;
 
  public AThread(Num num){
    this.num=num;
  }
 
  public void run() {
    while (num.value<20){
      synchronized (num){
        if(num.value%2!=0){
          System.out.println("AThread:"+ num.value);
          num.value++;
          //num.notify();
          num.notifyAll();
        }else{
          try {
            num.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
 
  }
}

BThread.java

package com.boonya.thread.test;
 
/**
 * @ClassName: BThread
 * @Description: TODO(偶數線程)
 * @author: pengjunlin
 * @motto: 學習需要毅力,那就秀毅力
 * @date 2019-01-14 22:26
 */
public class BThread implements Runnable {
 
    Num num;
 
    public BThread(Num num){
      this.num=num;
    }
 
    public void run() {
      while (num.value<=20){
        synchronized (num){
          if(num.value%2==0){
            System.out.println("BThread:"+ num.value);
            num.value++;
            //num.notify();
            num.notifyAll();
          }else{
            try {
              num.wait();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
 }

注:線程等待和通知的操作是節省CPU運算的一種方式,避免CPU空轉(線程方法一直不停地跑類似于死循環是很可怕的),如果是要讓線程停下來需要調用線程的wait()。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

璧山县| 吉水县| 青龙| 阿尔山市| 全椒县| 建阳市| 邯郸市| 台中市| 丹凤县| 固始县| 津南区| 小金县| 鄱阳县| 墨玉县| 林口县| 鹤庆县| 潞城市| 连江县| 韶关市| 昌宁县| 甘孜| 孟村| 公主岭市| 锦屏县| 星座| 凤山市| 错那县| 民丰县| 车险| 南木林县| 泽库县| 河源市| 溧阳市| 饶河县| 阿合奇县| 白朗县| 兖州市| 平乐县| 锡林郭勒盟| 嘉禾县| 大化|