synchronized

线程和偏向锁、轻量级锁、重量级锁的知识整理

天大地大妈咪最大 提交于 2019-12-04 13:02:13
线程和偏向锁、轻量级锁、重量级锁的知识整理 https://www.cnblogs.com/xlecho/p/11342971.html xl_echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075)交流学习。 百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!! 参考文章列表: Java并发编程:Synchronized底层优化(偏向锁、轻量级锁) 轻量级锁、偏向锁、重量级锁详情 偏向锁、轻量级锁、重量级锁、自旋锁原理讲解(推荐看一下) 参考视频:咕泡学院Mic老师的多线程基本原理 主要的内容如下 多线程同时执行的安全问题思考 Synchronized的基本认识 思考锁的存储 Synchronized锁的升级原理 wait/notify实现线程通信 多线程同时执行的安全问题思考 如果业务代码逻辑当中,有一个操作需要改变一个常量的值,比如int i = 0, 业务代码当中i需要i++。单线程的情况下,不会出现问题,如果是多线程并发操作i的值,这个时候,i的结果最终是什么?会出现线程的安全问题。这种情况应该怎么解决? 线程的安全性有三种 原子性 提供了互斥访问,同一时刻只能有一个线程对它进行操作 实现锁的两种方式: 1)synchronized:在作用对象的作用范围内,依赖JVM实现操作的原子性。 2)Lock

Java线程笔记-生产者和消费者

蹲街弑〆低调 提交于 2019-12-04 11:54:55
Java线程笔记 1. 线程的介绍:   Java中每一个对象都可以作为锁,这是synchronized实现同步的基础; 普通同步方法(实例方法),锁是当前实例对象 ,进入同步代码前要获得当前实例的锁; 静态同步方法,锁是当前类的class对象 ,进入同步代码前要获得当前类对象的锁; 同步方法块,锁是括号里面的对象,对给定对象加锁,进入同步代码库前要获得给定对象的锁; 2. synchronized 关键字;   关键字synchronized可以保证在同一时刻,只有一个线程可以执行某个方法或某个代码块;用于解决多线程共同操作共享数据的问题。 3. 方法介绍; wait() :当前线程进入等待状态,释放锁,停止执行wait() 方法后面的语句; notify() : notifyAll() : 通知所有等待相同资源的线程,不会立即释放锁,当前线程执行完后释放锁,即,notifyAll()通知发出后,需当前线程执行完后释放锁,其他等待的线程才能抢到资源; 4. 生产者-消费者实现方式一 (synchronized、wait和notify) 4.1 先定义一个资源池,用于存放线程共享资源,并提供生产、消费资源的方法 1 //定义一个资源池的类Resource 2 3 class Resource { 4 private int num = 0; 5 private int size =

Java多线程

坚强是说给别人听的谎言 提交于 2019-12-04 11:53:55
1. 多线程 image.png 新建状态: 一个新产生的线程从新状态开始了它的生命周期。它保持这个状态直到程序 start 这个线程。 运行状态:当一个新状态的线程被 start 以后,线程就变成可运行状态,一个线程在此状态下被认为是开始执行其任务 就绪状态:当一个线程等待另外一个线程执行一个任务的时候,该线程就进入就绪状态。当另一个线程给就绪状态的线程发送信号时,该线程才重新切换到运行状态。 休眠状态: 由于一个线程的时间片用完了,该线程从运行状态进入休眠状态。当时间间隔到期或者等待的时间发生了,该状态的线程切换到运行状态。 终止状态: 一个运行状态的线程完成任务或者其他终止条件发生,该线程就切换到终止状态。 2. 僵死进程 计算机的计算模型大部分是基于空间和时间来考虑的。僵死进程唯一占用的空间是pid空间,这个空间如果不能合理的应用就会造成浪费,之所以保留这个空间,是为了让父进程感知子进程已经终止这个行为。时间方面,这个感知过程是一个异步的过程。 3. 创建线程的方式 继承 Thread 类 实现 Runnable 接口 使用 Executor 框架 法一:继承Thread类 1.1定义一个类继承Thread 1.2重写run方法 1.3创建对象 1.4调用start方法开启线程 线程对象调用run()方法和start()方法区别? 调用run方法不开启线程,仅是对象调用方法

