synchronized

Java's happens-before and synchronization

China☆狼群 提交于 2020-01-01 00:22:39
问题 I'm having a little disagreement on Java's happens-before and synchronization. Imagine the following scenario: Main Thread MyObject o = new MyObject(); // (0) synchronized (sharedMonitor) { // (1) add the object to a shared collection } // (2) spawn other threads Other Threads MyObject o; synchronized (sharedMonitor) { // (3) retrieve the previously added object } // (4) actions to modify the object Note that the instance variables of MyObject aren't neither volatile , nor final . The methods

Java中多线程原理详解

时光怂恿深爱的人放手 提交于 2019-12-31 21:58:05
Java是少数的集中支持多线程的语言之一,大多数的语言智能运行单独的一个程序块,无法同时运行不同的多个程序块,Java的多线程机制弥补了这个缺憾,它可以让不同的程序块一起运行,这样可以让程序运行更加顺畅,同时也达到了多任务处理的目的。    一、线程和进程的概念   现在的操作系统是多任务操作系统。多线程是实现多任务的一种方式。   进程是程序的一个动态执行过程,是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程。比如在 Windows系统中,一个运行的exe就是一个进程。线程是指进程中的一个执行流程,一个进程中可以运行多个线程。比如java.exe进程中可以运行很多线程。线程总是属于某个进程,进程中的多个线程共享进程的内存。“同时”执行是人的感觉,在线程之间实际上轮换执行。    二、Java中线程的实现   在Java中想实现多线程有两种手段,一种是集成Thread类,另一种就是实现Runnable接口。下面看继承自Thread类线程的创建原理。   首先定义一个线程类,该类必须继承自Thread类,同时必须明确的覆写run()方法,如: class MyThread extends Thread{   public void run(){ //覆写Thread类中的run方法此方法是线程中   线程主体;   } } 然后定义一个主类

深入研究 Java Synchronize 和 Lock 的区别与用法

老子叫甜甜 提交于 2019-12-31 20:12:00
在分布式开发中,锁是线程控制的重要途径。Java为此也提供了2种锁机制,synchronized和lock。做为Java爱好者,自然少不了对比一下这2种机制,也能从中学到些分布式开发需要注意的地方。 我们先从最简单的入手,逐步分析这2种的区别。 一、synchronized和lock的用法区别 synchronized:在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要锁的对象。 lock:需要显示指定起始位置和终止位置。一般使用ReentrantLock类做为锁,多个线程中必须要使用一个ReentrantLock类做为对象才能保证锁的生效。且在加锁和解锁处需要通过lock()和unlock()显示指出。所以一般会在finally块中写unlock()以防死锁。 用法区别比较简单,这里不赘述了,如果不懂的可以看看Java基本语法。 二、synchronized和lock性能区别 synchronized是托管给JVM执行的,而lock是java写的控制锁的代码。在Java1.5中,synchronize是性能低效的。因为这是一个重量级操作,需要调用操作接口,导致有可能加锁消耗的系统时间比加锁以外的操作还多。相比之下使用Java提供的Lock对象,性能更高一些。但是到了Java1.6,发生了变化

When a lock holds a non-final object, can the object's reference still be changed by another thread?

倾然丶 夕夏残阳落幕 提交于 2019-12-31 02:33:53
问题 When an object needs to be synchronized, the IDE complains if it's not set non-final (because its reference isn't persistent): private static Object myTable; .... synchronized(myTable){ //IDE complains! //access myTable here... } We all know the IDE complains to prevent another thread from entering the guarded block if the thread holding the lock changes the non-final object's references. But could a synchronized object's reference also be changed by another thread B while thread A holds the

妖精的绣舞 提交于 2019-12-30 10:33:35
2. 轻量级锁   倘若偏向锁失败, 虚拟机并不会立即升级为重量级锁 ,它还会尝试使用一种称为轻量级锁的优化手段(1.6之后加入的)。轻量级锁不是为了代替重量级锁,它的本意是 在没有多线程竞争的前提下,减少传统的重量级锁使用操作系统互斥量产生的性能消耗,因为使用轻量级锁时,不需要申请互斥量   轻量级锁能够提升程序同步性能的依据是“对于绝大部分锁,在整个同步周期内都是不存在竞争的”,这是一个经验数据。如果没有竞争,轻量级锁使用 CAS 操作避免了使用互斥操作的开销。 但如果存在锁竞争,除了互斥量开销外,还会额外发生CAS操作,因此在有锁竞争的情况下,轻量级锁比传统的重量级锁更慢! 如果锁竞争激烈,那么轻量级将很快膨胀为重量级锁! 当有另外一个线程竞争锁时,由于该锁处于 偏向锁 状态 发现对象头Mark Word中的线程ID不是自己的线程ID,该线程就会执行CAS操作获取锁 如果获取 成功 ,直接替换Mark Word中的线程ID为自己的线程ID,该锁会***保持偏向锁状态*** 如果获取 失败 ,说明当前锁有一定的竞争,将偏向锁 升级 为轻量级锁 线程获取轻量级锁的步骤: 在加锁前,虚拟机需要在当前线程的栈帧中建立 锁记录 (Lock Record)的空间。Lock Record 中包含一个 _displaced_header 属性,用于存储锁对象的 Mark Word 的拷贝。

Statement execution interleaving with Synchronized method execution

。_饼干妹妹 提交于 2019-12-30 10:17:09
问题 I have some trouble in understanding the synchronized keyword functionality. As per java documentations and other tutorials, it is being said that when synchronized keyword is used, interleaving between the statements of that method is NOT possible between the two threads. But, please see the below piece of code. public class LockThread implements Runnable { String name; public LockThread(String name) { this.name = name; } public static void main(String[] args) { new Thread(new LockThread("a"

Statement execution interleaving with Synchronized method execution

青春壹個敷衍的年華 提交于 2019-12-30 10:16:23
问题 I have some trouble in understanding the synchronized keyword functionality. As per java documentations and other tutorials, it is being said that when synchronized keyword is used, interleaving between the statements of that method is NOT possible between the two threads. But, please see the below piece of code. public class LockThread implements Runnable { String name; public LockThread(String name) { this.name = name; } public static void main(String[] args) { new Thread(new LockThread("a"

《吊打面试官》系列-ConcurrentHashMap & HashTable

孤人 提交于 2019-12-30 10:03:48
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 你知道的越多,你不知道的越多 点赞再看,养成习惯 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试点思维导图,也整理了很多我的文档,欢迎Star和完善,大家面试可以参照考点复习,希望我们一起有点东西。 前言 作为一个在互联网公司面一次拿一次Offer的面霸,打败了无数竞争对手,每次都只能看到无数落寞的身影失望的离开,略感愧疚( 请允许我使用一下夸张的修辞手法 )。 于是在一个寂寞难耐的夜晚,我痛定思痛,决定开始写互联网技术栈面试相关的文章,希望能帮助各位读者以后面试势如破竹,对面试官进行360°的反击,吊打问你的面试官,让一同面试的同僚瞠目结舌,疯狂收割大厂Offer! 所有文章的名字只是我的噱头,我们应该有一颗谦逊的心,所以希望大家怀着空杯心态好好学,一起进步。 回手掏 上次面试呀,我发现面试官对我的几个回答还是不够满意,觉得还是有点疑问,我就挑几个回答一下。 16是2的幂,8也是,32也是,为啥偏偏选了16? 我觉得就是一个经验值,定义16没有很特别的原因,只要是2次幂,其实用 8 和 32 都差不多。 用16只是因为作者认为16这个初始容量是能符合常用而已。 Hashmap中的链表大小超过八个时会自动转化为红黑树,当删除小于六时重新变为链表,为啥呢?

《提升能力,涨薪可待》—Java并发之Synchronized

心不动则不痛 提交于 2019-12-30 09:37:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Synchronized简介 线程安全是并发编程中的至关重要的,造成线程安全问题的主要原因: 临界资源, 存在共享数据 多线程共同操作共享数据 而Java关键字synchronized,为多线程场景下防止临界资源访问冲突提供支持, 可以保证在同一时刻,只有一个线程可以执行某个方法或某个代码块操作共享数据。 即当要执行代码使用synchronized关键字时,它将检查锁是否可用,然后获取锁,执行代码,最后再释放锁。而synchronized有三种使用方式: synchronized方法: synchronized当前实例对象,进入同步代码前要获得当前实例的锁 synchronized静态方法: synchronized当前类的class对象 ,进入同步代码前要获得当前类对象的锁 synchronized代码块:synchronized括号里面的对象,对给定对象加锁,进入同步代码库前要获得给定对象的锁 Synchronized方法 首先看一下没有使用synchronized关键字,如下: public class ThreadNoSynchronizedTest { public void method1(){ try { Thread.sleep(2000); } catch

Java synchronized method around parameter value

无人久伴 提交于 2019-12-30 06:44:20
问题 Consider the following method: public void upsert(int customerId, int somethingElse) { // some code which is prone to race conditions } I want to protect this method from race conditions, but this can only occur if two threads with the same customerId are calling it at the same time. If I make the whole method synchronized it will reduce the efficiency and it's not really needed. What I really want is to synchronize it around the customerId . Is this possible somehow with Java? Are there any