1、介绍

Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍可以访问该object中的非加锁代码块。

2、修饰对象

2.1、修饰this,当前对象,这里的this指的是执行这段代码的对象,synchronized得到的锁就是this这个对象的锁。

线程thread1 访问对象testSy1的带有同步代码块的add()时,其他线程可以访问该对象的add()方法吗?

public class TestSy1 implements Runnable{
private int number; TestSy1(){
number = 0;
} public void add(){
synchronized (this){
for(int i=0;i<4;i++){
try{
System.out.println(Thread.currentThread().getName()+":thread:"+(number++));
Thread.sleep(500);
}catch (Exception e){
System.out.println("异常");
}
}
}
System.out.println("add");
} public void show(){
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + " 非同步:" + number);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} System.out.println("show");
} @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
add();
}else{
add();
}
} public static void main(String[] args){
TestSy1 testSy1 = new TestSy1();
Thread thread1 = new Thread(testSy1,"thread1");
Thread thread2 = new Thread(testSy1,"thread2");
thread1.start();
thread2.start();
}
}

结果如下:

thread1:thread:0
thread1:thread:1
thread1:thread:2
thread1:thread:3
thread2:thread:4
thread2:thread:5
thread2:thread:6
thread2:thread:7
可见,其他线程不能访问add()方法。

修改一下run()

 @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
add();
}else{
show();
}
}

结果如下:

thread1:thread:0
thread2 非同步:1
thread1:thread:1
thread2 非同步:2
thread2 非同步:2
thread1:thread:2
thread1:thread:3
thread2 非同步:4
thread2 非同步:4
其他线程可以访问该对象的show()方法。

那其他线程可以访问,其他对象的同步方法吗?

public class TestSy1 implements Runnable{
private int number; TestSy1(){
number = 0;
} public void add(){
synchronized (this){
for(int i=0;i<4;i++){
try{
System.out.println(Thread.currentThread().getName()+":thread:"+(number++));
Thread.sleep(500);
}catch (Exception e){
System.out.println("异常");
}
}
}
System.out.println("add");
} public void show(){
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + " 非同步:" + number);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} System.out.println("show");
}
@Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
add();
}else{
add();
}
} public static void main(String[] args){
TestSy1 testSy1 = new TestSy1();
TestSy1 testSy2 = new TestSy1();
Thread thread1 = new Thread(testSy1,"thread1");
Thread thread2 = new Thread(testSy2,"thread2");
thread1.start();
thread2.start();
}
}

结果如下:

thread1:thread:0
thread2:thread:0
thread1:thread:1
thread2:thread:1
thread1:thread:2
thread2:thread:2
thread1:thread:3
thread2:thread:3

总结:修饰this和修饰非静态方法一样。线程A访问对象A的带有同步代码块的方法A时,其他线程可以访问该对象的非同步方法和其他对象的所有方法。

3、修饰方法

3.1、修饰非静态方法

public class TestMethod implements Runnable{
private static int number; TestMethod(){
number = 0;
} public synchronized void m1(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public void m2(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
}
  @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
m1();
}else{
m1();
}
}
    public static void main(String[] args){
TestMethod testMethod = new TestMethod();
Thread thread1 = new Thread(testMethod,"thread1");
Thread thread2 = new Thread(testMethod,"thread2");
thread1.start();
thread2.start();
}
}

上述代码中,synchronized 修饰了非静态方法m1(),然后生成两个线程分别访问对象testMethod的m1()方法,结果如下:

thread1:0
thread1:1
thread1:2
thread2:3
thread2:4
thread2:5
可见,synchronized修饰非静态方法时,线程thread1访问testMethod对象的同步方法m1()时,其他线程不能访问testMethod对象的同步方法m1(),那它可以访问testMethod的非同步方法m2()吗?

对上述代码的run()方法进行修改

 @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
m1();
}else{
m2();//让thread2访问非同步方法m2()
} }

结果如下:

thread1:0
thread2:1
thread1:2
thread2:3
thread1:4
thread2:5
可见,线程thread2可以访问对象testMethod的非同步方法m2(),那线程thread2又可以访问其他对象的同步和非同步方法吗?
package p02;