Synchronized reordering in java

做~自己de王妃 提交于 2019-12-04 11:00:21
It is known that JVM shouldn't reorder statements from withing synchronized block to outside of the synchronized block. Considering this, is JVM allowed to reorder assignment y = 7 to occur after the synchronized block in the following snippet? x = 5; y = 7; synchronized (this) { x = 6; } We know that variable assignment before the synchronized block can be reordered to occur inside the block. So the following should be valid reordering of the initial code: x = 5; synchronized (this) { x = 6; y = 7; } One could argue that, because this is a valid ordering, y assignment cannot occur after the

Acquiring multiple locks atomically in java

﹥>﹥吖頭↗ 提交于 2019-12-04 10:57:11
I have the following code: Note: I simplified the code as much as possible for readability. If I forgot any critical pieces let me know. public class User(){ private Relations relations; public User(){ relations = new Relations(this); } public getRelations(){ return relations; } } public class Relations(){ private User user; public Relations(User user){ this.user = user; } public synchronized void setRelation(User user2){ Relations relations2 = user2.getRelations(); synchronized(relations2){ storeRelation(user2); if(!relations2.hasRelation(user)) relations2.setRelation(user); } } public

Synchronize on value, not object [duplicate]

冷暖自知 提交于 2019-12-04 10:26:28
问题 This question already has answers here : Synchronizing on String objects in Java (19 answers) Closed 2 years ago . I want to do something like this in Java public void giveMoney(String userId, int money) { synchronized (userId) { Profile p = fetchProfileFromDB(userId); p.setMoney(p.getMoney() + userId); saveProfileToDB(p); } } But of course, synchronizing on a string is not correct. What's a correct way to do something like this? 回答1: If the set of user ids is limited, you can synchronize on

Synchronized Vs Semaphore

帅比萌擦擦* 提交于 2019-12-04 09:59:45
问题 While reading concurrency in Java, I have following doubts: Does Java provides lower level construct then synchronized for synchronization? In what circumstances will we use semaphore over synchronized (which provides monitor behaviour in Java) 回答1: Synchronized allows only one thread of execution to access the resource at the same time. Semaphore allows up to n (you get to choose n) threads of execution to access the resource at the same time. 回答2: There is also volatile keyword, according

Mutually exclusive method execution in Java

被刻印的时光 ゝ 提交于 2019-12-04 09:00:02
I have two methods: a() and b() . While I'm fine with multiple threads accessing any of the methods at the same time (that is desirable), I do not want any threads to enter a() while b() is being executed. How do I do that? Edit 1 Lets say there are 4 threads and Thread 1 is accessing A() . What I want is all of the 4 threads should not use B() . assylias CHECK UPDATE at the bottom - I don't think this approach can work. Leaving it for information. You could use a Semaphore : if a thread is in b() , a thread trying to execute a() will block until the execution of b() is over. if a thread is in

Android 线程处理

社会主义新天地 提交于 2019-12-04 08:54:46
synchronized处理线程wait() 和notifyAll() 时,同步代码块中不要包含Thread.sleep(5)语句: 1 package com.csizg.test; 2 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8 * 与芯片交互线程 9 */ 10 public class SpiQueue implements Runnable { 11 12 private static final String TAG = SpiQueue.class.getSimpleName(); 13 14 private static final byte INIT = 0;// 初始化 15 private static final byte RUNNING = 1;// 执行中 16 private static final byte WITING = 2;// 暂停等待中 17 private static final byte DONE = 3;// 结束 18 19 private byte state = INIT;// 执行状态 20 21 private static SpiQueue spiQueue = null; 22 private boolean isTalking