3、静态方法(类)同步
  代码段三:
1 package com.paddx.test.concurrent;
2
3  public class SynchronizedTest {
4      public static synchronized void method1(){
5          System.out.println("Method 1 start");
6          try {
7              System.out.println("Method 1 execute");
8              Thread.sleep(3000);
9          } catch (InterruptedException e) {
10              e.printStackTrace();
11          }
12          System.out.println("Method 1 end");
13      }
14
15      public static synchronized void method2(){
16          System.out.println("Method 2 start");
17          try {
18              System.out.println("Method 2 execute");
19              Thread.sleep(1000);
20          } catch (InterruptedException e) {
21              e.printStackTrace();
22          }
23          System.out.println("Method 2 end");
24      }
25
26      public static void main(String[] args) {
27          final SynchronizedTest test = new SynchronizedTest();
28          final SynchronizedTest test2 = new SynchronizedTest();
29
30          new Thread(new Runnable() {
31              @Override
32              public void run() {
33                  test.method1();
34              }
35          }).start();
36
37          new Thread(new Runnable() {
38              @Override
39              public void run() {
40                  test2.method2();
41              }
42          }).start();
43      }
44  }
  执行结果如下,对静态方法的同步本质上是对类的同步(静态方法本质上是属于类的方法,而不是对象上的方法),所以即使test和test2属于不同的对象,但是它们都属于SynchronizedTest类的实例,所以也只能顺序的执行method1和method2,不能并发执行。
  Method 1 start
  Method 1 execute
  Method 1 end
  Method 2 start
  Method 2 execute
  Method 2 end
  4、代码块同步
  代码段四:
1 package com.paddx.test.concurrent;
2
3 public class SynchronizedTest {
4     public void method1(){
5         System.out.println("Method 1 start");
6         try {
7             synchronized (this) {
8                 System.out.println("Method 1 execute");
9                 Thread.sleep(3000);
10             }
11         } catch (InterruptedException e) {
12             e.printStackTrace();
13         }
14         System.out.println("Method 1 end");
15     }
16
17     public void method2(){
18         System.out.println("Method 2 start");
19         try {
20             synchronized (this) {
21                 System.out.println("Method 2 execute");
22                 Thread.sleep(1000);
23             }
24         } catch (InterruptedException e) {
25             e.printStackTrace();
26         }
27         System.out.println("Method 2 end");
28     }
29
30     public static void main(String[] args) {
31         final SynchronizedTest test = new SynchronizedTest();
32
33         new Thread(new Runnable() {
34             @Override
35             public void run() {
36                 test.method1();
37             }
38         }).start();
39
40         new Thread(new Runnable() {
41             @Override
42             public void run() {
43                 test.method2();
44             }
45         }).start();
46     }
47 }
  执行结果如下,虽然线程1和线程2都进入了对应的方法开始执行,但是线程2在进入同步块之前,需要等待线程1中同步块执行完成。
  Method 1 start
  Method 1 execute
  Method 2 start
  Method 1 end
  Method 2 execute
  Method 2 end