|
| ||||||||||
| Tags: atomic variables, java, programming language, volatile variable |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| What are an Atomic Variables in Java?
I have recently completed some part in the threads section of Java programming language. Now I am stuck at the point where I have to define an atomic variables. I have never gone through this term. So I don't know anything about this.!! Hope that someone would know there.! Please tell me what are an Atomic Variables in Java? Help me soon.!! ![]() |
|
#2
| ||||
| ||||
| Re: What are an Atomic Variables in Java?
The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes have get and set methods that work like reads and writes on volatile variables. Knowing atomic operation is very important when we are writing thread operation in Java. Classes providing atomic access to main primitive types or to an object reference: AtomicBoolean, AtomicInteger and AtomicLong, AtomicReference. |
|
#3
| ||||
| ||||
| Re: What are an Atomic Variables in Java?
Classes providing atomic access to fields in arrays of the correspoding types: AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray. Classes to atomically couple a boolean or integer with a reference field: AtomicMarkableReference and AtomicStampedReference. Java Specification says that assignment to variables smaller than or equal to 32 bits is an atomic operation, which excludes variables of types double and long (both are 64 bits). So, the operation is atomic or not completely depends on type. |
|
#4
| ||||
| ||||
| Re: What are an Atomic Variables in Java?
To see how this package might be used, let's return to the Counter class we originally used to demonstrate thread interference : Code: class CounterDemo {
private int c = 0;
public void increment() {
c++;
}
public void decrement() {
c--;
}
public int value() {
return c;
}
} |
|
#5
| ||||
| ||||
| Re: What are an Atomic Variables in Java?
One way to make Counter safe from thread interference is to make its methods synchronized, as in SynchronizedCounter is shown in the following example : Code: class SynchronizedCounterDemo {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
} |
|
#6
| |||
| |||
| Re: What are an Atomic Variables in Java?
Hi, Only read and write operation on variable less than 32 bit are atomic in Java , so when you increment or decrement in Integer its not an atomic operation and can crate memory corruption problem. to solve this Java 5 adds concept of Atomic variables e.g. AtomicInteger, AtomicLong , AtomicBoolean etc on these variable certain operation can be perform atomically and that's why they are very useful in multithreaded applicaiton. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "What are an Atomic Variables in Java?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| About instance variables in java | Khaled11 | Software Development | 1 | 19-03-2010 02:38 PM |
| Java properties / variables dynamically | Miles Runner | Software Development | 5 | 04-03-2010 10:31 AM |
| What is Instance variables in Java? | Owen Fernandes | Software Development | 5 | 23-01-2010 07:27 AM |
| Multiplying Variables in java | WinDoWLoCuS | Software Development | 3 | 03-12-2009 12:43 PM |
| Java bans Global Variables | KALLIYAN | Software Development | 4 | 10-11-2009 03:17 AM |