Solved

Objects running threads and Observables

Posted on 2007-03-31
7
242 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:mayankeagle
Comment Utility
>> 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
Comment Utility
Each of X,Y,Z needs to run in a saperate thread
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:hamadqtr
Comment Utility
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
Comment Utility
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
Comment Utility
Why synchronized

what does it do
0
 
LVL 3

Expert Comment

by:cavey_79
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now