Solved

Objects running threads and Observables

Posted on 2007-03-31
7
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

710 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