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. I want to know about all java application.
View Tutorial          By: Ahasan at 2009-04-10 09:42:48

2. more info needs to be given ............
View Tutorial          By: sufficient for new commer at 2008-05-09 00:41:58

3. Hi Steve,

thanks for your code. It

View Tutorial          By: Johnny at 2010-10-29 09:25:41

4. i am new to SMS, i think i need a device too. Whic
View Tutorial          By: Usman at 2015-10-05 12:06:34

5. I am using my samsung s5233 via usb connection, ru
View Tutorial          By: Puneet at 2010-04-23 06:14:28

6. Your Crystal report viewer showing all the records
View Tutorial          By: Neel at 2013-06-09 15:02:15

7. http://www.java-samples.com/showtutorial.php?tutor
View Tutorial          By: Prab at 2009-04-13 11:54:17

8. thank u very much...............
View Tutorial          By: naveen at 2011-07-05 06:00:04

9. hi
Can you anyone please post me the entir

View Tutorial          By: yasmeen at 2009-05-25 02:42:44

10. abstract class A {
abstract void callme()

View Tutorial          By: satish at 2012-04-07 10:35:38