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. How to download more than one files. If I use for
View Tutorial          By: Riyas at 2012-01-08 07:55:03

2. this code should be commented I mean a lot of comm
View Tutorial          By: mmmm at 2012-03-31 00:44:41

3. thanks a lot it was really helpful...........for d
View Tutorial          By: Joshi Rocky at 2010-01-29 14:13:40

4. the below error can be solve by another version co
View Tutorial          By: prasad raju v at 2010-04-08 04:20:10

5. i have a some knowledge of java . I want some exam
View Tutorial          By: kavitha at 2012-07-30 09:07:47

6. It's a good eg to learn join and isAlive method.Bu
View Tutorial          By: Nithin at 2012-02-02 08:43:28

7. Your code will not execute using above main method
View Tutorial          By: Gajanan Patil at 2012-05-09 04:42:29

8. public class Overload2 {


View Tutorial          By: ITMIT at 2011-06-05 19:16:46

9. Nice article..
View Tutorial          By: senthil at 2009-09-09 00:22:40

10. can I ask question? how to make a complete program
View Tutorial          By: manuel at 2012-08-13 10:06:24