• 【第八章 线程的同步机制(同步代码块、同步方法)】


    第八章 线程的同步机制(同步代码块、同步方法)

    1.线程的同步机制方式一:同步代码块
    java中通过同步机制解决线程安全问题。
    synchronized(同步监视器){ //需要被同步的代码 }
    ②说明:
    操作共享数据的代码即为需要被同步的代码。
    共享数据:多个线程共同操作的变量。比如:ticket。
    同步监视器:俗称:锁,任何一个类的对象都可以充当锁。
    要求:多个线程必须要共用同一把锁。
    ③同步方式的好处:解决了线程安全问题。
    操作同步代码时,只能有一个线程参与,其他线程等待,相当于是一个单线程的过程,效率低。
    ④在实现Runnable接口创建多线程的方式中,可用考虑使用this充当同步监视器。

    //④在实现Runnable接口创建多线程
    public class WindowTest1 {
        public static void main(String[] args) {
            Window1 w1=new Window1();
            Thread t1=new Thread(w1);
            Thread t2=new Thread(w1);
            Thread t3=new Thread(w1);
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
        }
    }
    class Window1 implements  Runnable{
        private  int ticket=100;
        Object obj=new Object();
        @Override
        public void run() {
            while (true){
                synchronized (obj){//可用this,代表当前对象,对象唯一,锁唯一
                    if(ticket>0){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
          System.out.println(Thread.currentThread().getName()+"卖票:票号为:"+ticket);
                        ticket--;
                    }else {
                        break;
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    ⑤在继承Thread类创建多线程的方式中,慎用this充当同步监视器,考虑使用当前类充当同步监视器。

    //⑤继承Thread类创建多线程
    public class WindowTest {
        public static void main(String[] args) {
            Window w1=new Window();
            Window w2=new Window();
            Window w3=new Window();
            w1.setName("窗口1");
            w2.setName("窗口2");
            w3.setName("窗口3");
            w1.start();
            w2.start();
            w3.start();
        }
    }
    class Window extends Thread{
        private  static int ticket=100;
        private  static Object obj=new Object();
        @Override
        public void run() {
            while (true){
                synchronized (obj){//class=clazz=Window.class,Window.class只会加载一次
                    if(ticket>0){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(getName()+":卖票,票号为:"+ticket);
                        ticket--;
                    }else {
                        break;
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2.线程的同步机制方式二:同步方法
    同步方法仍然涉及到同步监视器,只是不需要我们显式的声明,非静态的同步方法同步监视器为this,静态的同步方法的监视器为当前类本身。
    ①在实现Runnable接口创建多线程

    public class WindowTest3 {
        public static void main(String[] args) {
            Window1 w1=new Window1();
            Thread t1=new Thread(w1);
            Thread t2=new Thread(w1);
            Thread t3=new Thread(w1);
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
        }
    }
    class Window3 implements  Runnable{
        private  int ticket=100;
        @Override
        public void run() {
            while (true){
                 show();
            }
        }
        private  synchronized void show(){//同步监视器:this
                if(ticket>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
      System.out.println(Thread.currentThread().getName()+"卖票:票号为:"+ticket);
                    ticket--;
                }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    ②继承Thread类创建多线程

    public class WindowTest4 {
        public static void main(String[] args) {
            Window4 w1=new Window4();
            Window4 w2=new Window4();
            Window4 w3=new Window4();
            w1.setName("窗口1");
            w2.setName("窗口2");
            w3.setName("窗口3");
            w1.start();
            w2.start();
            w3.start();
        }
    }
    class Window4 extends Thread{
        private  static int ticket=100;
        @Override
        public void run() {
        	while(true){
            show();}
        }
    private static synchronized void show(){
    //同步监视器:t1,t2,t3,此时同步监视器为Window4.class
            if(ticket>0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Window4.currentThread().getName()+":卖票,票号为:"+ticket);
                ticket--;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    《OpenDRIVE1.6规格文档》4
    【王道代码】【2.3链表】d1
    c++ 常用STL 之set
    9月7日扒面经
    浅谈python后端面试(附一些经典项目源码)
    剑指offer—day 12(双指针-2)
    计算机毕设(附源码)JAVA-SSM辽宁省高考志愿智能辅助填报系统
    2024年区块链链游即将迎来大爆发
    Java高并发编程卷二(二) 锁
    数据结构与算法-顺序表
  • 原文地址:https://blog.csdn.net/qq_43742813/article/details/126842428