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. hi, i would like to know about sms sendimg via web
View Tutorial          By: abhi at 2008-07-09 07:28:54

2. This is a nice introduction. Thanks a lot.
View Tutorial          By: Provas Kumar Saha at 2010-04-26 02:38:34

3. but need in the case of radio buttons
View Tutorial          By: vishnu at 2011-07-21 11:39:26

4. The same article is originally given on struts apa
View Tutorial          By: Fact at 2011-05-09 16:19:18

5. Hi Everyone, i tried the code. im using Micromax
View Tutorial          By: adm at 2011-09-06 12:31:18

6. thanks emily, this was fantastic... just saved me
View Tutorial          By: philip at 2010-04-10 20:39:08

7. i am newly in java, i want to learn about this and
View Tutorial          By: punitha at 2012-07-21 06:16:53

8. this article is very good for the beginners. I am
View Tutorial          By: manoj kumar palbabu at 2009-12-02 10:44:04

9. Hi,
I wann to insert data into excel file b

View Tutorial          By: PRUTHVIRAJ RATHOD at 2011-05-23 11:42:14

10. great explanations.. some important words are miss
View Tutorial          By: Joshua Paul at 2015-04-14 03:43:44