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. c# 根据读取的配置信息删除某个目录及下所有文件

    #region 定时删除目录文件 /// <summary> /// 定时删除目录文件 /// northeasttycoon /// </summary> /// <p ...

  2. 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组

    [BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...

  3. 【题解】NOI2015软件包管理器

    [题解][P2146 NOI2015]软件包管理器 实际上就是树链剖分板子题. 对于\(install\)操作,直接查询它到\(0\)节点有多少已经安装了的,再用总数减去它. 对于\(uninstal ...

  4. 流畅python学习笔记:第二十章:属性描述符:

    在前面一章中介绍了@property的用法,但是存在一个问题,如果我有多个属性想转变成property特性,那不是针对每个都需要实现一个 @propery.setter 和 @property.get ...

  5. ABAP-创建物料主数据

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' *&------------------------------------------------------- ...

  6. 常见C C++问题(转)

    这一部分是C/C++程序员在面试的时候会被问到的一些题目的汇总.来源于基本笔试面试书籍,可能有一部分题比较老,但是这也算是基础中的基础,就归纳归纳放上来了.大牛们看到一笑而过就好,普通人看看要是能补上 ...

  7. 蓝牙 CTS 测试

    安装蓝牙测试安装包 之后  . 安卓包名字 android-cts-6.0_r19-linux_x86-x86.zip 解压之后 /cts/android-cts/tools/  目录下 运行  ./ ...

  8. mysql忘记root密码或报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘xx‘

    有的时候忘记了root密码或其他用户的密码,登录的时候报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ' ...

  9. Java for LeetCode 111 Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  10. 剑指Offer:删除链表的节点【18】

    剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...