最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java線程沖突與線程同步

2022-09-01 16:19 作者:虛云幻仙  | 我要投稿

/**
* 線程沖突和線程同步
* 當(dāng)多個線程同時對同一個對象進行存取會出現(xiàn)線程沖突
* java中通過線程同步synchronized限制對象的并行訪問,線程同步時當(dāng)某個線程正在訪問該對象,其他正要訪問的線程進入阻塞狀態(tài),直至前一個線程執(zhí)行完受同步影響的語句塊,下一個線程再變?yōu)榫途w狀態(tài)
*/

public class Test8Thread implements Runnable{

? ?public static class Account{
? ? ? ?//創(chuàng)建賬戶類,存款不能為負數(shù)
? ? ? ?String id;
? ? ? ?int money;

? ? ? ?public Account(String id, int money) {
? ? ? ? ? ?this.id = id;
? ? ? ? ? ?this.money = money;
? ? ? ?}
? ?}

? ?Account targetAccount;
? ?//要訪問的賬戶

? ?public Test8Thread(Account targetAccount) {
? ? ? ?this.targetAccount = targetAccount;
? ?}

? ?@Override
? ?public void run() {
? ? ? ?if (targetAccount.money>=800){
? ? ? ? ? ?System.out.println("正在取款,請稍等");
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?Thread.sleep(500);
? ? ? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?}
? ? ? ? ? ?targetAccount.money -= 800;
? ? ? ? ? ?System.out.println("取款成功,剩余金額: "+targetAccount.money);
? ? ? ?}else {
? ? ? ? ? ?System.out.println("余額不足,取款失敗");
? ? ? ?}
? ?}

? ?public static void main(String[] args) {
? ? ? ?Account a1 = new Account("101",1000);
? ? ? ?new Thread(new Test8Thread(a1),"1號取款人").start();
? ? ? ?new Thread(new Test8Thread(a1),"2號取款人").start();
? ? ? ?/*結(jié)果為:
? ? ? ?正在取款,請稍等
? ? ? ?正在取款,請稍等
? ? ? ?取款成功,剩余金額: 200
? ? ? ?取款成功,剩余金額: 200

? ? ? ?兩個線程幾乎同時讀取到money=1000都判斷>800,又幾乎同時用1000-800賦值給money
? ? ? ?如果其中一個線程先一步完成money-=800,另一個線程會計算200-800賦值給money產(chǎn)生負數(shù)
? ? ? ? */

? ?}
}

