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
Subscribe to Tutorials
Related Tutorials
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
MultiLevel Inheritance sample in Java
Archived Comments
1. It works great,
my code looks like this:
View Tutorial By: Anonymous at 2009-04-11 04:19:28
2. Thanks a lot for that excellent explanation. It ha
View Tutorial By: Gregory at 2014-12-30 05:49:21
3. Hi there, a nice tutorial. i wanna ask, what about
View Tutorial By: Alfa at 2013-03-06 04:04:26
4. Hey thanks for your tutorial.
But i
View Tutorial By: Matze at 2013-07-19 16:08:46
5. really nice and perfect ! thnak you very much for
View Tutorial By: arash at 2013-07-16 10:58:45
6. Sir, plz tell me how can I run the speech class in
View Tutorial By: Nitin Govil at 2012-01-21 10:14:52
7. i am looking for the samples in java programming u
View Tutorial By: mary grace leron at 2010-02-18 01:50:50
8. thanx allot its a very good sample , but im trying
View Tutorial By: Waleed Qaffaf at 2011-06-09 09:37:15
9. Good one ! and simple tutorial
View Tutorial By: Sim at 2011-02-24 01:57:53
10. can any beginners can try creating web pages
View Tutorial By: jo at 2015-07-28 19:35:11