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. Thanks. Well explanation..
View Tutorial          By: Ebi Divakar at 2011-07-26 04:31:30

2. Thanks for the Information. Its very useful
View Tutorial          By: Husain at 2009-03-04 13:30:38

3. help to place a menu inside a menu
View Tutorial          By: sandy at 2009-07-29 05:57:28

4. Excellent information
View Tutorial          By: mahesh at 2014-10-18 06:07:35

5. ma'am aiza teaches us very well.. >< i like
View Tutorial          By: chai at 2011-02-07 07:06:36

6. hi,

i'm a jmeter user.in my test i

View Tutorial          By: mahesh at 2015-03-06 16:58:05

7. Thanks a lot for sharing this solution.
View Tutorial          By: Bharath at 2012-12-18 18:32:33

8. HI,

The function is throwing an err

View Tutorial          By: Vamshi at 2010-08-31 05:04:03

9. Hello,

I've got javax.comm and fol

View Tutorial          By: Ashkan at 2009-12-19 21:51:19

10. i am MCA student and i have completed bca, so can
View Tutorial          By: shilam kumari at 2014-01-12 11:08:05