zoukankan      html  css  js  c++  java
  • JAVA多线程(六):线程同步(二)线程的同步代码实现

    1 实例一 创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式

    package com.atguigu.java;
    
    /**
     * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式
     *
     * 1.问题:卖票过程中,出现了重票、错票 -->出现了线程的安全问题
     * 2.问题出现的原因:当某个线程操作车票的过程中,尚未操作完成时,其他线程参与进来,也操作车票。
     * 3.如何解决:当一个线程a在操作ticket的时候,其他线程不能参与进来。直到线程a操作完ticket时,其他
     *            线程才可以开始操作ticket。这种情况即使线程a出现了阻塞,也不能被改变。
     *
     *
     * 4.在Java中,我们通过同步机制,来解决线程的安全问题。
     *
     *  方式一:同步代码块
     *
     *   synchronized(同步监视器){
     *      //需要被同步的代码
     *
     *   }
     *  说明:1.操作共享数据的代码,即为需要被同步的代码。  -->不能包含代码多了,也不能包含代码少了。
     *       2.共享数据:多个线程共同操作的变量。比如:ticket就是共享数据。
     *       3.同步监视器,俗称:锁。任何一个类的对象,都可以充当锁。
     *          要求:多个线程必须要共用同一把锁。
     *
     *       补充:在实现Runnable接口创建多线程的方式中,我们可以考虑使用this充当同步监视器。
     *  方式二:同步方法。
     *     如果操作共享数据的代码完整的声明在一个方法中,我们不妨将此方法声明同步的。
     *
     *
     *  5.同步的方式,解决了线程的安全问题。---好处
     *    操作同步代码时,只能有一个线程参与,其他线程等待。相当于是一个单线程的过程,效率低。 ---局限性
     *
     * @author shkstart
     * @create 2019-02-13 下午 4:47
     */
    class Window1 implements Runnable{
    
        private int ticket = 100;
    //    Object obj = new Object();
    //    Dog dog = new Dog();
        @Override
        public void run() {
    //        Object obj = new Object();
            while(true){
                synchronized (this){//此时的this:唯一的Window1的对象   //方式二:synchronized (dog) {
    
                    if (ticket > 0) {
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
    
    
                        ticket--;
                    } else {
                        break;
                    }
                }
            }
        }
    }
    
    
    public class WindowTest1 {
        public static void main(String[] args) {
            Window1 w = new Window1();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    
    }
    
    
    class Dog{
    
    }

    2 实例二使用同步代码块解决继承Thread类的方式的线程安全问题

    package com.atguigu.java;
    
    /**
     * @author shkstart
     * @create 2019-02-15 上午 11:15
     */
    /**
     * 使用同步代码块解决继承Thread类的方式的线程安全问题
     *
     * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式
     *
     * 说明:在继承Thread类创建多线程的方式中,慎用this充当同步监视器,考虑使用当前类充当同步监视器。
     *
     * @author shkstart
     * @create 2019-02-13 下午 4:20
     */
    class Window2 extends Thread{
    
    
        private static int ticket = 100;
    
        private static Object obj = new Object();
    
        @Override
        public void run() {
    
            while(true){
                //正确的
    //            synchronized (obj){
                synchronized (Window2.class){//Class clazz = Window2.class,Window2.class只会加载一次
                    //错误的方式:this代表着t1,t2,t3三个对象
    //              synchronized (this){
    
                    if(ticket > 0){
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(getName() + ":卖票,票号为:" + ticket);
                        ticket--;
                    }else{
                        break;
                    }
                }
    
            }
    
        }
    }
    
    
    public class WindowTest2 {
        public static void main(String[] args) {
            Window2 t1 = new Window2();
            Window2 t2 = new Window2();
            Window2 t3 = new Window2();
    
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
    
        }
    }

    3 实例三使用同步方法解决实现Runnable接口的线程安全问题

    package com.atguigu.java;
    
    /**
     * 使用同步方法解决实现Runnable接口的线程安全问题
     *
     *
     *  关于同步方法的总结:
     *  1. 同步方法仍然涉及到同步监视器,只是不需要我们显式的声明。
     *  2. 非静态的同步方法,同步监视器是:this
     *     静态的同步方法,同步监视器是:当前类本身
     *
     * @author shkstart
     * @create 2019-02-15 上午 11:35
     */
    
    
    class Window3 implements Runnable {
    
        private int ticket = 100;
    
        @Override
        public void run() {
            while (true) {
    
                show();
            }
        }
    
        private synchronized void show(){//同步监视器:this
            //synchronized (this){
    
                if (ticket > 0) {
    
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
    
                    ticket--;
                }
            //}
        }
    }
    
    
    public class WindowTest3 {
        public static void main(String[] args) {
            Window3 w = new Window3();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    
    }

    4 实例四使用同步方法处理继承Thread类的方式中的线程安全问题

    package com.atguigu.java;
    
    /**
     * 使用同步方法处理继承Thread类的方式中的线程安全问题
     *
     * @author shkstart
     * @create 2019-02-15 上午 11:43
     */
    class Window4 extends Thread {
    
    
        private static int ticket = 100;
    
        @Override
        public void run() {
    
            while (true) {
    
                show();
            }
    
        }
        private static synchronized void show(){//同步监视器:Window4.class
            //private synchronized void show(){ //同步监视器:t1,t2,t3。此种解决方式是错误的
            if (ticket > 0) {
    
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
                ticket--;
            }
        }
    }
    
    
    public class WindowTest4 {
        public static void main(String[] args) {
            Window4 t1 = new Window4();
            Window4 t2 = new Window4();
            Window4 t3 = new Window4();
    
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
    
        }
    }

    5 使用同步机制将单例模式中的懒汉式改写为线程安全的

    package com.atguigu.java1;
    
    /**
     * 使用同步机制将单例模式中的懒汉式改写为线程安全的
     *
     * @author shkstart
     * @create 2019-02-15 下午 2:50
     */
    public class BankTest {
    
    }
    
    class Bank{
    
        private Bank(){}
    
        private static Bank instance = null;
    
        public static Bank getInstance(){
            //方式一:效率稍差
    //        synchronized (Bank.class) {
    //            if(instance == null){
    //
    //                instance = new Bank();
    //            }
    //            return instance;
    //        }
            //方式二:效率更高
            if(instance == null){
    
                synchronized (Bank.class) {
                    if(instance == null){
    
                        instance = new Bank();
                    }
    
                }
            }
            return instance;
        }
    
    }

    6 死锁的演示

    package com.atguigu.java1;
    
    /**
     * 演示线程的死锁问题
     *
     * 1.死锁的理解:不同的线程分别占用对方需要的同步资源不放弃,
     * 都在等待对方放弃自己需要的同步资源,就形成了线程的死锁
     *
     * 2.说明:
     * 1)出现死锁后,不会出现异常,不会出现提示,只是所有的线程都处于阻塞状态,无法继续
     * 2)我们使用同步时,要避免出现死锁。
     *
     * @author shkstart
     * @create 2019-02-15 下午 3:20
     */
    public class ThreadTest {
    
        public static void main(String[] args) {
    
            StringBuffer s1 = new StringBuffer();
            StringBuffer s2 = new StringBuffer();
    
    
            new Thread(){
                @Override
                public void run() {
    
                    synchronized (s1){
    
                        s1.append("a");
                        s2.append("1");
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
    
                        synchronized (s2){
                            s1.append("b");
                            s2.append("2");
    
                            System.out.println(s1);
                            System.out.println(s2);
                        }
    
    
                    }
    
                }
            }.start();
    
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (s2){
    
                        s1.append("c");
                        s2.append("3");
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        synchronized (s1){
                            s1.append("d");
                            s2.append("4");
    
                            System.out.println(s1);
                            System.out.println(s2);
                        }
    
    
                    }
    
    
    
                }
            }).start();
    
    
        }
    
    
    }
    package com.atguigu.java1;
    //死锁的演示
    class A {
        public synchronized void foo(B b) { //同步监视器:A类的对象:a
            System.out.println("当前线程名: " + Thread.currentThread().getName()
                    + " 进入了A实例的foo方法"); ////        try {
    //            Thread.sleep(200);
    //        } catch (InterruptedException ex) {
    //            ex.printStackTrace();
    //        }
            System.out.println("当前线程名: " + Thread.currentThread().getName()
                    + " 企图调用B实例的last方法"); //
            b.last();
        }
    
        public synchronized void last() {//同步监视器:A类的对象:a
            System.out.println("进入了A类的last方法内部");
        }
    }
    
    class B {
        public synchronized void bar(A a) {//同步监视器:b
            System.out.println("当前线程名: " + Thread.currentThread().getName()
                    + " 进入了B实例的bar方法"); ////        try {
    //            Thread.sleep(200);
    //        } catch (InterruptedException ex) {
    //            ex.printStackTrace();
    //        }
            System.out.println("当前线程名: " + Thread.currentThread().getName()
                    + " 企图调用A实例的last方法"); //
            a.last();
        }
    
        public synchronized void last() {//同步监视器:b
            System.out.println("进入了B类的last方法内部");
        }
    }
    
    public class DeadLock implements Runnable {
        A a = new A();
        B b = new B();
    
        public void init() {
            Thread.currentThread().setName("主线程");
            // 调用a对象的foo方法
            a.foo(b);
            System.out.println("进入了主线程之后");
        }
    
        public void run() {
            Thread.currentThread().setName("副线程");
            // 调用b对象的bar方法
            b.bar(a);
            System.out.println("进入了副线程之后");
        }
    
        public static void main(String[] args) {
            DeadLock dl = new DeadLock();
            new Thread(dl).start();
    
    
            dl.init();
        }
    }

    7 解决线程安全问题的方式三:Lock锁  --- JDK5.0新增

    package com.atguigu.java1;
    
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * 解决线程安全问题的方式三:Lock锁  --- JDK5.0新增
     *
     * 1. 面试题:synchronized 与 Lock的异同?
     *   相同:二者都可以解决线程安全问题
     *   不同:synchronized机制在执行完相应的同步代码以后,自动的释放同步监视器
     *        Lock需要手动的启动同步(lock()),同时结束同步也需要手动的实现(unlock())
     *
     * 2.优先使用顺序:
     * Lock  同步代码块(已经进入了方法体,分配了相应资源)  同步方法(在方法体之外)
     *
     *
     *  面试题:如何解决线程安全问题?有几种方式
     * @author shkstart
     * @create 2019-02-15 下午 3:38
     */
    class Window implements Runnable{
    
        private int ticket = 100;
        //1.实例化ReentrantLock
        private ReentrantLock lock = new ReentrantLock();
    
        @Override
        public void run() {
            while(true){
                try{
    
                    //2.调用锁定方法lock()
                    lock.lock();
    
                    if(ticket > 0){
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ":售票,票号为:" + ticket);
                        ticket--;
                    }else{
                        break;
                    }
                }finally {
                    //3.调用解锁方法:unlock()
                    lock.unlock();
                }
    
            }
        }
    }
    
    public class LockTest {
        public static void main(String[] args) {
            Window w = new Window();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
  • 相关阅读:
    MySQL数据类型2
    MySQL数据类型1
    powerdesigner使用之——从“概念模型”到“物理模型”
    javascript中click和onclick的区别
    eclipse将javaSE项目导出成可执行jar包
    配置SQLServer,允许远程连接
    VirtualBox虚拟机中安装XP系统
    fastjson 的使用总结
    idea前端页面不刷新----springboot
    后台可以用layui快速开发
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14231979.html
Copyright © 2011-2022 走看看