safe-publication

When a form is handled in JSF, does it all happen in one thread?

本小妞迷上赌 提交于 2019-12-24 22:09:09
问题 Say I have this piece of code <p:dataTable styleClass="scheduleTable" value="#{todaySchedule.hours}" var="hour"> <p:column headerText="Hour" styleClass="hourColumn" > #{hour.time} </p:column> </p:dataTable> and in a class called todaySchedule, have a method public List<Hour> getHours() { final List<Hour> hours = IntStream.range(0, Hour.TIME.values().length) .mapToObj($ -> new Hour()).collect(Collectors.toList()); for (int i = 0; i < 5; i++) { hours.get(i).setHour(1); } return hours; } and

Is happens-before transitive when calling Thread.start()?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 21:19:33
问题 Let's say we have a class class Foo { int x; Foo() { x = 5; } } and some client code public static void main(String[] args) { Foo foo = new Foo(); new Thread(() -> { while (true) { new Thread(() -> { if (foo.x != 5) { throw new AssertionError("this statement is false 1"); } new Thread(() -> { if (foo.x != 5) { throw new AssertionError("this statement is false 2"); } }).start(); }).start(); } }).start(); } Is it impossible for an AssertionError to be thrown because happens-before is transitive

Concurrency object creation in Java

梦想与她 提交于 2019-12-21 07:38:53
问题 I'm reading a book "Java concurrency in practice" by Brian Goetz. Paragraphs 3.5 and 3.5.1 contains statements that I can not understand. Consider the following code: public class Holder { private int value; public Holder(int value) { this.value = value; } public void assertValue() { if (value != value) throw new AssertionError("Magic"); } } class HolderContainer { // Unsafe publication public Holder holder; public void init() { holder = new Holder(42); } } Author states that: In Java, Object

How to publish StringBuffer safely?

百般思念 提交于 2019-12-10 15:00:26
问题 Since StringBuffer is thread safe it can safely be published. Consider the public constructor of StringBuffer ( sources ): public StringBuffer() { super(16); } where super(16) designates this one: AbstractStringBuilder(int capacity) { value = new char[capacity]; } where value declared as char[] value; QUESTION: How to publish StringBuffer safely? I've got the following class: public class Holder{ public final StringBuffer sb = new StringBuffer(); } Can it be considered as safe-publication? I

Volatile guarantee safe publication of a mutable object?

北战南征 提交于 2019-12-07 00:59:16
问题 By reading Java Concurrency in Practice I can see: To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer Storing a reference to it into a volatile field or AtomicReference Storing a reference to it into a final field of a properly constructed object Storing a reference to it into a field that

Volatile guarantee safe publication of a mutable object?

笑着哭i 提交于 2019-12-05 05:01:51
By reading Java Concurrency in Practice I can see: To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer Storing a reference to it into a volatile field or AtomicReference Storing a reference to it into a final field of a properly constructed object Storing a reference to it into a field that is properly guarded by a lock. However, I am confused about the second idiom. Since volatile can only

Is calling start() on a object of this class safe? An example from Java Concurrency in practice

情到浓时终转凉″ 提交于 2019-12-02 16:23:16
问题 First off, I will give links to the source code that I will be talking about since copy/paste would make this question page too long. In Listing 5.15 http://jcip.net/listings/CellularAutomata.java of JCIP, I imagine that in some main method, one will create a CellularAutomata object and then call start() on that object. However, is it okay to do so? When the object's start method is called, it will create N(number of processors) threads with instances of Worker. It seems though that the N

Is calling start() on a object of this class safe? An example from Java Concurrency in practice

℡╲_俬逩灬. 提交于 2019-12-02 07:30:02
First off, I will give links to the source code that I will be talking about since copy/paste would make this question page too long. In Listing 5.15 http://jcip.net/listings/CellularAutomata.java of JCIP, I imagine that in some main method, one will create a CellularAutomata object and then call start() on that object. However, is it okay to do so? When the object's start method is called, it will create N(number of processors) threads with instances of Worker. It seems though that the N threads that are created with the worker object might be seeing a incomplete reference or object of that

Will this AssertionError never be thrown in this case?

自闭症网瘾萝莉.ら 提交于 2019-12-02 00:21:20
问题 First off the code, from JCIP listing http://jcip.net/listings/StuffIntoPublic.java and http://jcip.net/listings/Holder.java public class SafePublication { public static void main(String[] args) throws InterruptedException { // System.out.println(Thread.currentThread().getName()); StuffIntoPublic t = new StuffIntoPublic(); t.initialize(); while (true) { new Thread(() -> { t.holder.assertSanity(); }).start(); } } } //@author Brian Goetz and Tim Peierls class StuffIntoPublic { public Holder

Java multi-threading & Safe Publication

ぃ、小莉子 提交于 2019-11-26 00:19:53
问题 After reading \"Java concurrent in practice\" and \"OSGI in practice\" I found a specific subject very interesting; Safe Publication. The following is from JCIP: To publish an object safely, both the reference to the object and the object\'s state must be made visible to other threads at the same time. A properly constructed object can be safely published by: Initializing an object reference from a static initializer. Storing a reference to it into a volatile field. Storing a reference to it