class TakeMoney implements Runnable{
? ?Test8Thread.Account target;

? ?public TakeMoney(Test8Thread.Account target) {
? ? ? ?this.target = target;
? ?}


? ?@Override
? ?public void run() {
? ? ? ?synchronized (target){
? ? ? ? ? ?//synchronized(鎖對象){同步代碼} 在執(zhí)行語句塊的內(nèi)容時給對象(必須是對象)上鎖,同步代碼執(zhí)行時具有線程互斥的能力(并行變串行)
? ? ? ? ? ?if (target.money>=800){
? ? ? ? ? ? ? ?System.out.println(Thread.currentThread().getName()+"正在取款,請稍等");
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?Thread.sleep(500);
? ? ? ? ? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?target.money-=800;
? ? ? ? ? ? ? ?System.out.println(Thread.currentThread().getName()+"取款成功");
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?System.out.println(Thread.currentThread().getName()+"余額不足,取款失敗");
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?System.out.println(Thread.currentThread().getName()+"結(jié)束,余額為:"+target.money);
? ? ? ?//同步語句塊外的內(nèi)容仍然為并行
? ?}

? ?public static void reset(Test8Thread.Account t){
? ? ? ?synchronized (t){
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?Thread.sleep(3000);
? ? ? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?}
? ? ? ? ? ?t.money=700;
? ? ? ?}
? ?}
? ?public static void main(String[] args) {
? ? ? ?Test8Thread.Account a1 = new Test8Thread.Account("101",1000);
? ? ? ?Thread t1 = new Thread(new TakeMoney(a1),"1號取款人");
? ? ? ?Thread t2 = new Thread(new TakeMoney(a1),"2號取款人");
? ? ? ?t1.start();
? ? ? ?t2.start();
? ? ? ?/*結(jié)果為:
? ? ? ?1號取款人正在取款,請稍等
? ? ? ?1號取款人取款成功
? ? ? ?1號取款人結(jié)束,余額為:200
? ? ? ?2號取款人余額不足,取款失敗
? ? ? ?2號取款人結(jié)束,余額為:200

? ? ? ?t1執(zhí)行synchronized語句時t2阻塞,等t1執(zhí)行完語句后t2進入就緒狀態(tài),判定money<800取款失敗
? ? ? ? */


? ? ? ?Test8Thread.Account tar = new Test8Thread.Account("102",1000);
? ? ? ?Thread t3 = new Thread(new TakeMoney(tar));
? ? ? ?t3.start();
? ? ? ?reset(tar);
? ? ? ?/*結(jié)果為
? ? ? ?Thread-0余額不足,取款失敗
? ? ? ?Thread-0結(jié)束,余額為:700

? ? ? ?synchronized鎖對象為同一對象時,執(zhí)行不同語句塊也會線程互斥
? ? ? ? */

? ?}
}

class CustomizeArray{
? ?private int[] arr = new int[10];
? ?public synchronized void initialize(){
? ? ? ?//synchronized修飾方法時鎖對象為this/當(dāng)前對象
? ? ? ?for (int i =0;i<10;i++){
? ? ? ? ? ?arr[i]=i;
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?Thread.sleep(500);
? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?System.out.println(Arrays.toString(arr));
? ?}

? ?public synchronized void sum(){
? ? ? ?synchronized (this){
? ? ? ? ? ?//鎖對象為this,并且語句塊涵蓋整個方法體可改寫為方法的修飾詞
? ? ? ? ? ?int sum=0;
? ? ? ? ? ?for (int i:arr){
? ? ? ? ? ? ? ?sum+=i;
? ? ? ? ? ?}
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?Thread.sleep(500);
? ? ? ? ? ?} catch (InterruptedException e) {
? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?}
? ? ? ? ? ?System.out.println("和為:"+sum);
? ? ? ?}
? ?}
}
class InitialThread implements Runnable{
? ?//執(zhí)行arr初始化
? ?CustomizeArray arr;

? ?public InitialThread(CustomizeArray arr) {
? ? ? ?this.arr = arr;
? ?}

? ?@Override
? ?public void run() {
? ? ? ?arr.initialize();
? ?}
}
class SumThread implements Runnable{
? ?//執(zhí)行arr求和
? ?CustomizeArray arr;

? ?public SumThread(CustomizeArray arr) {
? ? ? ?this.arr = arr;
? ?}

? ?@Override
? ?public void run() {
? ? ? ?arr.sum();
? ?}
}
class Test9Thread{
? ?public static void main(String[] args) {
? ? ? ?CustomizeArray arr = new CustomizeArray();
? ? ? ?Thread t1 = new Thread(new InitialThread(arr));
? ? ? ?Thread t2 = new Thread(new SumThread(arr));
? ? ? ?t1.start();
? ? ? ?t2.start();
? ? ? ?//結(jié)果為
? ? ? ?// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? ? ? ?//和為:45
? ? ? ?//多個線程訪問同一個鎖對象,任何一個線程執(zhí)行任何一段同步代碼時,都會線程互斥,其他線程進入阻塞狀態(tài)等待喚醒

? ?}
}

java線程沖突與線程同步的評論 (共 條)

分享到微博請遵守國家法律
全南县| 淳化县| 西盟| 广安市| 咸丰县| 门源| 通化县| 西平县| 胶南市| 台北市| 抚宁县| 连山| 收藏| 堆龙德庆县| 无极县| 玛曲县| 白水县| 奉节县| 钟祥市| 威海市| 瑞安市| 原平市| 安多县| 沈丘县| 三原县| 阜城县| 清水县| 常州市| 禄丰县| 渑池县| 大名县| 南漳县| 科尔| 镇平县| 繁峙县| 临高县| 汪清县| 通辽市| 新乡县| 桃园市| 福建省|