Solved

Objects running threads and Observables

Posted on 2007-03-31
7
274 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

627 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