真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java線程使用同步鎖交替執(zhí)行打印奇數(shù)偶數(shù)的方法

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

10年的西盟網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整西盟建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“西盟網(wǎng)站設計”,“西盟網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

需求描述

1-20個數(shù)字

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

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

C線程在AB兩個線程執(zhí)行完了之后打印結(jié)果:“success”。

線程代碼實現(xiàn)

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(奇數(shù)線程)
* @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(偶數(shù)線程)
 * @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(等待結(jié)果線程)
 * @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();
  }
}

測試結(jié)果

"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 作為方法塊使用時需要只能對對象加鎖,不能是常用數(shù)據(jù)類型。

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

AThread.java

package com.boonya.thread.test;
 
/**
* @ClassName: AThread
* @Description: TODO(奇數(shù)線程)
* @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(偶數(shù)線程)
 * @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();
            }
          }
        }
      }
    }
 }

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


分享文章:Java線程使用同步鎖交替執(zhí)行打印奇數(shù)偶數(shù)的方法
轉(zhuǎn)載來源:http://weahome.cn/article/jsihhe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部