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
原文地址:https://www.cnblogs.com/blogxiao/p/7743378.html