synchronized

单例模式的懒汉饿汉

旧巷老猫 提交于 2019-12-03 03:01:21
单例模式的三个特点:   1. 私有构造方法;   2. 内部创建静态对象;   3. 提供静态方法返回该静态对象; 单例设计模式主要有两种实现方法:饿汉式和懒汉式   饿汉式单例:在定义开始,便实例化自己。 class Singleton0 { //构造方法私有化 private Singleton0() { } //内部创建静态对象并实例化 private static Singleton0 s=new Singleton0(); //提供静态方法返回该静态对象 public static Singleton0 getsingle() { return s; } }   懒汉式单例:在第一次调用时实例化自己。 class Singleton1{ //构造方法私有化 private Singleton1() { } //内部创建静态对象 public static Singleton1 single=null; //提供静态方法返回该静态对象 public static Singleton1 getsingle() { //静态对象实例化 single=new Singleton1(); return single; } }   懒汉式与饿汉式的区别:     1. 线程安全:       饿汉式天生线程安全,可以直接用于多线程而不会出现问题。       懒汉式本身非线程安全

Mixing synchronized() with ReentrantLock.lock()

北战南征 提交于 2019-12-03 03:01:00
In Java, do ReentrantLock.lock() and ReetrantLock.unlock() use the same locking mechanism as synchronized() ? My guess is "No," but I'm hoping to be wrong. Example: Imagine that Thread 1 and Thread 2 both have access to: ReentrantLock lock = new ReentrantLock(); Thread 1 runs: synchronized (lock) { // blah } Thread 2 runs: lock.lock(); try { // blah } finally { lock.unlock(); } Assume Thread 1 reaches its part first, then Thread 2 before Thread 1 is finished: will Thread 2 wait for Thread 1 to leave the synchronized() block, or will it go ahead and run? No, Thread 2 can lock() even when Thread

Sharing an object between two threads and main program

女生的网名这么多〃 提交于 2019-12-03 02:49:22
I am new to Java and I'm attending a Concurrent Programming course. I am desperately trying to get a minimal working example that can help to demonstrate concepts I have learnt like using 'synchronized' keyword and sharing an object across threads. Have been searching, but could not get a basic framework. Java programmers, kindly help. Here is a very shot example of sharing an array between two threads. Usually you will see all zeros, but sometimes things get screwy and you see other numbers. final int[] arr = new int[100]; Thread one = new Thread() { public void run() { // synchronized (arr)

Acquiring inner-class lock using outer-class locks?

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an outer class A and that has a public inner class, the structure is as follows -: public class A{ public int x; public class B{ private static final int y; } public synchronized int method1(){ return x+ B.y; } } the question is if I used synchronized keyword on every method of the class A will it also lock the members of the inner class as well? 回答1: if I used synchronized keyword on every method of the class A will it also lock the members of the inner class as well? No it won't. You seem to be confused in a number of respects here.

wait(n) is acting different each time I change the position of synchronized keyword

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Refer below code public void acquire(){ synchronized(a){ print("acquire()"); try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } print("I have awoken"); print("" + a); }catch(Exception e){ e.printStackTrace(); } } print("Leaving acquire()"); } public void modify(int n){ print("Entered in modfy"); synchronized(a){ try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } this.a=n; print("new value" + a); }catch(Exception e){ e.printStackTrace(); } } } And final SynchoTest ttObj = new SynchoTest(); Thread A = new Thread(new

synchronized block - lock more than one object

流过昼夜 提交于 2019-12-03 02:37:09
问题 I'm modelling a game where multiple players (threads) move at the same time. The information of where a player is located at the moment is stored twice: the player has a variable "hostField" that references to a field on the board and every field has an ArrayList storing the players that are currently located at this field. I'm not very happy with the fact that I have redundant information, but I found no way avoiding that without looping through a big dataset. However, when a player moves

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

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 lock for the same object? 回答1: But could a

is synchronized by class name in a function will be valid across extended classes?

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a methode foo in base class uses Synchronized (class name) , and two classes A and B that extends the base class. if i called foo from A instance and B instance in two different thread are they gonna be Synchronized. here's a sample code : class BaseClass { void foo () { synchronized ( BaseClass . class ) // do something like increment count } } class A extends BaseClass { } class B extends BaseClass { } A a = new A (); B b = new B (); //in thread 1 a . foo () ; //in thread 2 b . foo () ; 回答1: Yes, that will be synchronized

java面试题

大憨熊 提交于 2019-12-03 02:15:55
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)); //

What is the difference between a synchronized method and synchronized block in Java? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Is there an advantage to use a Synchronized Method instead of a Synchronized Block? 22 answers What is the difference between a synchronized method and synchronized block in Java ? I have been searching the answer on the Net, people seem to be so unsure about this one :-( My take would be there is no difference between the two, except that the synch block might be more localized in scope and hence the lock will be of lesser time ?? And in case of Lock on a static method, on what is the Lock taken ?