Comment on Tutorial - Transient vs Volatile modifiers in Java By Reema sen
Comment Added by : Mayank
Comment Added at : 2011-01-25 15:44:21
Comment on Tutorial : Transient vs Volatile modifiers in Java By Reema sen
Real Life example of using Volatile
******************************************
class StackImpl {
private Object[] stackArray;
private volatile int topOfStack;
StackImpl (int capacity) {
stackArray = new Object[capacity];
topOfStack = -1;
}
public synchronized Object pop() {
System.out.println(Thread.currentThread() + ": popping");
while (isEmpty())
try {
System.out.println(Thread.currentThread() + ": waiting to pop");
wait(); // (1)
} catch (InterruptedException e) { }
Object obj = stackArray[topOfStack];
stackArray[topOfStack--] = null;
System.out.println(Thread.currentThread() + ": notifying after pop");
notify(); // (2)
return obj;
}
public synchronized void push(Object element) {
System.out.println(Thread.currentThread() + ": pushing");
while (isFull())
try {
System.out.println(Thread.currentThread() + ": waiting to push");
wait(); // (3)
} catch (InterruptedException e) { }
stackArray[++topOfStack] = element;
System.out.println(Thread.currentThread() + ": notifying after push");
notify(); // (4)
}
public boolean isFull() { return topOfStack >= stackArray.length -1; }
public boolean isEmpty() { return topOfStack < 0; }
}
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. why are we using it ch = str.charAt(i);
View Tutorial By: Rumman at 2014-10-10 19:42:42
2. This tutorial is very nice for beginners.
View Tutorial By: Wakil Ahamad at 2009-02-07 05:58:31
3. Good examples for beginners.
View Tutorial By: Sagar at 2009-10-16 01:04:46
4. Plz help me......its urgent
i need a code
View Tutorial By: Nisha at 2010-03-22 00:38:42
5. thank you very much....
View Tutorial By: divya at 2013-01-06 15:50:43
6. no output is shown
View Tutorial By: shubham bansal at 2015-02-21 15:36:31
7. It is very good example to know the concept better
View Tutorial By: abdulkadir at 2010-01-27 00:56:07
8. The arraycopy in a System is static native method
View Tutorial By: Ashwinkumar.vj at 2011-07-19 09:25:02
9. nice material
View Tutorial By: ravi at 2008-12-24 00:40:12
10. hai
i can run this program bt 1 exception
View Tutorial By: Raheela at 2010-05-25 21:59:38