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. ASP.NET动态网站制作(0)

    前言:一直想系统地学习一下网站建设的相关内容,看过相关的书籍,也跟着视频学过,但总觉得效率不高,学过的东西印象不深刻,或许还是自己动手实践的少.无意中免费听了一堂讲ASP.NET网站建设的课,觉得性价 ...

  2. c# combobox 绑定枚举方式

    建立一个类 : using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. [URAL-1517][求两个字符串的最长公共子串]

    Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...

  4. MongoDB的CRUD操作(java Util )

    1.保存插入操作: public static synchronized String insert(DBObject record) { DBCollection col = MongoDB.get ...

  5. Servlet单例模式(注意)

    package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax ...

  6. ios Symbol(s) not found for architecture arm64总结 含隐藏错误cocoapods

    一.通用 报错:Desktop/project/ASDF/WEIXIN/libWeChatSDK.a (3 slices) Undefinedsymbols for architecture arm6 ...

  7. Java AQS详解(转)

    原文地址 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同 ...

  8. Java for LeetCode 085 Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. ZOJ - 3862 Intersection 【贪心】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3862 思路 因为交换次数达到 n + 10 其实我们可以先将他们 ...

  10. centos或ubuntu下手动安装jdk8

    https://blog.csdn.net/pang_ping/article/details/80570011 https://blog.csdn.net/u012707739/article/de ...