aspose file tools*
The moose likes Threads and Synchronization and the fly likes IS an Instance variable threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "IS an Instance variable threadsafe?" Watch "IS an Instance variable threadsafe?" New topic
Author

IS an Instance variable threadsafe?

samir vasani
Ranch Hand

Joined: Nov 24, 2010
Posts: 62
Dear Expert,

Please explain me with the example.
Kuma Phani
Greenhorn

Joined: Jan 27, 2012
Posts: 1
Yes, instance variable in enum is threadsafe.

public enum BMW{

INSTANCE;

}

INSTANCE - is thread safe.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Kuma Phani wrote:Yes, instance variable in enum is threadsafe.

Can you answer why that particular type of reference is thread safe?

If the question is if, in general, variables belonging to an instance are thread safe, the answer is no. If the question is if a variable belonging to an instance can be made thread safe, the answer is yes. Here is an example of an instance variable that is not safe:



Here is an example of one that is safe:


Steve
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Kuma Phani wrote:Yes, instance variable in enum is threadsafe.

public enum BMW{

INSTANCE;

}

INSTANCE - is thread safe.


FALSE

No variable or object in Java is intrinsically threadsafe. If an object is accessible by 2 different threads, they can totally slaughter it.

To be threadsafe, mechanisms must be employed to enforce threadsafe access. The most common mechanism used for this purpose is Java's built-in "synchronized" capabilities. You use the "synchronized" modifier to ensure that critical properties, methods, or even the class instance as a while are only accessed according to proper threading protocols.

Nothing is synchronized by default, however.

For an example of a class whose methods have been made threadsafe, look at the java StringBuffer class. Then contrast it with the newer and non-threadsafe StringBuilder class.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IS an Instance variable threadsafe?