public class Piggybacking {
private volatile boolean flag;
private Item i;
public void doGet() {
boolean _flag = flag;
Item _i = this.i;
int _one = _i.one;
int _two = _i.two;
int _three = _i.three;
}
public void doSet(Item i) {
this.i = i;
flag = true;
}
public static class Item {
public int one, two, three;
public Item(int one, int two, int three) {
this.one = one;
this.two = two;
this.three = three;
}
}
}
afaik, if there are two threads one calling doGet() and other calling doSet(), we are still thread-safe due to piggy-backing on happens-before relationship. Is this true?
What if there are more threads hitting doGet() and doSet()? Are we still thread-safe?
Cheers.
--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
From: mechanica...@googlegroups.com [mailto:mechanica...@googlegroups.com] On Behalf Of Ladislav Thon
Sent: Thursday, February 5, 2015 2:29 AM
To: mechanica...@googlegroups.com
Subject: Re: Piggybacking on happens-before
I think the question here is if doGet() reads the new value of flag as set by doSet(), will it also read the new value of the item? If my limited understanding is correct, then yes, it will, but I don't think this is called "thread safety".
2015-02-05 9:15 GMT+01:00 Jan van Oort <exercit...@gmail.com>:
It's not thread-safe at all. Imagine one thread calling doGet() and executing the first line of doGet(), and at the same time the other thread calling doSet() and also executing the first line of that method. The Item that doGet() gets to see is wholly indeterminate, and depends only upon the relative execution speeds and call order. The flag protects you from nothing at all IMHO -- unless you use the "flag" field to perform some sort of locking. Which becomes a source of headache when one more than one thread is simultaneously executing doGet().
To sum it up: if you want thread safety, this is strange and definitely the wrong approach. Use a semaphore, a mutex or - computationally the most expensive, but also the most elegant - a Barrier or a RendezVous.
Fortuna audaces adiuvat - hos solos ?
On 5 February 2015 at 09:08, tm jee <tmj...@gmail.com> wrote:
It doesn't matter much, it's just a sample, the idea is to determine the thread safety of _i in it.
On Thursday, 5 February 2015 19:04:22 UTC+11, Jan van Oort wrote:
What result do you want to reach with doGet() ? It has return type "void" ???
Fortuna audaces adiuvat - hos solos ?
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public class Piggybacking2 {
private volatile Item item;
public Item get() {
Item _item = item;
return _item; // could be null
}
public void set(Item item) {
Item _item = new Item(item.one, item.two, item.three);
item = _item;
}
public static class Item {
public final int one, two, three;
}