被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法的调用者是同一个,所以两个方法用的是同一个锁,先调用方法的先执行。
- public class Demo {
- public static void main(String[] args) {
- Number number = new Number();
-
- new Thread(() -> number.getOne()).start();
-
- new Thread(() -> number.getTwo()).start();
- }
- }
-
- class Number {
- public synchronized void getOne() {
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
one
two
被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法的调用者是同一个,所以两个方法用的是同一个锁,先调用方法的先执行,第二个方法只有在第一个方法执行完释放锁之后才能执行。
- public class Demo {
- public static void main(String[] args) {
- Number number = new Number();
-
- new Thread(() -> number.getOne()).start();
-
- new Thread(() -> number.getTwo()).start();
- }
- }
-
- class Number {
-
- public synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
//等待3秒
one
two
新增的方法没有被synchronized修饰,不是同步方法,不受锁的影响,所以不需要等待。其他线程共用了一把锁,所以还需要等待。
- public class Demo {
- public static void main(String[] args) {
- Number number = new Number();
-
- new Thread(() -> number.getOne()).start();
-
- new Thread(() -> number.getTwo()).start();
-
- new Thread(() -> number.getThree()).start();
- }
- }
-
- class Number {
- public synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
-
- public void getThree() {
- System.out.println("three");
- }
- }
输出结果如下:
three
//等待3秒
one
two
被synchronized修饰的方法,锁的对象是方法的调用者。因为用了两个对象调用各自的方法,所以两个方法的调用者不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。
- public class Demo {
- public static void main(String[] args) {
- Number numberOne = new Number();
- Number numberTwo = new Number();
-
- new Thread(() -> numberOne.getOne()).start();
-
- new Thread(() -> numberTwo.getTwo()).start();
- }
- }
-
- class Number {
- public synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
two
//等待3秒
one
被synchronized和static修饰的方法,锁的对象是类的class对象。仅仅被synchronized修饰的方法,锁的对象是方法的调用者。因为两个方法锁的对象不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。
- public class Demo {
- public static void main(String[] args) {
- Number number = new Number();
-
- new Thread(() -> number.getOne()).start();
-
- new Thread(() -> number.getTwo()).start();
- }
- }
-
- class Number {
- public static synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
two
//等待3秒
one
被synchronized和static修饰的方法,锁的对象是类的class对象。因为两个同步方法都被static修饰了,所以两个方法用的是同一个锁,后调用的方法需要等待先调用的方法。
- public class Demo {
- public static void main(String[] args) {
- Number number = new Number();
-
- new Thread(() -> number.getOne()).start();
-
- new Thread(() -> number.getTwo()).start();
- }
- }
-
- class Number {
- public static synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public static synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
//等待3秒
one
two
被synchronized和static修饰的方法,锁的对象是类的class对象。仅仅被synchronized修饰的方法,锁的对象是方法的调用者。即便是用同一个对象调用两个方法,锁的对象也不是同一个,所以两个方法用的不是同一个锁,后调用的方法不需要等待先调用的方法。
- public class Demo {
- public static void main(String[] args) {
- Number numberOne = new Number();
- Number numberTwo = new Number();
-
- new Thread(() -> numberOne.getOne()).start();
-
- new Thread(() -> numberTwo.getTwo()).start();
- }
- }
-
- class Number {
- public static synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
two
//等待3秒
one
被synchronized和static修饰的方法,锁的对象是类的class对象。因为两个同步方法都被static修饰了,即便用了两个不同的对象调用方法,两个方法用的还是同一个锁,后调用的方法需要等待先调用的方法。
- public class Demo {
- public static void main(String[] args) {
- Number numberOne = new Number();
- Number numberTwo = new Number();
-
- new Thread(() -> numberOne.getOne()).start();
-
- new Thread(() -> numberTwo.getTwo()).start();
- }
- }
-
- class Number {
- public static synchronized void getOne() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("one");
- }
-
- public static synchronized void getTwo() {
- System.out.println("two");
- }
- }
输出结果如下:
//等待3秒
one
two