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
  • 相关阅读:
    android 进程/线程管理(一)----消息机制的框架
    android的屏幕保持常亮
    android network develop(3)----Xml Parser
    android network develop(2)----network status check
    android network develop(1)----doing network background
    android 开发小记
    转 Android中shape中的属性大全
    转 Android学习 之 ColorStateList按钮文字变色
    《大话设计模式》c++实现 建造者模式
    《大话设计模式》c++实现 外观模式
  • 原文地址:https://www.cnblogs.com/blogxiao/p/7743378.html
Copyright © 2011-2022 走看看