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. Good Article. Small and Sweet...
View Tutorial          By: Anupam Pawar at 2009-05-27 03:04:08

2. i'am having an error in main.xml in line RelativeL
View Tutorial          By: Nova at 2013-04-18 11:21:06

3. Thanks a lot .. Great work!!
View Tutorial          By: MonamAlwerfally at 2013-03-26 14:41:29

4. Thanks
View Tutorial          By: Bad Programmer at 2011-11-23 10:43:49

5. Excellent work mate! works like a charm! Lifesaver
View Tutorial          By: jmcg at 2015-01-29 02:31:56

6. Thank you for sharing your knowledge. =>
View Tutorial          By: Condel Batac at 2013-02-03 23:17:26

7. Thanks a lot! This really helped!!!
View Tutorial          By: SuperSeppel13 at 2009-04-14 04:25:06

8. Thanks a lot!!!

Its very useful exp

View Tutorial          By: dipali at 2012-12-31 06:49:41

9. Great tutorial. Working absolutely fine. I needed
View Tutorial          By: Ashwin at 2013-02-28 21:45:29

10. excellent, thanks a lot..............
View Tutorial          By: yellareddy at 2013-03-12 06:56:12