I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also:
Shared Object:
public class ThreadValue {
private String caller;
private String value;
public ThreadValue( String caller, String value ) ...
|
I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also:
Shared Object:
public class ThreadValue {
private String caller;
private String value;
public ThreadValue( String caller, String value ) ...
|
In java, assignment is atomic if the size of the variable is less that or equal to 32 bits but is not if more than 32 bits. What(volatile/synchronized) would be more ... |
If I have a volatile boolean (let's call it valid), is the following piece of code thread-safe in Java?
if (valid)
return;
valid = true;
Or, do I need to synchronize since valid ... |
This is a followup question to: http://stackoverflow.com/questions/3077703/java-serialization-of-objects-in-a-multithreaded-environment.
Suppose I have a collection of objects that are always acted upon by multiple threads.
public class AlwaysChanging implements Serializable {
// ...
|
In a nice article with some concurrency tips, an example was optimized to the following lines:
double getBalance() {
Account acct = verify(name, password);
...
|
Does a variable that is accessed by multiple threads, but only inside synchronized blocks, need the volatile modifier? If not, why?
|
|
I am wondering at the difference between declaring a variable as volatile and always accessing the variable in a synchronized(this) block in JAVA (particularly J2ME)?
According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml ... |
Does the following variable, x, need to be volatile?
Or does the manipulation within a utils.concurrent lock perform the same function as a synchronized block (ensuring it's written to memory, and ... |
Java 5 and above only. Assume a multiprocessor shared-memory computer (you're probably using one right now).
Here is a code for lazy initialization of a singleton:
public final class MySingleton {
...
|
So I just learned about the volatile keyword while writing some examples for a section that I am TAing tomorrow. I wrote a quick program to demonstrate that the ++ and ... |
Consider a primitive type variable with lots of threads reading and a few threads writing, will the following code work correctly?
If it will, does it provide better performance than 1). declaring ... |
I'm reading a book which says not to use such a code:
private volatile Thread myThread;
....
myThread.stop();
Instead one should use:
if (myThread != null ) {
Thread dummy = myThread;
myThread = null;
dummy.interrupt();
}
Unfortunately ... |
Lets look at classical double check
class Foo {
private volatile Foo singleton = null;
public Foo getFooSingleton() {
...
|
I read somewhere that x86 processors have cache coherency and can sync the value of fields across multiple cores anyway on each write.
Does that mean that we can code without using ... |
Say that I have a private variable and I have a setVariable() method for it which is synchronized, isn't it exactly the same as using volatile modifier?
|
|
|
If you just want to do a simple operation like set a flag, (done = true) or check a flag, (if (done != true) { blah } ), there is really little reason to synchronize in order to be thread safe. You can just declare the variable as volatile and avoid synchronization all-together. Prior to JDK 1.5, this was probably the ... |
Greetings, I'm trying to really understand the semantics of volatile and synchronized when it comes to visibility. import java.util.concurrent.TimeUnit; class A { boolean stopRequested = false; } public class StopThread { private static volatile A a = new A(); public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { public void run() { int i = 0; while (!a.stopRequested) ... |
|
|
For example, in the following code (of a Singleton), is there any reason the instance variables need to be marked volatile to be safe, or is the synchronization sufficient for Java 1.5 code? ... private Map someCachedMap; private Map someOtherCachedMap; private long lastRefreshTime; ... public void someMethod() { Map someMap; Map someOtherMap; ... synchronized(this) { if (lastRefreshTime < System.currentTimeMillis() - 60000) ... |
|
|
|