参考博客

Java多线程系列--“基础篇”04之 synchronized关键字

synchronized基本规则

第一条 当线程访问A对象的synchronized方法和同步块的时候,其他线程无法访问A对象的synchronized方法和同步块
第二条 当线程访问A对象的synchronized方法和同步块的时候,其他线程可以访问A对象的非synchronized方法和同步块
第三条 当线程访问A对象的synchronized方法和同步块的时候,其他线程不可以访问A对象其他synchronizedd方法和同步块

第三条基本原则测试



import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SysDemo3 {
public static void main(String[] args) {
final Count count =new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodB();
}
},"t2");
t1.start();
t2.start();
} }
@Slf4j
class Count {
int countsize =5; public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
} }
public synchronized void synMethodB(){
int count =0;
while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
} }
}

测试结果

2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 0
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 1
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 2
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 3
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 4
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 0
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 1
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 2
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 3
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 4

说明

每个对象都有一把同步锁,同步锁是依赖对象存在的。上面的结果说明当A对象的同步方法或者同步块被调用的时候,这把锁

就在使用者中,其他的使用者调用A对象的同步方法或者同步块的时候,是不会获取到锁的。

实例锁与全局锁

实例锁是锁在对象上

全局锁是锁在类上,static syncronized方法或者同步块

不同的线程调用一个类的不同的static syncronized方法。

不同的线程调用一个类的不同的static syncronized方法:x.classSyn1()与y.classSyn2(),x不释放,y是无法运行静态同步方法的。

测试


import lombok.extern.slf4j.Slf4j; import static java.lang.Thread.sleep;
/*
全局锁的使用,类的static syncronized方法或代码块被线程调用,
其他线程没有拥有全局锁,无法调用其他的同步方法和同步块
*/
@Slf4j
public class SysDemo5 {
static class Count {
static int countsize =5; public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public synchronized void synMethodB(){
int count =0; while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public static synchronized void cSynMethodA(){
int count =0;
while(count<countsize){
log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void cSynMethodB(){
int count =0;
while(count<countsize){
log.info("class synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodB();
}
},"t2");
t1.start();
t2.start();
}
}

测试结果

2019-07-25 17:31:24,358   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 0
2019-07-25 17:31:24,857 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 1
2019-07-25 17:31:25,356 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 2
2019-07-25 17:31:25,855 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 3
2019-07-25 17:31:26,354 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 4
2019-07-25 17:31:26,853 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 0
2019-07-25 17:31:27,351 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 1
2019-07-25 17:31:27,850 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 2
2019-07-25 17:31:28,349 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 3
2019-07-25 17:31:28,847 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 4

x.classSyn1与x.sysn1

对象的同步方法和类的同步方法互不影响

测试

import lombok.extern.slf4j.Slf4j;

import static java.lang.Thread.sleep;

@Slf4j
public class SysDemo4 {
static class Count {
static int countsize =5; public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public synchronized void synMethodB(){
int count =0; while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public static synchronized void cSynMethodA(){
int count =0;
while(count<countsize){
log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final Count count =new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodA();
}
},"t2");
t1.start();
t2.start();
} }

测试结果

2019-07-25 19:04:53,184   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 0
2019-07-25 19:04:53,199 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 0
2019-07-25 19:04:53,683 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 1
2019-07-25 19:04:53,699 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 1
2019-07-25 19:04:54,182 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 2
2019-07-25 19:04:54,198 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 2
2019-07-25 19:04:54,682 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 3
2019-07-25 19:04:54,697 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 3
2019-07-25 19:04:55,181 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 4
2019-07-25 19:04:55,197 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 4 Process finished with exit code 0

java并发(二):初探syncronized的更多相关文章

  1. Java并发(二):基础概念

    并发编程的第二部分,先来谈谈发布(Publish)与逸出(Escape); 发布是指:对象能够在当前作用域之外的代码中使用,例如:将对象的引用传递到其他类的方法中,对象的引用保存在其他类可以访问的地方 ...

  2. Java并发编程初探

    package test; import java.io.File; import java.io.FileReader; import java.io.IOException; import jav ...

  3. java并发:初探消费者和生产者模式

    消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...

  4. java并发:初探用户线程和守护线程

    用户线程和守护线程 用户线程 用户线程执行完,jvm退出.守护线程还是可以跑的 /** * A <i>thread</i> is a thread of execution i ...

  5. java并发初探ConcurrentHashMap

    java并发初探ConcurrentHashMap Doug Lea在java并发上创造了不可磨灭的功劳,ConcurrentHashMap体现这位大师的非凡能力. 1.8中ConcurrentHas ...

  6. 【Java并发编程实战】----- AQS(二):获取锁、释放锁

    上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...

  7. Java并发基础框架AbstractQueuedSynchronizer初探(ReentrantLock的实现分析)

    AbstractQueuedSynchronizer是实现Java并发类库的一个基础框架,Java中的各种锁(RenentrantLock, ReentrantReadWriteLock)以及同步工具 ...

  8. Java并发编程二三事

    Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...

  9. java 并发多线程 锁的分类概念介绍 多线程下篇(二)

    接下来对锁的概念再次进行深入的介绍 之前反复的提到锁,通常的理解就是,锁---互斥---同步---阻塞 其实这是常用的独占锁(排它锁)的概念,也是一种简单粗暴的解决方案 抗战电影中,经常出现为了阻止日 ...

随机推荐

  1. sql语句查询指定月份数据

    要求:查询出emp表中1981年2月份入职的员工 emp表 常用的两种方式: 1.YEAR查询年,MONTH查询月 SELECT * FROM emp WHERE ' 2.date_format (使 ...

  2. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  3. 5种JVM调优配置方法概览!!!

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  4. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  5. traceback说明

    一:traceback说明 该模块提供了一个标准接口来提取,格式化和打印Python程序的堆栈跟踪.它完全模仿Python解释器在打印堆栈跟踪时的行为.当您想要在程序控制下打印堆栈跟踪时,这很有用. ...

  6. Nexus-vPC相关特性

    vPC Peer-switch: 不开启这功能,只有Primary设备发送BPDU,开启之后,将会把这一对设备呈现为一个STP Root,使用一个MAC地址,那么都可以发送BPDU了.STP BPDU ...

  7. k sum 问题系列

    转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...

  8. 安装PHP解析环境!

    较新版本(如5.6)的PHP已经自带FPM(fastCGI process manager,FastCGI进程管理器)模块,用来对PHP解析实例进行管理,优化解析效率,因此在配置PHP编译选项时应添加 ...

  9. Cake ZOJ - 3537

    题目链接 本题也是区间dp三角剖分板子题,只不过加入了判断是否是凸包,计算顺序要用凸包顺序(凸包板) #include<bits/stdc++.h> using namespace std ...

  10. 同一台服务器lnmpa环境下配置ip或域名访问不同站点

    1.配置域名访问 (1)添加虚拟主机 (2)nginx配置 cd /usr/local/nginx/conf/vhost vim zkadmin.zouke.com.conf (3)apache配置 ...