Solved

Objects running threads and Observables

Posted on 2007-03-31
7
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
VB Script to add site to Java Exception List 4 50
couple of eclipse 5 25
Way to decrease size of apk file 9 45
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…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 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

16 Experts available now in Live!

Get 1:1 Help Now