Comment on Tutorial - Transient vs Volatile modifiers in Java By Reema sen
Comment Added by : it career
Comment Added at : 2010-02-24 00:13:50
Comment on Tutorial : Transient vs Volatile modifiers in Java By Reema sen
If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value.
package javabeat.samples;
class ExampleThread extends Thread {
private volatile int testValue;
public ExampleThread(String str){
super(str);
}
public void run() {
for (int i = 0; i < 3; i++) {
try {
System.out.println(getName() + " : "+i);
if (getName().equals("Thread 1 "))
{
testValue = 10;
}
if (getName().equals("Thread 2 "))
{
System.out.println( "Test Value : " + testValue);
}
Thread.sleep(1000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
}
public class VolatileExample {
public static void main(String args[]) {
new ExampleThread("Thread 1 ").start();
new ExampleThread("Thread 2 ").start();
}
}
Not Working i tried it... but
---------- run java ----------
Thread 1 : 0
Thread 2 : 0
Test Value : 0
Thread 2 : 1
Test Value : 0
Thread 1 : 1
Thread 1 : 2
Thread 2 : 2
Test Value : 0
Output completed (3 sec consumed)
<a href="http://www.itcareer.co.in">it career</a>
View Tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
Archived Comments
1. sir there is no startapp(),destroyapp() and pause
View Tutorial By: Sathish at 2012-08-20 08:49:33
2. So simple..Very easy to understand
View Tutorial By: G.Tejasree at 2011-11-05 11:51:38
3. hai...
i am completed MCA am good at java.
View Tutorial By: HEMALATHA at 2013-08-03 10:04:57
4. Hi thanks for the example for overriding toString(
View Tutorial By: Reshma at 2011-05-11 10:46:08
5. thank for the sharing.
View Tutorial By: likelove at 2014-11-22 12:41:58
6. I running this on localhost offline and getting er
View Tutorial By: nitisha at 2011-05-14 09:18:16
7. I'm working on an intranet site/web application. W
View Tutorial By: Tony Gingrich at 2011-10-23 21:22:31
8. in statment -
new Treeset<String> (ne
View Tutorial By: Siddhesh Khedekar at 2009-10-27 00:14:17
9. when I exported the data to excel there is a value
View Tutorial By: mizhelle at 2015-10-10 13:17:56
10. Good example thank you so much
View Tutorial By: Albadri at 2011-05-14 15:48:17