Solved

Objects running threads and Observables

Posted on 2007-03-31
7
257 Views
Last Modified: 2013-11-12
Hi:


Here is my problem:

1- I have Class Calcultae.java --> performs some calculations and store them in local variables with getValue method for each of the values.  Those values changes few times in each sec.

2- I have many classes( call them X,Y,Z) --> those classes will need to call the prorate getValue methods once the value is updated.


I  need to make each of  X,Y,Z run in a separate thread.
---------------------------------------------
I used Observable/Observer for the notification part.  So when Calculate updates it’s values it calls the update method in each of X,Y,Z.

So fare so good.

However, I have problem running the threads.  The thread runs the code with in the run() method only.  And  code needs to listen to the call from Calculate.java to perform the job.
----------------------------------------
If you going to suggest wait/notify , I am going to need an example.  I have tried searching about a way to use it in my case, but I could no.


Note:  X,Y,Z are different objects
0
Comment
Question by:hamadqtr
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18831244
>> I used Observable/Observer for the notification part

They already use the notify () method - so have you not implemented that already? If so, where did the run () come from and why do you need another example of notify () ?
0
 

Author Comment

by:hamadqtr
ID: 18832380
Each of X,Y,Z needs to run in a saperate thread
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832445
Create them as threads and run them:

public class X implements Runnable
{
  private Thread t ;
  public X ()
  {
    t = new Thread ( this ) ;
    t.start () ;
  }
  public void run ()
  {
    // implementation
  }
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:hamadqtr
ID: 18832735
I know this part, but my problem is the each of the X<Y<X implments Observer too

that is each will have to implment the upday() method.

Where the update() method contains the code to be run after receiving a signal from the Observable object (in my case the Calculate class)


thanks
0
 
LVL 3

Accepted Solution

by:
cavey_79 earned 500 total points
ID: 18837079
In the run method of the X,Y,Z you would do a this.wait and the update method would do this.notify. That would wake up the X,Y,Z to do whatever stuff they need to do. Don't forget, wait and notify calls need to be performed inside synchronized blocks on the object the method is callled on

sample:
run:
synchronized(this){this.wait();}

update:
synchronized(this){this.notify();}

You'll need to catch some exceptions, but you get the point.
0
 

Author Comment

by:hamadqtr
ID: 18839571
Why synchronized

what does it do
0
 
LVL 3

Expert Comment

by:cavey_79
ID: 18839629
synchronized is required in order to call wait and notify methods...I'm probably not the best explainer, so this page should be exactly what you need:
http://www.jchq.net/tutorial/07_03Tut.htm

wait and notify methods act on an object's monitor - but you have to have that monitor before you can call them. A monitor is basically a kind of token which allows you to do crazy stuff with the object :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 11 50
configure dependency in POM for new database 3 34
maven module vs maven project 3 48
passing enum to a method 4 25
What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question