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 am developing a code for data transfer from
View Tutorial          By: Rajavel at 2014-03-21 14:17:54

2. hai,
i want to know how the connect

View Tutorial          By: V.Selvi at 2010-02-28 21:01:33

3. Looks good, but don't use gets(). gets() is respon
View Tutorial          By: Luke at 2011-11-21 09:03:38

4. The DirSync sync intervals can be changed.

View Tutorial          By: Nitin at 2012-12-22 16:20:38

5. Helps very much. Thank you.
View Tutorial          By: Uwe at 2010-08-17 12:35:44

6. Okay, I have implemented this and it works, except
View Tutorial          By: Matthew at 2009-06-16 13:08:48

7. nice 1
View Tutorial          By: vivek at 2013-03-19 16:25:47

8. import java.io.*;
class lastindexof

View Tutorial          By: arya at 2012-11-07 08:37:59

9. i want to know how to read a file inside zipfile.<
View Tutorial          By: iman at 2009-11-26 12:50:21

10. Hi ! do u know have use FileOutputStream in Surfac
View Tutorial          By: lukas at 2011-07-18 16:55:20