zoukankan      html  css  js  c++  java
  • synchronized 线程同步-类级别锁定

    1.demo

    说明:代码中通过 printNum 方法传入参数判断 a、b 分别对 num 这个参数的值进行了修改。

    package demo1;
    
    import sun.applet.Main;
    
    public class MyThread2 extends Thread {
    
        private  int num = 0;
        public synchronized void printNum(String str) {
            try {
                if (str.equals("a")) {
                    num = 1000;
                    System.err.println("thread -> A over");
                    Thread.sleep(1000);
                } else if (str.equals("b")) {
                    num = 200;
                    System.err.println("thread -> B over");
                }
                System.err.println("str:" + str + "	num:" + num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            //2个不同的对象,只能new一次
            final MyThread2 n1 = new MyThread2();
            final MyThread2 n2 = new MyThread2();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    n1.printNum("a");
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    n2.printNum("b");
                }
            });
            t1.start();
            t2.start();
        }
    }
    结果输出:

          thread -> A over
          thread -> B over

          a休眠2秒后
          str:b num:200
          str:a num:1000

     

    描述:

    synchronized  结果中并没有发生作用,正常的结果应该是:先执行完a,在执行b。

    Thread.sleep(1000): 让当前线程挂起、停顿,交出当前线程占用cpu的时间(在某个时间内,把资源给别人,当前线程在这个时间不占用),使其他线程与当前的线程抢占cpu资源,让2个线程重新分配资源,让另一个线程抢到资源并执行。

    2.demo

    package demo1;
    
    import sun.applet.Main;
    public class MyThread2 extends Thread {
    
        private static int num = 0;
        public static synchronized void printNum(String str) {
           
            try {
                if (str.equals("a")) {
                    num = 1000;
                    System.err.println("thread -> A over");
                    Thread.sleep(1000);
                } else if (str.equals("b")) {
                    num = 200;
                    System.err.println("thread -> B over");
                }
                System.err.println("str:" + str + "	num:" + num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            //2个不同的对象,只能new一次
            final MyThread2 n1 = new MyThread2();
            final MyThread2 n2 = new MyThread2();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    n1.printNum("a");
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    n2.printNum("b");
                }
            });
            t1.start();
            t2.start();
        }
    }
    结果输出:

          thread -> A over
          str:a num:1000
          thread -> B over
          str:b num:200

     

    描述:多个线程多个锁,每个线程都会去拿到属于自己的锁,分别获得后,执行  synchronized  修饰的方法。

       1.synchronized  取得的锁都是对象锁,而不是把一段代码、方法的锁,多个线程就持有该所属的对象锁。2个对象,线程获取的就是2个不同的锁(相互午影响)。

       2.有一种情况就是【相同的锁】,就是在该方法 synchronized 使用static 关键字,表示锁定class类,类级别的锁(独占class类)。

  • 相关阅读:
    C语言I博客作业02
    第一次C语言作业
    C语言I博客作业02
    网页常用分享代码
    js生成验证码并验证
    js时间格式的转换
    Git 常用命令
    ASP.NET MVC中使用事务写法
    数据库游标导入数据
    js截取所需字符串长度
  • 原文地址:https://www.cnblogs.com/xxt19970908/p/6954258.html
Copyright © 2011-2022 走看看