public class TestMethod implements Runnable{
private static int number; TestMethod(){
number = 0;
} @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
m1();
}else{
m1();
}
} public synchronized void m1(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public void m2(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public static void main(String[] args){
TestMethod testMethod = new TestMethod();
TestMethod testMethod1 = new TestMethod();
Thread thread1 = new Thread(testMethod,"thread1");
Thread thread2 = new Thread(testMethod1,"thread2");
thread1.start();
thread2.start();
}
}

结果如下:

thread1:0
thread2:1
thread1:2
thread2:3
thread2:4
thread1:5
可见,其他线程可以访问对象testMethod1的同步方法。

总结:

修饰非静态方法时,线程A访问对象A的非静态同步方法A时,其他线程可以访问该对象的非同步方法以及其他对象的任何方法

3.2、修饰静态方法

因为静态方法是属于类的,不是对象的,所以就不测试其他对象了,感兴趣的话可以自己试一试。

public class TestMethod implements Runnable{
private static int number; TestMethod(){
number = 0;
} @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
m1();
}else{
m1();
}
} public static synchronized void m1(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public void m2(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public static void main(String[] args){
TestMethod testMethod = new TestMethod();
Thread thread1 = new Thread(testMethod,"thread1");
Thread thread2 = new Thread(testMethod,"thread2");
thread1.start();
thread2.start();
}
}

上述代码,m1()是静态同步方法,当两个线程同时访问该方法时会发生什么?

thread1:0
thread1:1
thread1:2
thread2:3
thread2:4
thread2:5
当线程thread1访问静态同步方法m1()时,其他线程不能访问m1(),可以访问m2(),结果如下:

thread1:0
  thread2:1
  thread1:2
  thread2:3
  thread1:4
  thread2:5

如果m2()是非静态同步方法呢?

public class TestMethod implements Runnable{
private static int number; TestMethod(){
number = 0;
} @Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
m1();
}else{
m2();
}
} public static synchronized void m1(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public synchronized void m2(){
for(int i=0;i<3;i++){
try {
System.out.println(Thread.currentThread().getName()+":"+(number++));
Thread.sleep(200);
}catch (Exception e){
System.out.println("异常");
}
}
} public static void main(String[] args){
TestMethod testMethod = new TestMethod();
Thread thread1 = new Thread(testMethod,"thread1");
Thread thread2 = new Thread(testMethod,"thread2");
thread1.start();
thread2.start();
}
}

上述代码中,线程thread1访问静态同步方法m1(),线程thread2 访问非静态同步方法m2(),结果如下:

thread1:0
thread2:1
thread1:2
thread2:3
thread2:4
thread1:5
线程thread1访问静态同步方法m1()时,线程thread2可以访问非静态同步方法m2()

总结:修饰静态方法时,作用于类,同时作用于该类的所有对象。所以,线程A访问静态同步方法时,其他线程可以访问非静态同步方法和非同步方法,不可以访问静态同步方法。

4、修饰类

synchronized不可以直接修饰类,但是可以通过synchronized(类名.class)作用于类。修饰类和修饰静态方法一样,也是作用于该类的所有对象。

例子1

public class TestSy1 implements Runnable{
private int number; TestSy1(){
number = 0;
} public void add(){
synchronized (TestSy1.class){
for(int i=0;i<4;i++){
try{
System.out.println(Thread.currentThread().getName()+":thread:"+(number++));
Thread.sleep(500);
}catch (Exception e){
System.out.println("异常");
}
}
}
System.out.println("add");
} public void show(){
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + " 非同步:" + number);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} System.out.println("show");
}
@Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
add();
}else{
add();
}
} public static void main(String[] args){
TestSy1 testSy1 = new TestSy1();
Thread thread1 = new Thread(testSy1,"thread1");
Thread thread2 = new Thread(testSy1,"thread2");
thread1.start();
thread2.start();
}
}

结果如下:

thread1:thread:0
thread1:thread:1
thread1:thread:2
thread1:thread:3
thread2:thread:4
thread2:thread:5
thread2:thread:6
thread2:thread:7
线程A访问带有synchronized(类名.class)的代码的方法时,其他线程不能方法该方法。

例子2

线程A访问带有synchronized (TestSy1.class)的add()方法,其他线程访问非静态同步方法。

