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. sir,
my project is in asp.net so is ther

View Tutorial          By: mohsin at 2011-04-14 06:25:59

2. How to sort an Arraylist in some given order? Say,
View Tutorial          By: Arunava Chatterjee at 2015-03-07 04:17:51

3. thanks! it helped!
View Tutorial          By: shgy at 2015-03-17 07:42:40

4. concept of static keyword is beautifully explained
View Tutorial          By: jaideep aswal at 2012-04-14 07:21:56

5. Very helpful and easily understandable!
View Tutorial          By: Teerna at 2014-11-12 23:55:49

6. This website is great for the students who want to
View Tutorial          By: Parth Patalia at 2009-11-19 21:09:24

7. Whe I compiled the above code, it sends me to the
View Tutorial          By: Koo Larbi at 2014-08-02 22:32:05

8. that is fifo rigth? how about lifo?
View Tutorial          By: galz at 2015-02-18 03:03:31

9. Hi Jim, to get the code for this clock, right clic
View Tutorial          By: Rajan at 2008-04-11 08:25:03

10. nice... thanks for the explain, you safe my day...
View Tutorial          By: Joseph Louis at 2011-05-11 08:53:53