| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Event handling and Java Technology
Description :
Event handling and Java Technology More...
|
Written By : admin
Title :
Using Final Keyword
Description :
Example on using Final Keyword in Java code More...
|
Written By : Amit
Title :
In memory AWT Image Creation
Description :
Java Code More...
|
Written By : Amit
Title :
Java Integral Types
Description :
Java Types with Bit size More...
|
Written By : Amit
Title :
Search Class in JAR
Description :
Using Java Technology More...
|
Written By : Amit
Title :
Move component on Java Applet
Description :
Drag and Drop More...
|
Written By : Amit
Title :
ArrayBlockingQueue and Queue
Description :
Using Java Technology More...
|
Written By : Amit
Title :
Auto generation of code using Java
Description :
Castor XSD More...
|
| Tags/Keywords : Thread example, Java, Java Thread Example,wait, notify, notifyAll, Using wait and notify methods example Author : Amit Date (Year/Month/Date): 2010-05-02
Java Thread Example and using wait and notify method. | |
Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.
In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.
Example using synchronized block and using wait and notify
methods on the synchronized object, in order to have a
very predictable sequence of operation performed on a
chared object instance in two separate runnable instance
in two separate thread objects.
In this example there are two runnable class files, Getter
and Setter.
Getter and Setter are designed to run within two separate
Threads and Setter is expected to retrieve the value from
the shared object "TableDrawer".
TableDrawer while returning the internally stored value
removes the value from the internal data holder.
It means TableDrawer uses the given value/counter to it,
as the key and value in a HashMap object.
For me interesting code is to use wait and notify methods
on the synchronized object inside the synchronized block
within the run methods both Setter and Getter object
instances.
Idea is to make the Setter thread wait after setting the
value into the commond shared object TableDrawer, and
then the Getter thread retrieves the value and notify
other waiting thread to start processing, so thie way
we can have a sequential setter and getter operations
one after another in a very expected way.
as shown in the image below:
In order to see the consequences by removing/commenting
wait and notify method calls in setter and getter run
methods, we an see these setter and getter operations
getting executes at an uneven way as shown below:
Now can we see and walkthrough the source code for this
example.
Setter.java
package example;
public class Setter implements Runnable {
private TableDrawer tableDrawer;
public Setter(TableDrawer td) {
tableDrawer = td;
}
public void run() {
int i=0;
while(i<1000) {
synchronized(tableDrawer) {
tableDrawer.setValue(i);
System.out.println("Setter set value: >>>> "+i);
try
{
//Makes this Thread to wait and release the lock associated
//with this shared TableDrawer object within the synchronized
//block
tableDrawer.wait();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
i++;
}
}
}
| |
Getter.java
package example;
public class Getter implements Runnable {
private TableDrawer tableDrawer;
public Getter(TableDrawer td) {
tableDrawer = td;
}
public void run() {
int i=0;
while(i<1000) {
synchronized(tableDrawer) {
Object value = tableDrawer.getValue(i);
if(value == null)
i--;
else {
System.out.println("Getter got value:---- "+value);
//Notifies all the Thread to wakeup and move to
//runnable state by invoking notify method of the
//shared synchronized instance of the TableDrawer.
tableDrawer.notify();
}
}
i++;
}
}
}
| |
TableDrawer.java
package example;
import java.util.Map;
import java.util.HashMap;
public class TableDrawer {
private Map store = new HashMap();
public void setValue(int v) {
store.put(new Integer(v),new Integer(v));
}
public Object getValue(int v) {
return store.remove(new Integer(v));
}
}
| |
TestClient.java
package example;
public class TestClient {
public static void main(String args[]) {
TableDrawer td = new TableDrawer();
Thread t1 = new Thread(new Setter(td));
Thread t2 = new Thread(new Getter(td));
t1.start();
t2.start();
}
}
| |
This TestClient instantiates two threads and attaches
Setter and Getter runnable code within these Threads
and of course passes the same insatnce of the TableDrawer
class.
After executing these example code I could understand that
wait and notify, notifyAll methods can only be executed for
an instance within the synchronized instance of that object.
Any more example code on this very aspect would be highly
appreciated. |
|
|
|
| Home >>> Coding Tips on Java >>> Java Thread Example >>> Using Wait Notify |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
| |  |
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners
This web site's Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.
By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.
|  |
|
|
|
|
|