0
点赞
收藏
分享

微信扫一扫

synchronized的用法

编程练习生J 2022-02-27 阅读 67
java

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeG1oLXN4aC0xMzE0,size_17,color_FFFFFF,t_70,g_se,x_16一 修饰方法 

 

 

Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起来的范围,而修饰方法范围是整个函数。

 

例如:

 

方法一

 

public synchronized void method()

{

   // todo

}

方法二

 

复制代码

public void method()

{

   synchronized(this) {

      // todo

   }

}

复制代码

写法一修饰的是一个方法,写法二修饰的是一个代码块,但写法一与写法二是等价的,都是锁定了整个方法时的内容。

 

synchronized关键字不能继承。 

虽然可以使用synchronized来定义方法,但synchronized并不属于方法定义的一部分,因此,synchronized关键字不能被继承。如果在父类中的某个方法使用了synchronized关键字,而在子类中覆盖了这个方法,在子类中的这个方法默认情况下并不是同步的,而必须显式地在子类的这个方法中加上synchronized关键字才可以。当然,还可以在子类方法中调用父类中相应的方法,这样虽然子类中的方法不是同步的,但子类调用了父类的同步方法,因此,子类的方法也就相当于同步了。这两种方式的例子代码如下: 

在子类方法中加上synchronized关键字

 

复制代码

class Parent {

   public synchronized void method() { }

}

class Child extends Parent {

   public synchronized void method() { }

}

复制代码

在子类方法中调用父类的同步方法

 

复制代码

class Parent {

   public synchronized void method() { }

}

class Child extends Parent {

   public void method() { super.method(); }

复制代码

在定义接口方法时不能使用synchronized关键字。

构造方法不能使用synchronized关键字,但可以使用synchronized代码块来进行同步。 

二 修饰一个代码块

 

1)一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞

 

注意下面两个程序的区别

 

复制代码

class SyncThread implements Runnable {

       private static int count;

 

       public SyncThread() {

          count = 0;

       }

 

       public void run() {

          synchronized(this) {

             for (int i = 0; i < 5; i++) {

                try {

                   System.out.println(Thread.currentThread().getName() + ":" + (count++));

                   Thread.sleep(100);

                } catch (InterruptedException e) {

                   e.printStackTrace();

                }

             }

          }

       }

 

       public int getCount() {

          return count;

       }

}

 

public class Demo00 {

    public static void main(String args[]){

    //test01

    //SyncThread s1 = new SyncThread();

    //SyncThread s2 = new SyncThread();

    //Thread t1 = new Thread(s1);

    //Thread t2 = new Thread(s2);

    //test02        

        SyncThread s = new SyncThread();

        Thread t1 = new Thread(s);

        Thread t2 = new Thread(s);

        

        t1.start();

        t2.start();

    }

}

复制代码

test01的运行结果

 

 

 

 

test02的运行结果

 

 

 

 

 

 

 

当两个并发线程(thread1和thread2)访问同一个对象(syncThread)中的synchronized代码块时,在同一时刻只能有一个线程得到执行,另一个线程受阻塞,必须等待当前线程执行完这个代码块以后才能执行该代码块。Thread1和thread2是互斥的,因为在执行synchronized代码块时会锁定当前的对象,只有执行完该代码块才能释放该对象锁,下一个线程才能执行并锁定该对象

 

为什么上面的例子中thread1和thread2同时在执行。这是因为synchronized只锁定对象,每个对象只有一个锁(lock)与之相关联。

 

复制代码

class Counter implements Runnable{

   private int count;

 

   public Counter() {

      count = 0;

   }

 

   public void countAdd() {

      synchronized(this) {

         for (int i = 0; i < 5; i ++) {

            try {

               System.out.println(Thread.currentThread().getName() + ":" + (count++));

               Thread.sleep(100);

            } catch (InterruptedException e) {

               e.printStackTrace();

            }

         }

      }

   }

 

   //非synchronized代码块,未对count进行读写操作,所以可以不用synchronized

   public void printCount() {

      for (int i = 0; i < 5; i ++) {

         try {

            System.out.println(Thread.currentThread().getName() + " count:" + count);

            Thread.sleep(100);

         } catch (InterruptedException e) {

            e.printStackTrace();

         }

      }

   }

 

   public void run() {

      String threadName = Thread.currentThread().getName();

      if (threadName.equals("A")) {

         countAdd();

      } else if (threadName.equals("B")) {

         printCount();

      }

   }

}

 

public class Demo00{

    public static void main(String args[]){

        Counter counter = new Counter();

        Thread thread1 = new Thread(counter, "A");

        Thread thread2 = new Thread(counter, "B");

        thread1.start();

        thread2.start();

    }

}

复制代码

 

 

可以看见B线程的调用是非synchronized,并不影响A线程对synchronized部分的调用。从上面的结果中可以看出一个线程访问一个对象的synchronized代码块时,别的线程可以访问该对象的非synchronized代码块而不受阻塞。

 

3)指定要给某个对象加锁

 

复制代码

/**

 * 银行账户类

 */

class Account {

   String name;

   float amount;

 

   public Account(String name, float amount) {

      this.name = name;

      this.amount = amount;

   }

   //存钱

   public void deposit(float amt) {

      amount += amt;

      try {

         Thread.sleep(100);

      } catch (InterruptedException e) {

         e.printStackTrace();

      }

   }

   //取钱

   public void withdraw(float amt) {

      amount -= amt;

      try {

         Thread.sleep(100);

      } catch (InterruptedException e) {

         e.printStackTrace();

      }

   }

 

   public float getBalance() {

      return amount;

   }

}

 

/**

 * 账户操作类

 */

class AccountOperator implements Runnable{

   private Account account;

   public AccountOperator(Account account) {

      this.account = account;

   }

 

   public void run() {

      synchronized (account) {

         account.deposit(500);

         account.withdraw(500);

         System.out.println(Thread.currentThread().getName() + ":" + account.getBalance());

      }

   }

}

 

public class Demo00{

    

    //public static final Object signal = new Object(); // 线程间通信

 

举报

相关推荐

0 条评论