javaSE 18 多线程02

 1 package LS_进阶多线程02;
 2 
 3 public class TestAccount {
 4     public static void main(String[] args) {
 5         Account ac = new Account("账号001",0);
 6         AccountThread t1 = new AccountThread(ac);//创建线程
 7         AccountThread t2 = new AccountThread(ac);
 8         t1.setName("t1");//设置线程名
 9         t2.setName("t2");
10 
11         t1.start();//启动线程
12         t2.start();
13 
14     }
15 }
01
 1 package LS_进阶多线程02;
 2 
 3 public class AccountThread extends Thread {
 4     private Account ac;
 5     public AccountThread(Account ac){
 6         this.ac = ac;
 7     }
 8     @Override
 9     public void run() {
10         //实现存钱的功能
11         //锁在方法上面会故扩大范围,降低效率
12    //synchronized(ac){
13         ac.add(100);
14         System.out.println(Thread.currentThread().getName()+"存钱,余额"+ac.getMoney());
15    //}
16     }
17 }
02
 1 package LS_进阶多线程02;
 2 
 3 public class Account {
 4     String id;
 5     int money;
 6     Object o = new Object();
 7     public Account(String id,int money){
 8         super();
 9         this.id = id;
10         this.money = money;
11     }
12 
13     public String getId() {
14         return id;
15     }
16 
17     public void setId(String id) {
18         this.id = id;
19     }
20 
21     public int getMoney() {
22         return money;
23     }
24 
25     public void setMoney(int money) {
26         this.money = money;
27     }
28     //存钱
29     public void add(int money){
30 //        //获取原本的余额
31 //        int before = this.money;
32 //        //加上存进去的钱
33 //        int after = before+money;
34 //
35 //        //模拟网络延迟
36 //        try {
37 //            Thread.sleep(1000);
38 //        } catch (InterruptedException e) {
39 //            e.printStackTrace();
40 //        }
41 //        //将总的余额设置成形成的余额
42 //        this.setMoney(after);
43         //synchronized(){}
44         //同步锁:synchronized的互斥机制
45         //()里面放的是:同步的线程的共享对象,id也可以
46         //synchronized也可以对方法和类实现锁
47 
48         //局部变量无法实现锁定
49         Object  o = new Object();
50         //synchronized (this){
51         //synchronized (o){
52         synchronized (id){
53             //获取原本的余额
54             int before = this.money;
55             //加上存进去的钱
56             int after = before+money;
57 
58             //模拟网络延迟
59             try {
60                 Thread.sleep(1000);
61             } catch (InterruptedException e) {
62                 e.printStackTrace();
63             }
64             //将总的余额设置成形成的余额
65             this.setMoney(after);
66         }
67     }
68 }
03
 1 package LS_进阶多线程02;
 2 
 3 //死锁的实现
 4 
 5 public class TestDeadLock {
 6     public static void main(String[] args) {
 7 
 8         Object o1 = new Object();
 9         Object o2 = new Object();
10         Thread t1 = new Thread(new Thread01(o1,o2));
11         Thread t2 = new Thread(new Thread02(o1,o2));
12 
13         t1.start();
14         t2.start();
15 
16     }
17 }
18 
19 
20 class Thread01 implements Runnable {
21     Object o1;
22     Object o2;
23 
24     public Thread01(Object o1, Object o2) {
25         this.o1 = o1;
26         this.o2 = o2;
27     }
28 
29     @Override
30     public void run() {
31         synchronized (o1) {
32             try {
33                 Thread.sleep(1000);
34             } catch (InterruptedException e) {
35                 e.printStackTrace();
36             }
37             synchronized (o2) {
38 
39             }
40         }
41     }
42 }
43 
44 
45 class Thread02 implements Runnable {
46 
47     Object o1;
48     Object o2;
49         public Thread02(Object o1, Object o2) {
50             this.o1 = o1;
51             this.o2 = o2;
52         }
53 
54         @Override
55         public void run() {
56             synchronized (o2) {
57                 try {
58                     Thread.sleep(1000);
59                 } catch (InterruptedException e) {
60                     e.printStackTrace();
61                 }
62                 synchronized (o1) {
63 
64                 }
65             }
66         }
67     }
04
 1 package LS_进阶多线程02;
 2 
 3 //死锁的实现
 4 
 5 public class TestDeadLock {
 6     public static void main(String[] args) {
 7         Object o1 = new Object();
 8         Object o2 = new Object();
 9         Thread t1 = new Thread(new Thread01(o1,o2));
10         Thread t2 = new Thread(new Thread01(o1,o2));
11 
12         t1.start();
13         t2.start();
14 
15     }
16 }
17 
18 
19 class Thread01 implements Runnable {
20     Object o1;
21     Object o2;
22 
23     public Thread01(Object o1, Object o2) {
24         this.o1 = o1;
25         this.o2 = o2;
26     }
27 
28     @Override
29     public void run() {
30         synchronized (o1) {
31             try {
32                 Thread.sleep(1000);
33             } catch (InterruptedException e) {
34                 e.printStackTrace();
35             }
36             synchronized (o2) {
37             }
38         }
39     }
40 }
41     class Thread02 implements Runnable {
42         Object o1;
43         Object o2;
44 
45         public Thread02(Object o1, Object o2) {
46             this.o1 = o1;
47             this.o2 = o2;
48         }
49 
50         @Override
51         public void run() {
52             synchronized (o2) {
53                 try {
54                     Thread.sleep(1000);
55                 } catch (InterruptedException e) {
56                     e.printStackTrace();
57                 }
58                 synchronized (o1) {
59 
60                 }
61             }
62         }
63     
 1 package LS_进阶多线程02;
 2 
 3 //t2要不要等t1执行完,才能执行--->不要
 4 //对象锁锁的对象,100个对象有100把锁
 5 //类锁锁的是类,100个对象还是一把锁
 6 public class TestLock {
 7     public static void main(String[] args) {
 8         MyClass mc1 = new MyClass();
 9         MyClass mc2 = new MyClass();
10 
11         Thread t1 = new Thread(new MyThread(mc1));
12         Thread t2 = new Thread(new MyThread(mc1));
13 
14         t1.setName("t1");
15         t2.setName("t2");
16 
17 
18         t1.start();
19 
20         try {
21             Thread.sleep(1000);//确保t1先执行
22         } catch (InterruptedException e) {
23             e.printStackTrace();
24         }
25 
26         t2.start();
27     }
28 }
29 
30 class MyThread implements Runnable{
31     private MyClass mc;
32     public MyThread(MyClass mc){
33         this.mc = mc;
34     }
35     @Override
36     public void run() {
37         if(Thread.currentThread().getName()=="t1"){
38             mc.test01();
39         }
40         if(Thread.currentThread().getName()=="t2"){
41             mc.test02();
42         }
43     }
44 }
45 
46 
47 class MyClass{
48     //public void test01(){
49     public synchronized void test01(){   //这个可以实现t1执行完,t2执行
50         System.out.println("test01.begin");
51 
52         try {
53             Thread.sleep(1000*5);
54         } catch (InterruptedException e) {
55             e.printStackTrace();
56         }
57         System.out.println("test01.over");
58     }
59 
60     public synchronized void test02(){//如果没有synchronized,那么无法实现t1先完成
61         System.out.println("test02.begin");
62         System.out.println("test02.over");
63     }
64 }
05
会当凌绝顶,一览众山小
原文地址:https://www.cnblogs.com/quenvpengyou/p/14384260.html