java之 ------ 多線程(互動線程)

來源:互聯網
上載者:User

標籤:java   多線程   互動線程   


1、運行結果不惟一,取決於線程調度
2、線程執行被打斷時出現錯誤

3、線程互斥和臨界區管理

1)作業系統對共用一個變數的若干線程進入各自臨界區有以下3個調度原則:
2)一次至多一個線程能夠在它的臨界區內。
3)不能讓一個線程無限地留在它的臨界區內。
4)不能強迫一個線程無限地等待進入它的臨界區。特別地,進入臨界區的任一線程不能妨礙正等待進入的其他線程的進展。

4、同步語句   synchronized (對象)

       同步方法  synchronized  方法聲明   方法聲明synchronized(this){  方法體  }

5、銀行賬戶的存款取款線程


Account.c

package p25;public class Account {private String name;//儲戶姓名private double balance;//賬戶餘額public Account(String name) {this.name = name;this.balance = 0;}//查詢餘額public double balance() {return this.balance;}public String getName() {return this.name;}//存款操作public void put(double value) {if (value > 0) {this.balance += value;}}//取款操作public double get(double value) {if (value > 0) {if (value < this.balance) {//夠取this.balance -= value;} else {value = this.balance;this.balance = 0;}return value;}return 0;}}class Save extends Thread {private Account account;private double value;public Save(Account account, double value) {this.account = account;this.value = value;}public void run() {synchronized (account) {//聲明臨界區,鎖定當前account對象double howmuch = this.account.balance();//查看賬戶餘額try {sleep(1);} catch (InterruptedException e) {e.printStackTrace();}this.account.put(this.value);double howmuch2 = this.account.balance();//查看賬戶餘額System.out.println(this.account.getName() + "賬戶: 現有" + howmuch+ ",存入" + this.value + ",餘額為" + howmuch2);}}}class Fetch extends Thread {private Account account;private double value;public Fetch(Account account, double value) {this.account = account;this.value = value;}public void run() {synchronized (account) {double howmuch = this.account.balance();try {sleep(1);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}double v = this.account.get(this.value);double howmuch2 = this.account.balance();System.out.println(this.account.getName() + "賬戶: 現有" + howmuch+ ",取出" + v + ",餘額為" + howmuch2);}}}class Func extends Thread {private Account account;private double value;public Func(Account account, double value) {this.account = account;this.value = value;}public void run() {fun();}synchronized void fun(){double howmuch = this.account.balance();try {sleep(1);} catch (InterruptedException e) {e.printStackTrace();}this.account.put(this.value);double v = this.account.get(this.value);double howmuch2 = this.account.balance();System.out.println(this.account.getName() + "賬戶: 現有" + howmuch + ",取出"+ v + ",餘額為" + howmuch2);}}
Bank.c

package p25;public class Bank {public static void main(String[] args) {Account wang = new Account("wang");(new Save(wang,100)).start();(new Save(wang,200)).start();(new Fetch(wang,100)).start();Account li=new Account("li");(new Save(li,300)).start();Account xu=new Account("xu");(new Func(xu,100000)).start();}}


註:必須要聲明臨界區,鎖定account對象,不然會出錯
正確結果:

                                          

錯誤結果:

                     

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java之 ------ 多線程(互動線程)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.