public class TestSy1 implements Runnable{
private int number; TestSy1(){
number = 0;
} public void add(){
synchronized (TestSy1.class){
for(int i=0;i<4;i++){
try{
System.out.println(Thread.currentThread().getName()+":thread:"+(number++));
Thread.sleep(500);
}catch (Exception e){
System.out.println("异常");
}
}
}
System.out.println("add");
} public synchronized void show(){
for (int i = 0; i < 5; i++) {
try {
System.out.println(Thread.currentThread().getName() + " 非同步:" + number);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} System.out.println("show");
}
@Override
public void run() {
String name = Thread.currentThread().getName();
if(name.equalsIgnoreCase("thread1")){
add();
}else{
show();
}
} public static void main(String[] args){
TestSy1 testSy1 = new TestSy1();
TestSy1 testSy2 = new TestSy1();
Thread thread1 = new Thread(testSy1,"thread1");
Thread thread2 = new Thread(testSy1,"thread2");
thread1.start();
thread2.start();
}
}

结果如下:

thread1:thread:0
thread2 非同步:1
thread1:thread:1
thread2 非同步:2
thread1:thread:2
thread2 非同步:3
thread1:thread:3
thread2 非同步:4
thread2 非同步:4
其他线程可以访问非静态同步方法,非同步方法更不要说了。

总结:

如果一个方法内带有synchronized (类名.class)这样的代码块,这个方法就相当于静态同步方法。

线程A方法该类的静态同步方法时,其他线程不可以访问静态同步方法,但是可以访问非静态同步方法和非同步方法。

synchronized用法详解的更多相关文章

  1. Java并发编程1--synchronized关键字用法详解

    1.synchronized的作用 首先synchronized可以修饰方法或代码块,可以保证同一时刻只有一个线程可以执行这个方法或代码块,从而达到同步的效果,同时可以保证共享变量的内存可见性 2.s ...

  2. Android GLSurfaceView用法详解(二)

    输入如何处理       若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...

  3. Java多线程(三)—— synchronized关键字详解

    一.多线程的同步 1.为什么要引入同步机制 在多线程环境中,可能会有两个甚至更多的线程试图同时访问一个有限的资源.必须对这种潜在资源冲突进行预防. 解决方法:在线程使用一个资源时为其加锁即可. 访问资 ...

  4. 2020了你还不会Java8新特性?(五)收集器比较器用法详解及源码剖析

    收集器用法详解与多级分组和分区 为什么在collectors类中定义一个静态内部类? static class CollectorImpl<T, A, R> implements Coll ...

  5. Java synchronized 关键字详解

    Java synchronized 关键字详解 前置技能点 进程和线程的概念 线程创建方式 线程的状态状态转换 线程安全的概念 synchronized 关键字的几种用法 修饰非静态成员方法 sync ...

  6. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  7. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  8. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  9. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

随机推荐

  1. NDK以及C语言基础语法(二)

    一.字符串类:(属于类类型) -String (在C++中才有) 使用之前必学引入String 类型: 引入String头文件(系统的头文件): #include <string>   p ...

  2. Objective-C中NSString转NSNumber的方法

    本文转载至 http://www.linuxidc.com/Linux/2013-02/78866.htm 在Objective-C中,以数字格式组成的字符串经常需要转换为NSNumber对象后再使用 ...

  3. EasyNVR如何实现跨域鉴权

    EasyNVR提供简单的登录鉴权,客户端通过用户名密码登录成功后,服务端返回认证token的cookie, 后续的接口访问, 服务端从cookie读取token进行校验. 但是, 在与客户系统集成时, ...

  4. [转]JS学习之正则表达式

    js正则表达式实例 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 •替换文本.可以在文档 ...

  5. 常见 WEB 安全漏洞(转)

    SQL注入 成因:程序未对用户的输入的内容进行过滤,从而直接代入数据库查询,所以导致了sql 注入 漏洞 . 思路:在URL处可以通过 单引号 和 and 1=1 and 1=2 等语句进行手工测试s ...

  6. mysql insert返回主键

    使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...

  7. (图解)Description Resource Path Location Type Java compiler level does not match the version of

    Description Resource Path Location Type Java compiler level does not match the version of project 编译 ...

  8. 采集练习(十二) python 采集之 xbmc 酷狗电台插件

    前段时间买了个树莓派才知道有xbmc这么强大的影音软件(后来我逐渐在 电脑.手机和机顶盒上安装xbmc),在树莓派上安装xbmc后树莓派就成为了机顶盒,后面在hdpfans论坛发现了jackyspy  ...

  9. 20145239 《Java程序设计》第6周学习总结

    20145239 <Java程序设计>第6周学习总结 教材学习内容总结 10.1.1串流设计 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 输入串流代表对象 ...

  10. haproxy 修改 访问路径

    # 匹配 jsessionid,并去除 jessionid参数reqrep ^([^\ :]*)\ /a/test.html;jsessionid=.*\?(.*) \1\ /b/test.html? ...