• 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
  • 相关阅读:
    HDU 1847 Good Luck in CET-4 Everybody! (博弈)
    HDU 1242 Rescue (BFS+优先队列)
    POJ 1061 青蛙的约会(扩展欧几里德算法)
    UVa 1610 Party Games(思维)
    hdu 1025LIS思路同1257 二分求LIS
    hdu 1257 LIS (略坑5
    什么是可串行化MVCC
    老板又出难题,气得我写了个自动化软件
    opencv学习一:概述与环境搭建(python3.7+pycharm)
    人工智能能力提升指导总结
  • 原文地址:https://www.cnblogs.com/blogxiao/p/7743378.html
Copyright © 2020-2023  润新知