20220804 第一组 于芮 多线程基础(第二十四天)()

小白成长记——第二十四天

   经历了三天的学习终于完成了多线程基础的学习,又是被打击的一天,好像听懂了,好像又没听懂,就像一团浆糊,果然是比较难啃的一块硬骨头,但是我会继续努力,早晚可以看懂的,加油啊!!!来看一下今天的学习笔记吧!!

   今天的实例也有很多,通过实例可以将所学的知识结合起来,来看看今天的实例吧!!

 1 package D0804;
 2 
 3  public class Test {
 4     private float account;
 5 
 6      public void ATM(float money){
 7         this.account=money;
 8     }
 9     public synchronized  float getAccount(){
10         try {
11             Thread.sleep(2000);
12         }catch (InterruptedException e){
13             e.printStackTrace();
14         }
15         return account;
16     }
17     public  synchronized  void deposit(float money){
18         account+=money;
19     }
20     public  synchronized void drawMoney(float money) throws Exception{
21         if(account<money){
22             throw  new Exception("余额不足!");
23         }
24         account-=money;
25     }
26 
27      }
28 class UserA extends Thread{
29     private Test atm;
30     private float money;
31     public  UserA(Test a,float m){
32         atm=a;
33         money=m;
34     }
35     public  void run(){
36         System.out.println("账户存款数额为:"+atm.getAccount()+"元!");
37         System.out.println("账户用A存了:"+money+"元!");
38         try {
39             atm.deposit(money);
40         }catch (Exception e){
41             System.out.println("余额为:"+atm.getAccount()+"元!");
42         }
43     }
44 }
45 class UserB extends Thread{
46     private Test atm;
47     private  float money;
48     public UserB(Test a,float m){
49         atm=a;
50         money=m;
51     }
52     public  void run(){
53         System.out.println("账户存款数额为:"+atm.getAccount()+"元!");
54         System.out.println("账户用B取了:"+money+"元!");
55         try {
56             atm.drawMoney(money);
57             System.out.println("余额为:"+atm.getAccount()+"元!");
58         }catch (Exception e){
59             System.out.println(e.getMessage());
60         }
61     }
62 }
63 class Test1{
64     public static void main(String[] args) {
65         Test atm=new Test();
66         UserA userA=new UserA(atm,100);
67         UserB userB=new UserB(atm,50);
68         userA.start();
69         try {
70             userA.join();
71         }catch (InterruptedException e){
72             userB.start();
73         }
74         System.out.println("==========================");
75         userB.start();
76         try {
77             userB.join();
78         }catch (InterruptedException e){
79             userA.start();
80         }
81 
82     }
83 }
 1 package D0804;
 2 
 3 import java.util.ArrayList;
 4 import java.util.concurrent.Semaphore;
 5 
 6 public class Ch04 extends Thread{
 7     private  final  static  String name="哲学家";
 8     private  final  int num;
 9     private  volatile ArrayList<Semaphore>semaphores;
10     public Ch04(int num,ArrayList<Semaphore>semaphores){
11         super(Ch04.name+String.valueOf(num));
12         this.num=num;
13         this.semaphores=semaphores;
14     }
15 
16     @Override
17     public void run() {
18         try {
19             if(this.num%2==0){
20                 semaphores.get(this.num).acquire();
21                 Thread.sleep(1000);
22                 semaphores.get(this.getRightSemaphoreIndex(this.num)).acquire();
23             }else {
24                 semaphores.get(this.getRightSemaphoreIndex(this.num)).acquire();
25                 Thread.sleep(1000);
26                 semaphores.get(this.num).acquire();
27             }
28             System.out.println(this.getName()+"获得了筷子"+String.valueOf(this.num)+"和"+String.valueOf(this.getRightSemaphoreIndex(this.num))+"开始吃饭");
29 
30         }catch (Exception e){
31             e.printStackTrace();
32         }
33         finally {
34             semaphores.get(this.num).release();
35             semaphores.get(this.getRightSemaphoreIndex(this.num)).release();
36         }
37 
38     }
39     private int getRightSemaphoreIndex(int num){
40         return (num-1<0)?semaphores.size()-1:num-1;
41     }
42 
43     public static void main(String[] args) {
44         ArrayList<Semaphore>semaphores1=new ArrayList<>();
45         int counter=5;
46         for (int i = 0; i < counter ; i++) {
47             semaphores1.add(new Semaphore(1));
48         }
49         for (int i = 0; i < counter; i++) {
50             new Ch04(i,semaphores1).start();
51         }
52     }
53 }
————————

