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



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. Hello,
I've tried the above code, but I've

View Tutorial          By: Phyo Phyo at 2009-09-14 05:32:28

2. Really awesome keep posting ........
View Tutorial          By: viswanath at 2013-01-30 07:38:13

3. Thanks :) It works for me :)
View Tutorial          By: Milind at 2011-12-07 10:05:23

4. hey plz tell me... can the sequenceinputstream be
View Tutorial          By: gaurav at 2010-08-18 17:21:58

5. Thank you very much you resolved my issue.
View Tutorial          By: ketan at 2012-02-01 11:16:47

6. Hi Everyone, i tried the code. im using this code.
View Tutorial          By: sourabh at 2011-09-09 12:42:02

7. just remember to add
<uses-permission an

View Tutorial          By: Mouad at 2013-07-22 10:47:27

8. A big thanks to Ramlak for the detailed and crisp
View Tutorial          By: Chidanand Gangur at 2009-08-27 06:02:16

9. Really just brilliant...thanks
View Tutorial          By: Ash Singh at 2011-02-21 04:47:20

10. I tried to create an object on my 6th code but i a
View Tutorial          By: AARON VICUÑA at 2009-03-24 05:20:24