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



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

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions 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

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. My exchange email account is only partially delete
View Tutorial          By: ali at 2010-12-30 02:38:14

2. this work is good
View Tutorial          By: sanath at 2008-12-15 01:18:43

3. Hello,
could you send me the complete code

View Tutorial          By: Sanjay at 2013-04-19 07:28:12

4. This all works fine, but when I read records from
View Tutorial          By: Ayac at 2011-10-31 10:58:21

5. thanks....good explanation...
View Tutorial          By: jiby at 2012-01-14 05:57:33

6. Great Help...
View Tutorial          By: adity at 2012-04-27 05:34:26

7. thanks to this site which get me understand about
View Tutorial          By: Sayak at 2012-10-08 15:57:25

8. Hi ngocanh, You are right. The client has to be in
View Tutorial          By: Emiley J at 2013-07-14 05:51:35

9. Hello! May I know where is the "main" fu
View Tutorial          By: Angelia at 2009-06-25 18:54:28

10. Hi
I can compile it
root$ javac -cp

View Tutorial          By: Tebogo Modiselle at 2014-10-31 08:47:09