小白成长记——第二十四天

   经历了三天的学习终于完成了多线程基础的学习,又是被打击的一天,好像听懂了,好像又没听懂,就像一团浆糊,果然是比较难啃的一块硬骨头,但是我会继续努力,早晚可以看懂的,加油啊!!!来看一下今天的学习笔记吧!!

   今天的实例也有很多,通过实例可以将所学的知识结合起来,来看看今天的实例吧!!

 1 package D0804;
 2 
 3  public class Test {
 4     private float account;
 5 
 6      public void ATM(float money){
 7         this.account=money;
 8     }
 9     public synchronized  float getAccount(){
10         try {
11             Thread.sleep(2000);
12         }catch (InterruptedException e){
13             e.printStackTrace();
14         }
15         return account;
16     }
17     public  synchronized  void deposit(float money){
18         account+=money;
19     }
20     public  synchronized void drawMoney(float money) throws Exception{
21         if(account<money){
22             throw  new Exception("余额不足!");
23         }
24         account-=money;
25     }
26 
27      }
28 class UserA extends Thread{
29     private Test atm;
30     private float money;
31     public  UserA(Test a,float m){
32         atm=a;
33         money=m;
34     }
35     public  void run(){
36         System.out.println("账户存款数额为:"+atm.getAccount()+"元!");
37         System.out.println("账户用A存了:"+money+"元!");
38         try {
39             atm.deposit(money);
40         }catch (Exception e){
41             System.out.println("余额为:"+atm.getAccount()+"元!");
42         }
43     }
44 }
45 class UserB extends Thread{
46     private Test atm;
47     private  float money;
48     public UserB(Test a,float m){
49         atm=a;
50         money=m;
51     }
52     public  void run(){
53         System.out.println("账户存款数额为:"+atm.getAccount()+"元!");
54         System.out.println("账户用B取了:"+money+"元!");
55         try {
56             atm.drawMoney(money);
57             System.out.println("余额为:"+atm.getAccount()+"元!");
58         }catch (Exception e){
59             System.out.println(e.getMessage());
60         }
61     }
62 }
63 class Test1{
64     public static void main(String[] args) {
65         Test atm=new Test();
66         UserA userA=new UserA(atm,100);
67         UserB userB=new UserB(atm,50);
68         userA.start();
69         try {
70             userA.join();
71         }catch (InterruptedException e){
72             userB.start();
73         }
74         System.out.println("==========================");
75         userB.start();
76         try {
77             userB.join();
78         }catch (InterruptedException e){
79             userA.start();
80         }
81 
82     }
83 }
 1 package D0804;
 2 
 3 import java.util.ArrayList;
 4 import java.util.concurrent.Semaphore;
 5 
 6 public class Ch04 extends Thread{
 7     private  final  static  String name="哲学家";
 8     private  final  int num;
 9     private  volatile ArrayList<Semaphore>semaphores;
10     public Ch04(int num,ArrayList<Semaphore>semaphores){
11         super(Ch04.name+String.valueOf(num));
12         this.num=num;
13         this.semaphores=semaphores;
14     }
15 
16     @Override
17     public void run() {
18         try {
19             if(this.num%2==0){
20                 semaphores.get(this.num).acquire();
21                 Thread.sleep(1000);
22                 semaphores.get(this.getRightSemaphoreIndex(this.num)).acquire();
23             }else {
24                 semaphores.get(this.getRightSemaphoreIndex(this.num)).acquire();
25                 Thread.sleep(1000);
26                 semaphores.get(this.num).acquire();
27             }
28             System.out.println(this.getName()+"获得了筷子"+String.valueOf(this.num)+"和"+String.valueOf(this.getRightSemaphoreIndex(this.num))+"开始吃饭");
29 
30         }catch (Exception e){
31             e.printStackTrace();
32         }
33         finally {
34             semaphores.get(this.num).release();
35             semaphores.get(this.getRightSemaphoreIndex(this.num)).release();
36         }
37 
38     }
39     private int getRightSemaphoreIndex(int num){
40         return (num-1<0)?semaphores.size()-1:num-1;
41     }
42 
43     public static void main(String[] args) {
44         ArrayList<Semaphore>semaphores1=new ArrayList<>();
45         int counter=5;
46         for (int i = 0; i < counter ; i++) {
47             semaphores1.add(new Semaphore(1));
48         }
49         for (int i = 0; i < counter; i++) {
50             new Ch04(i,semaphores1).start();
51         }
52     }
53 }