synchronized

How to return a thread safe/immutable Collection in Java?

倖福魔咒の 提交于 2019-12-07 01:45:32
问题 In the project I am coding, I need to return a thread safe and immutable view from a function. However, I am unsure of this. Since synchronizedList and unmodifiableList just return views of a list, I don't know if Collections.synchronizedList(Collections.unmodifiableList(this.data)); would do the trick. Could anyone tell me if this is correct, and in case it is not, are there any situations that this would likely to fail? Thanks for any inputs! 回答1: I find this to be a real gap in the JDK.

线程运行诊断

风流意气都作罢 提交于 2019-12-06 22:36:25
线程运行诊断** 定位 用top定位哪个进程对cpu的占用过高 ps H -eo pid,tid,%cpu | grep 进程id (用ps命令进一步定位是哪个线程引起的cpu占用过高) jstack 进程id 可以根据线程id 找到有问题的线程,进一步定位到问题代码的源码行号 要先将32665转为16进制,即0x7f99,定位到了具体的代码 /** * 演示 cpu 占用过高 */ public class Demo1_16 { public static void main(String[] args) { new Thread(null, () -> { System.out.println("1..."); while(true) { } }, "thread1").start(); new Thread(null, () -> { System.out.println("2..."); try { Thread.sleep(1000000L); } catch (InterruptedException e) { e.printStackTrace(); } }, "thread2").start(); new Thread(null, () -> { System.out.println("3..."); try { Thread.sleep(1000000L); }

synchronized被这么问,谁能受得了

喜夏-厌秋 提交于 2019-12-06 21:15:23
synchronized是面试中经常会被问到的知识点,相关的问题点也很多,问题答案涉及的知识点也很多,有经验的面试官就会顺着你的答案不断追问一下,下面的对话场景就是相关面试题的连环炮。 面试官:说一下synchronized的作用。 小白:对于单一JVM来说,synchronized可以保证在并发情况下,同一时刻只有一个线程执行某个方法或某段代码,它可用于修饰方法或代码块,实现对同步代码的并发安全控制。 面试官:你刚刚说synchronized可用于修饰方法和代码块,他们有什么区别呢? 小白:修饰方法在底层实现上会在方法访问标识中设置ACC_SYNCHRONIZED标示符,修饰代码块在底层实现上会使用monitorenter和monitorexit指令。 面试官:那你说一下修饰方法方式的底层实现原理? 小白:反编译字节码文件,可以看到在方法的flags中设置了ACC_SYNCHRONIZED访问标识。每个对象都与一个monitor相关联,当且仅当monitor被线程持有时,monitor处于锁定状态。当方法执行时,线程将先尝试获取对象相关联的monitor所有权,然后再执行方法,最后在方法完成(无论是正常执行还是非正常执行)时释放monitor所有权。在方法执行期间,线程持有了monitor所有权,其它任何线程都无法再获得同一个对象相关联的monitor所有权。

java中的synchronized与volatile

青春壹個敷衍的年華 提交于 2019-12-06 20:02:44
在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉。 Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和 volatile 关键字机制。 synchronized 同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized 和 块语句,在多线程访问的时候,同一时刻只能有一个线程能够用synchronized 修饰的方法 或者 代码块。 volatile 用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最新值。volatile很容易被误用,用来进行原子性操作。 下面看一个例子,我们实现一个计数器,每次线程启动的时候,会调用计数器inc方法,对计数器进行加一 <!-- lang: java --> public class Counter{ public static int count = 0; public static void inc() { //这里延迟1毫秒,使得结果明显 try { Thread.sleep(1); } catch (InterruptedException e) { } count++; } public static void main(String[] args)

Synchronized release order

巧了我就是萌 提交于 2019-12-06 19:44:33
问题 I scanned all the java documentation on the synchronized statements looking for an answer to this question with no luck. Say I have thread1 , thread2 , thread3 trying to run the following code all at the same time. synchronized(lockObj) { doSomething(); } Assume thread1 gets first to doSomething() , thread2 then thread3 which will block and wait on the synchronized statement. Question When thread1 releases the lock, which of the threads will be released first? What is the general order rule

Why `this.synchronized` instead of just `synchronized` in Scala?

偶尔善良 提交于 2019-12-06 16:44:52
问题 In an example of working with JDBC in Scala, there is a following code: this.synchronized { if (!driverLoaded) loadDriver() } Why this.synchronized instead of just synchronized ? 回答1: In scala synchronized is not a keyword, as in java. It is in fact a member of AnyRef, which is scala equivalent for java's Object . So to answer your question, you can either use synchronized or this.synchronized , just as you can do toString or this.toString . 来源: https://stackoverflow.com/questions/7826822/why

Garbage collection and synchronized visibility

隐身守侯 提交于 2019-12-06 16:08:12
I have read about marking an object as volatile doesn't guarantee visibility of it's members ( I'm not saying about thread safety just memory visibility , quoting : only the object reference will be considered to be volatile by the JVM and not the object data itself which will reside on the heap my questions : The synchronize will ensure the visibility of the members (on the same lock object) in case they have been edited. Is that because the happens-before at the end (release) of the lock which makes the actions visible to the other thread? In case of using volatile on the object, and the

Two threads executing two `synchronized` methods?

时光毁灭记忆、已成空白 提交于 2019-12-06 15:25:09
问题 I was reading about JAVA synchronization. I have 2 methods in my class. public synchronized void eat() { System.out.println("eat"); eatDinner(); } public synchronized void eatDinner() { System.out.println("eat"); } Both of my methods are synchronized. Now Is it possible for 2 threads one is calling eat() and another eatDinner() to run simultaneously? And If thread2 has not still executing eatDinner() . Can thread1 can call eatDinner() from eat() ? 回答1: No , it is not possible for two threads

Java并发编程系列-(1) 并发编程基础

萝らか妹 提交于 2019-12-06 15:21:47
1.并发编程基础 1.1 基本概念 CPU核心与线程数关系 Java中通过多线程的手段来实现并发,对于单处理器机器上来讲,宏观上的多线程并行执行是通过CPU的调度来实现的,微观上CPU在某个时刻只会运行一个线程。事实上,如果这些任务不存在阻塞,也就是程序中的某个任务因为该程序控制范围之外的某些条件(通常是I/O)而导致不能继续执行,由于在任务之间切换会产生开销,因此并行的效率可能没有顺序执行的效率高,并行也就没有意义。 一般来讲,CPU核心数和线程数的关系为核心数:线程数=1:1;但是如果使用了超线程技术,可以达到1:2甚至更多。 CPU调度方式 CPU采用时间片轮转机制,来调度不同的线程运行,又称RR调度,注意这样会导致上下文切换。如果线程数目过大,可能产生较大的线程切换开销。 线程和进程 进程 :进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位。(包括程序段,相关数据段,和进程控制块PCB) 线程 :线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源. 关系:一个线程可以创建和撤销另一个线程;同一个进程中的多个线程之间可以并发执行

volatile

北战南征 提交于 2019-12-06 14:34:53
Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized ”;与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。本文介绍了几种有效使用 volatile 变量的模式,并强调了几种不适合使用 volatile 变量的情形。 锁提供了两种主要特性: 互斥(mutual exclusion) 和 可见性(visibility) 。互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议,这样,一次就只有一个线程能够使用该共享数据。可见性要更加复杂一些,它必须确保释放锁之前对共享数据做出的更改对于随后获得该锁的另一个线程是可见的 —— 如果没有同步机制提供的这种可见性保证,线程看到的共享变量可能是修改前的值或不一致的值,这将引发许多严重问题。 Volatile 变量 Volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性。这就是说线程能够自动发现 volatile 变量的最新值。Volatile 变量可用于提供线程安全,但是只能应用于非常有限的一组用例:多个变量之间或者某个变量的当前值与修改后值之间没有约束。因此,单独使用 volatile 还不足以实现计数器