Questions tagged [safe-publication]

42 questions
46
votes
6 answers

Java multi-threading & Safe Publication

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…
Schildmeijer
  • 20,702
  • 12
  • 62
  • 79
19
votes
4 answers

Read field stale value after object construction

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) { …
11
votes
3 answers

Volatile guarantee safe publication of a mutable object?

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…
Guifan Li
  • 1,543
  • 5
  • 14
  • 28
10
votes
3 answers

Safe publication of local final references

I know that you can safely publish a non-thread safe object by writing the reference to a final or volatile field which will later be read by exactly one other thread, provided that upon publication, the thread that created the object discards the…
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
9
votes
2 answers

Can a thread first acquire an object via safe publication and then publish it unsafely?

This question came to me after reading this answer. Code example: class Obj1 { int f1 = 0; } volatile Obj1 v1; Obj1 v2; Thread 1 | Thread 2 | Thread 3 ------------------------------------------------- var o = new Obj1(); | …
user15215795
5
votes
3 answers

How to publish StringBuffer safely?

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…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
4
votes
1 answer

Use volatile field to publish an object safely

From the book Java Concurrency In Practice: 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…
4
votes
1 answer

Safe publication of a ConcurrentHashMap into a class field

I am trying to write a test that demonstrates that assigning a new reference to a class' field in a multi-threading environment is not thread-safe and more specifically has visibility problems if that field is not declared as volatile or…
c.s.
  • 4,786
  • 18
  • 32
4
votes
1 answer

Will this AssertionError never be thrown in this case?

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 { // …
katiex7
  • 863
  • 12
  • 23
3
votes
1 answer

Java Concurrency in Practice Listing 6.2

The following code snippet is from listing 6.2 in Java Concurrency in Practice (http://jcip.net/listings/ThreadPerTaskWebServer.java) package net.jcip.examples; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** …
bhh1988
  • 1,262
  • 3
  • 15
  • 30
3
votes
2 answers

Double checked locking without using volatile-keyword and without synchronizing the entire getInstance() method

Following is my singleton class where I am using double-checked-locking without using volatile keyword and without synchronizing the entire getInstance() method: public class MySingleton { private static MySingleton mySingleton; public…
2
votes
1 answer

Export Cox regression results to excel or word using publish package

Looking at the code below library(pec) data(GBSG2,package="pec") setDT(GBSG2) GBSG2 library(survival) library(prodlim) library(Publish) cox_lung <-…
Allan
  • 321
  • 1
  • 8
2
votes
1 answer

Thread-safe access to a Kotlin var from multiple threads

Consider the following Kotlin code: import kotlin.concurrent.thread fun main() { println("Press to terminate.") var interrupted = false val worker = thread { while (!interrupted) { println("Working...") …
Bass
  • 4,977
  • 2
  • 36
  • 82
2
votes
2 answers

Safe initialisation of null reference

I'm wondering what publication guarantees exist for a non-final field initialised to null, if any. Consider the following snippet: public class MyClass { private final CopyOnWriteArrayList list = new CopyOnWriteArrayList<>(); …
2
votes
1 answer

Java final & Safe Publication

When I read jsr-133-faq, in question "How do final fields work under the new JMM?", it said: class FinalFieldExample { final int x; int y; static FinalFieldExample f; public FinalFieldExample() { x = 3; y = 4; } static void…
azs1478963
  • 23
  • 2
1
2 3