您好,登錄后才能下訂單哦!
對同一個對象進行多線程操作時,如何保證線程執行結果的一致性?我們需要對線程操作對象加同步鎖。(這是一道面試題)
需求描述
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()。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。