zoukankan      html  css  js  c++  java
  • java synchronized 用法

    1.synchronized 可以作用于类 或者 对象。

    作用于类举例:将类的静态变量进行加锁。或者对类的静态代码块进行加锁。

            影响:多个线程调用对于同一个对象或者不同对象调用start() 方法时候,只能有一个线程获取锁,其他的线程等待。

    作用于对象举例:对对象某个非静态变量加锁,或者对于方法加锁。

            影响:多个线程调用同一个对象的start(),只有一个线程会获取锁。当多个线程调用的不同对象,会对不同对象的方法各自加锁,达不到预期的结果。

    作用于类代码:

    package test.thread;
    
    /*
     *测试synchronized 关键字 
     */
    public  class   TestSync implements Runnable {
        private static Integer count;   //注意这个static 关键字 修饰count变量,这个变量属于类变量
         
           public TestSync() {
              count = 100;
           }
           
           /*
            *1 测试锁方法
            */
           public synchronized void inc(){  //锁方法
              
               System.out.println(Thread.currentThread().getName() + ":" + (count--));
           }
           
           public  void run() {
               while(count>0){
                   inc();
               }
               }
           
           
           /*
            *2 测试锁对象
            */
           
          /* public  void run() {
              synchronized(count) {  //锁类
                 for (int i = 0; i < 20; i++) {
                    try {
                       System.out.println(Thread.currentThread().getName() + ":" + (count++));
                       Thread.sleep(100);
                    } catch (InterruptedException e) {
                       e.printStackTrace();
                    }
                 }
              }
           }*/
           
      
         
           public int getCount() {
              return count;
           }
           
           
           public static void main(String[] args){
    //3
    /* TestSync syncThread = new TestSync(); Thread thread1 = new Thread(syncThread, "SyncThread1"); Thread thread2 = new Thread(syncThread, "SyncThread2");*/
    //4
    Thread thread1 = new Thread(new TestSync(), "SyncThread1"); Thread thread2 = new Thread(new TestSync(), "SyncThread2"); thread1.start(); thread2.start(); } }

    结果:3 和 4 运行的结果是100 。。。。 1

    作用于对象代码:

    package test.thread;
    
    /*
     *测试synchronized 关键字 
     */
    public  class   TestSync implements Runnable {
        private Integer count;   //注意这没有static 关键字 修饰count变量,这个变量属于对象变量
         
           public TestSync() {
              count = 100;
           }
           
           /*
            *1 测试锁方法
            */
           public synchronized void inc(){  //锁方法
              
               System.out.println(Thread.currentThread().getName() + ":" + (count--));
           }
           
           public  void run() {
               while(count>0){
                   inc();
               }
               }
           
           
           /*
            *2 测试锁对象
            */
           
          /* public  void run() {
              synchronized(count) {  //锁类
                 for (int i = 0; i < 20; i++) {
                    try {
                       System.out.println(Thread.currentThread().getName() + ":" + (count++));
                       Thread.sleep(100);
                    } catch (InterruptedException e) {
                       e.printStackTrace();
                    }
                 }
              }
           }*/
           
      
         
           public int getCount() {
              return count;
           }
           
           
           public static void main(String[] args){
    //3
             /*  TestSync syncThread = new TestSync();
               Thread thread1 = new Thread(syncThread, "SyncThread1");
               Thread thread2 = new Thread(syncThread, "SyncThread2");*/
    //4
    
               Thread thread1 = new Thread(new TestSync(), "SyncThread1");
               Thread thread2 = new Thread(new TestSync(), "SyncThread2");
               thread1.start();
               thread2.start();
               
           }
    
    }
    
    结果:3运行的结果是100  。。。。 1            4运行的结果可能是:100,100,99,98,97,97
  • 相关阅读:
    Developer 转型记:一个开发平台的“魔力”
    实践录丨如何在鲲鹏服务器OpenEuler操作系统中快速部署OpenGauss数据库
    一图看懂华为云DevCloud如何应对敏捷开发的测试挑战
    华为云GaussDB(DWS)内存知识点,你知道吗?
    在人工智能时代追逐的“后浪”
    【华为云技术分享】DLI跨源|当DLI遇见MongoDB
    授人以渔:stm32资料查询技巧
    云小课 | IPv4枯了,IPv6来了
    揭秘淘宝平台广告策略,拆解最佳投放实践
    520了,用32做个简单的小程序
  • 原文地址:https://www.cnblogs.com/blogxiao/p/7743378.html
Copyright © 2011-2022 走看看