Skip to content

Commit 83508ba

Browse files
committed
java: adjust qhelp and examples for SafePublication
1 parent 9e77e5b commit 83508ba

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
public class SafePublication {
2-
private Object value;
2+
private volatile Object value;
3+
private final int server_id;
34

4-
public synchronized void produce() {
5-
value = new Object(); // Safely published using synchronization
5+
public SafePublication() {
6+
value = new Object(); // Safely published as volatile
7+
server_id = 1; // Safely published as final
68
}
79

810
public synchronized Object getValue() {
911
return value;
1012
}
13+
14+
public int getServerId() {
15+
return server_id;
16+
}
1117
}

java/ql/src/Likely Bugs/Concurrency/SafePublication.qhelp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ Choose a safe publication technique that fits your use case. If the value only n
3131
</recommendation>
3232
<example>
3333

34-
<p>In the following example, the value of <code>value</code> is not safely published. The <code>produce</code> method
35-
creates a new object and assigns it to the field <code>value</code>. However, the field is not
36-
declared as <code>volatile</code>, and there are no synchronization mechanisms in place to ensure
37-
that the value is fully constructed before it is published.</p>
34+
<p>In the following example, the values of <code>value</code> and <code>server_id</code> are not safely published. The constructor creates a new object and assigns it to the field <code>value</code>. However, the field is not declared as <code>volatile</code> or <code>final</code>, and there are no synchronization mechanisms in place to ensure that the value is fully constructed before it is published. A different thread may see the default value <code>null</code>. Similarly, the field <code>server_id</code> may be observed to be <code>0</code>.</p>
3835

3936
<sample src="UnsafePublication.java" />
4037

41-
<p>To fix this example, declare the field <code>value</code> as <code>volatile</code>, or use
42-
synchronized blocks or methods to ensure that the value is fully constructed before it is
43-
published. We illustrate the latter with the following example:</p>
38+
<p>To fix this example, we declare the field <code>value</code> as volatile. This will ensure that all changes to the field are visible to all threads. The field <code>server_id</code> is only meant to be written once, so we only need the write inside the constructor to be visible to other threads; declaring it <code>final</code> guarantees this:</p>
4439

4540
<sample src="SafePublication.java" />
4641

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
@ThreadSafe
21
public class UnsafePublication {
32
private Object value;
3+
private int server_id;
44

5-
public void produce() {
5+
public UnsafePublication() {
66
value = new Object(); // Not safely published, other threads may see the default value null
7+
server_id = 1; // Not safely published, other threads may see the default value 0
78
}
89

910
public Object getValue() {
1011
return value;
1112
}
13+
14+
public int getServerId() {
15+
return server_id;
16+
}
1217
}

0 commit comments

Comments
 (0)