synchronized

Synchronization, When to or not to use?

回眸只為那壹抹淺笑 提交于 2019-12-19 05:12:30
问题 I have started learning concurrency and threads in Java. I know the basics of synchronized (i.e. what it does). Conceptually I understand that it provides mutually exclusive access to a shared resource with multiple threads in Java. But when faced with an example like the one below I am confused about whether it is a good idea to have it synchronized. I know that critical sections of the code should be synchronized and this keyword should not be overused or it effects the performance. public

Java synchronized references

主宰稳场 提交于 2019-12-19 04:58:28
问题 I have a class A and B. public class A() { private static List<int> list = new ArrayList<int>(); public static List<int> getList() { return list; } } public class B() { public void foo() { synchronized(A.getList()) { // DO Stuff } } } In class B where I synchronize. Does this synchronize on A's list, or on B's reference to A's list. I think it is the latter but could use some help. If so then how do I accomplish a similar thing to this that will actually work? Thanks! 回答1: The OP followed up

Java中的线程

泄露秘密 提交于 2019-12-19 03:09:54
我们可以在计算机上运行各种计算机软件程序。每一个运行的程序可能包括多个独立运行的线程(Thread)。 线程(Thread)是一份独立运行的程序,有自己专用的运行栈。线程有可能和其他线程共享一些资源,比如,内存,文件,数据库等。 当多个线程同时读写同一份共享资源的时候,可能会引起冲突。这时候,我们需要引入线程“同步”机制,即各位线程之间要有个先来后到,不能一窝蜂挤上去抢作一团。 同步这个词是从英文synchronize(使同时发生)翻译过来的。我也不明白为什么要用这个很容易引起误解的词。既然大家都这么用,咱们也就只好这么将就。 线程同步的真实意思和字面意思恰好相反。线程同步的真实意思,其实是“排队”:几个线程之间要排队,一个一个对共享资源进行操作,而不是同时进行操作。 因此,关于线程同步,需要牢牢记住的第一点是:线程同步就是线程排队。同步就是排队。线程同步的目的就是避免线程“同步”执行。这可真是个无聊的绕口令。 关于线程同步,需要牢牢记住的第二点是 “共享”这两个字。只有共享资源的读写访问才需要同步。如果不是共享资源,那么就根本没有同步的必要。 关于线程同步,需要牢牢记住的第三点是,只有“变量”才需要同步访问。如果共享的资源是固定不变的,那么就相当于“常量”,线程同时读取常量也不需要同步。至少一个线程修改共享资源,这样的情况下,线程之间就需要同步。 关于线程同步

2020年Java面试100题

别来无恙 提交于 2019-12-19 01:54:10
一、Java 基础 1. JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,java 运行环境,为 java 的运行提供了所需环境。 具体来说 JDK 其实包含了 JRE,同时还包含了编译 java 源码的编译器 javac,还包含了很多 java 程序调试和分析的工具。简单来说:如果你需要运行 java 程序,只需安装 JRE 就可以了,如果你需要编写 java 程序,需要安装 JDK。 2. == 和 equals 的区别是什么? == 解读 对于基本类型和引用类型 == 的作用效果是不同的,如下所示: 基本类型:比较的是值是否相同; 引用类型:比较的是引用是否相同; 代码示例: String x = "string"; String y = "string"; String z = new String("string"); System.out.println(x==y); // true System.out.println(x==z); // false System.out.println(x.equals(y)); // true System.out.println(x.equals(z));

Java: Are all monitors released when thread waits on an object?

三世轮回 提交于 2019-12-18 14:11:55
问题 Before a thread can wait on an object, it has to acquire a monitor on that object. The monitor is then released, and the thread attempts to re-acquired it once it awakes. But what happens to other monitors the thread holds when it calls wait ? Consider this example: Object a = // ... Object b = // ... synchronized(a) { synchronized(b) { b.wait(); // continue } } When the thread calls b.wait() , will it release the locks on both a and b , or only b ? 回答1: Only b . The authoritarian source for

proper usage of synchronized singleton?

馋奶兔 提交于 2019-12-18 13:11:59
问题 So I am thinking about building a hobby project, one off kind of thing, just to brush up on my programming/design. It's basically a multi threaded web spider, updating the same data structure object->int. So it is definitely overkill to use a database for this, and the only thing I could think of is a thread-safe singleton used to contain my data structure. http://web.archive.org/web/20121106190537/http://www.ibm.com/developerworks/java/library/j-dcl/index.html Is there a different approach I

Can you safely synchronize on a Java method parameter?

亡梦爱人 提交于 2019-12-18 12:15:03
问题 Take this code: public class MyClass { private final Object _lock = new Object(); private final MyMutableClass _mutableObject = new MyMutableClass() public void myMethod() { synchronized(_lock) { // we are synchronizing on instance variable _lock // do something with mutableVar //(i.e. call a "set" method on _mutableObject) } } } now, imagine delegating the code inside myMethod() to some helper class where you pass the lock public class HelperClass { public helperMethod(Object lockVar,

JDK的命令行工具系列 (三) jhat、jstack

一笑奈何 提交于 2019-12-18 11:43:41
jhat: heapdump文件分析工具 在前两篇系列文章 JDK的命令行工具系列 (一) jps、jstat 、 JDK的命令行工具系列 (二) javap、jinfo、jmap 中, 我们已经介绍过了 jps 、 jmap 这些命令行工具的使用, 所以这里就不在多做说明, 直接演示 jhat 的使用。 代码清单: public class JhatDemo { Integer i = 6; public static void main(String[] args) { String str = "qingshanli"; while(true) { System.out.println(str); } } } 命令行下: //查看java进程的pidC:\Users\liqingshan>jps -l 12816 14352 org.apache.catalina.startup.Bootstrap 15924 sun.tools.jps.Jps 14184 JhatDemo 14092 org.jetbrains.jps.cmdline.Launcher //生成heapdump文件, 文件名为JhatDemo_heapdump C:\Users\liqingshan>jmap -dump:format=b,file=JhatDemo_heapdump 14184

Java多线程:多线程的Synchronized详解

雨燕双飞 提交于 2019-12-18 02:30:47
输出为什么要引入同步机制 在多线程环境中,可能会有两个甚至更多的线程试图同时访问一个有限的资源。必须对这种潜在资源冲突进行预防。 解决方法:在线程使用一个资源时为其加锁即可。访问资源的第一个线程为其加上锁以后,其他线程便不能再使用那个资源,除非被解锁。 怎样实现同步 对于访问某个关键共享资源的所有方法,都必须把它们设为synchronized 例如: synchronized void f() { /* ... */ } synchronized void g() { /* ... */ } 如果想保护某些资源不被多个线程同时访问,可以强制通过synchronized方法访问那些资源。 调用synchronized方法时,对象就会被锁定。 1 public class MyStack { 2 int idx = 0; 3 char [] data = new char[ 6]; 4 public synchronized void push( char c) { 5    data[ idx] = c; 6    idx++; 7 } 8 public synchronized char pop() { 9   idx--; 10    return data[ idx]; 11 } 12 } 说明: •当synchronized方法执行完或发生异常时,会自动释放锁。

Does synchronized keyword prevent reordering in Java?

那年仲夏 提交于 2019-12-17 18:43:39
问题 Suppose I have the following code in Java a = 5; synchronized(lock){ b = 5; } c = 5; Does synchronized prevent reordering? There is no dependency between a, b and c. Would assignment to a first happen then to b and then to c? If I did not have synchronized, the statements can be reordered in any way the JVM chooses right? 回答1: Does synchronized prevent reordering? It prevents some re-ordering. You can still have re-ordering outside the synchronized block and inside the synchronized block, but