[Java] java 线程的互斥 线程的同步 →→→→→进入此内容的聊天室

来自 , 2019-07-23, 写在 Java, 查看 108 次.
URL http://www.code666.cn/view/42e77b63
  1. /**
  2.  * 线程的互斥,主要展示同步方法与非同步方法的区别
  3.  */
  4. public class Synchronized {
  5.         /**
  6.          * 帐号类
  7.          */
  8.         static class Account{
  9.                 //共享资源, 钱数
  10.                 private double money = 1000.0d;
  11.                 /**
  12.                  * 存钱       没有同步机制
  13.                  * @param fFees
  14.                  */
  15.                 public void nonSynDeposit(double fFees){
  16.                         System.out.println("Account nonSynDeposit begin! money = " + this.money + "; fFees = " + fFees);
  17.                         //存钱钱先等待300毫秒
  18.                         System.out.println("Account nonSynDeposit sleep begin!");
  19.                         try {
  20.                                 Thread.sleep(300);
  21.                         } catch (InterruptedException e) {
  22.                                 e.printStackTrace();
  23.                         }
  24.                         System.out.println("Account nonSynDeposit sleep end!");
  25.                         this.money = this.money + fFees;
  26.                         System.out.println("Account nonSynDeposit end! money = " + this.money);
  27.                 }
  28.                 /**
  29.                  * 取钱       没有同步机制
  30.                  * @param fFees
  31.                  */
  32.                 public void nonSynWithdraw(double fFees){
  33.                         System.out.println("Account nonSynWithdraw begin! money = " + this.money + "; fFees = " + fFees);
  34.                         //取钱时先等待400毫秒
  35.                         System.out.println("Account nonSynWithdraw sleep begin!");
  36.                         try {
  37.                                 Thread.sleep(400);
  38.                         } catch (InterruptedException e) {
  39.                                 e.printStackTrace();
  40.                         }
  41.                         System.out.println("Account nonSynWithdraw sleep begin!");
  42.                         this.money = this.money - fFees;
  43.                         System.out.println("Account nonSynWithdraw end! money = " + this.money);
  44.                 }
  45.                 /**
  46.                  * 存钱       有同步机制
  47.                  * @param fFees
  48.                  */
  49.                 public synchronized void synDeposit(double fFees){
  50.                         System.out.println("Account synDeposit begin! money = " + this.money + "; fFees = " + fFees);
  51.                         //存钱钱先等待300毫秒
  52.                         System.out.println("Account synDeposit sleep begin!");
  53.                         try {
  54.                                 Thread.sleep(300);
  55.                         } catch (InterruptedException e) {
  56.                                 e.printStackTrace();
  57.                         }
  58.                         System.out.println("Account synDeposit sleep begin!");
  59.                         this.money = this.money + fFees;
  60.                         System.out.println("Account synDeposit end! money = " + this.money);
  61.                 }
  62.                 /**
  63.                  * 取钱       有同步机制
  64.                  * @param fFees
  65.                  */
  66.                 public synchronized void synWithdraw(double fFees){
  67.                         System.out.println("Account synWithdraw begin! money = " + this.money + "; fFees = " + fFees);
  68.                         //取钱时先等待400毫秒
  69.                         System.out.println("Account synWithdraw sleep begin!");
  70.                         try {
  71.                                 Thread.sleep(400);
  72.                         } catch (InterruptedException e) {
  73.                                 e.printStackTrace();
  74.                         }
  75.                         System.out.println("Account synWithdraw sleep end!");
  76.                         this.money = this.money - fFees;
  77.                         System.out.println("Account synWithdraw end! money = " + this.money);
  78.                 }
  79.         }
  80.        
  81.         static class AccessThread extends Thread{
  82.                 //待访问的帐号对象
  83.                 private Account account = null;
  84.                 //访问帐号的方法
  85.                 private String method = "";
  86.  
  87.                 public AccessThread(Account account, String method){
  88.                         this.method = method;
  89.                         this.account = account;
  90.                 }
  91.                 public void run(){
  92.                         //对不同的方法名参数调用不同的方法
  93.                         if (method.equals("nonSynDeposit")){
  94.                                 account.nonSynDeposit(500.0);
  95.                         } else if (method.equals("nonSynWithdraw")){
  96.                                 account.nonSynWithdraw(200.0);
  97.                         } else if (method.equals("synDeposit")){
  98.                                 account.synDeposit(500.0);
  99.                         } else if (method.equals("synWithdraw")){
  100.                                 account.synWithdraw(200.0);
  101.                         }
  102.                 }
  103.         }
  104.  
  105.         public static void main(String[] args) {
  106.                 //待操作的帐号对象,所有操作都针对该帐号
  107.                 Account account = new Account();
  108.                
  109.                 System.out.println("使用非同步方法时:");
  110.                 //非同步方法存钱的线程
  111.                 Thread threadA = new AccessThread(account, "nonSynDeposit");
  112.                 //非同步方法取钱的线程
  113.                 Thread threadB = new AccessThread(account, "nonSynWithdraw");
  114.                 threadA.start();
  115.                 threadB.start();
  116.                 //等待两线程运行结束
  117.                 try {
  118.                         threadA.join();
  119.                         threadB.join();
  120.                 } catch (InterruptedException e) {
  121.                         e.printStackTrace();
  122.                 }
  123.                 //下面测试同步方法
  124.                 System.out.println();
  125.                 account = new Account();
  126.                 System.out.println("使用同步方法时:");
  127.                 threadA = new AccessThread(account, "synDeposit");
  128.                 threadB = new AccessThread(account, "synWithdraw");
  129.                 threadA.start();
  130.                 threadB.start();
  131.         }
  132. }

回复 "java 线程的互斥 线程的同步"

这儿你可以回复上面这条便签

captcha