Questions tagged [safe-publication]
42 questions
2
votes
1 answer
Is happens-before transitive when calling Thread.start()?
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(() -> {
…

katiex7
- 863
- 12
- 23
2
votes
2 answers
Are objects that have no state always visible when published?
Say I have a class
public class Foo {
public void printHi() {
System.out.print("Hi");
}
}
and in some client code I do something like
public static void main() {
Foo foo = new Foo();
(new Thread(() ->…

katiex7
- 863
- 12
- 23
2
votes
1 answer
does "unsafe publication" exist in Javascript?
I just wrote the following Javascript code...
class Team {
constructor(id, eventHandler) {
this.id = id;
this._eventHandler = eventHandler;
this._eventHandler.trigger("NewTeam", this); // my concerning line
}
}
I…

WoodenKitty
- 6,521
- 8
- 53
- 73
2
votes
1 answer
Error occured when opening Epub from PHP header EPUB file using Epub JS Library
I'm trying to open Epub file from my own server.
PHP:
2
votes
1 answer
Is this Class thread safe? if not can someone explain a scenario?
I am trying to understand safe publication in case of Effectively Immutable classes. For my class I cannot come up with a scenario in which it would be thread unsafe. Do I need to add some other safe gaurd?
CLARIFICATION: Container elements are…

Suryavanshi
- 595
- 1
- 7
- 24
2
votes
2 answers
Giving recovery time for an unsafely published java.lang.String
java.lang.String is only effectively immutable. Brian Goetz of "Java Concurrency in Practice" said something like effectively immutable objects will only be thread safe if safely published. Now, say I unsafely publish String like this:
public class…

damat-perdigannat
- 5,780
- 1
- 17
- 33
2
votes
3 answers
How private constructors achieve publication safety in Java
In the classic "Java concurrency in Practice" Brian Goetz uses the following snippet of code to demonstrate how to safely publish an object using a private constructor and a factory method:
public class SafeListener {
private final EventListener…

stratis
- 7,750
- 13
- 53
- 94
1
vote
0 answers
ACM Digital Library Search Problems
I have problems using the ACM advanced search at https://dl.acm.org/search/advanced
I only want to search for abstracts, so I choose the abstract field in the "Search within" block.
To provide a simple example, I use the following search…

a1nez
- 21
- 3
1
vote
1 answer
Why are both "Storing a reference to it into a final field" and "of a properly constructed object" needed in order to publish an object safely?
I'm reading "Java concurrency in practice", and it says:
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…

Jason Law
- 965
- 1
- 9
- 21
1
vote
1 answer
Do final fields initialized outside of constructors get initialized before running the constructor?
Say you have this code snippet
private final Set set = new HashSet() {{ add(1); }};
SomeConstructor() {
printSet();
}
long printSet() {
new Thread(() -> {System.out.println(set)}).start();
}
for example if the compiler decided to make…

katiex7
- 863
- 12
- 23
1
vote
1 answer
Does improperly constructed object only affect visibility for the thread it publishes inside of tbe constructor?
When http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#finalRight says
The values for an object's final fields are set in its constructor. Assuming the object is constructed "correctly", once an object is constructed, the values…

katiex7
- 863
- 12
- 23
1
vote
1 answer
EJB & safe publication
I have been reading lately about safe publication of Java objects (e.g. here: http://shipilev.net/blog/2014/safe-public-construction/).
Until now I was trusting EJB container without questions when relying on container managed concurrency.
Now I'm…

Lars
- 11
- 3
1
vote
1 answer
Spring Safe-Publication
I know a similar question was asked several times, but I still can't find the only true answer.
public class SimpleMovieLister {
private MovieFinder movieFinder;
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder =…

d3rzKy
- 142
- 2
- 9
1
vote
3 answers
Ensuring safe publication and thread safety in java by means of static factories
The class below is meant to be immutable (but see edit):
public final class Position extends Data {
double latitude;
double longitude;
String provider;
private Position() {}
private static enum LocationFields implements
…

Mr_and_Mrs_D
- 32,208
- 39
- 178
- 361
0
votes
1 answer
Project Reactor and Safe Publication
I need your help. There is a reactive chain in a Spring WebFlux application that uses R2dbcRepository:
entityRepository //0
.findById(entityId) //1 Mono
.doOnNext(e-> e.setValue("0")) //2
.flatMap(entity-> //3
…

user18032014
- 13
- 3