Java并发编程:Synchronized及其实现原理

发表于:2016-4-29 10:05

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:liuxiaopeng    来源:51Testing软件测试网采编

#
java
分享:
  一、Synchronized的基本使用
  Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法。Synchronized的作用主要有三个:(1)确保线程互斥的访问同步代码(2)保证共享变量的修改能够及时可见(3)有效解决重排序问题。从语法上讲,Synchronized总共有三种用法:
  (1)修饰普通方法
  (2)修饰静态方法
  (3)修饰代码块
  接下来我就通过几个例子程序来说明一下这三种使用方式(为了便于比较,三段代码除了Synchronized的使用方式不同以外,其他基本保持一致)。
  1、没有同步的情况:
  代码段一:
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             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 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
29         new Thread(new Runnable() {
30             @Override
31             public void run() {
32                 test.method1();
33             }
34         }).start();
35
36         new Thread(new Runnable() {
37             @Override
38             public void run() {
39                 test.method2();
40             }
41         }).start();
42     }
43 }
  执行结果如下,线程1和线程2同时进入执行状态,线程2执行速度比线程1快,所以线程2先执行完成,这个过程中线程1和线程2是同时执行的。
  Method 1 start
  Method 1 execute
  Method 2 start
  Method 2 execute
  Method 2 end
  Method 1 end
  2、对普通方法同步:
  代码段二:
1 package com.paddx.test.concurrent;
2
3 public class SynchronizedTest {
4     public 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 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
29         new Thread(new Runnable() {
30             @Override
31             public void run() {
32                 test.method1();
33             }
34         }).start();
35
36         new Thread(new Runnable() {
37             @Override
38             public void run() {
39                 test.method2();
40             }
41         }).start();
42     }
43 }
  执行结果如下,跟代码段一比较,可以很明显的看出,线程2需要等待线程1的method1执行完成才能开始执行method2方法。
  Method 1 start
  Method 1 execute
  Method 1 end
  Method 2 start
  Method 2 execute
  Method 2 end
31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号