• Post Reply Bookmark Topic Watch Topic
  • New Topic

firing events  RSS feed

 
Jessid Leon Velez Gutierrez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. It seems I ask very strange questions (or at least it seems to be like that) and here I am with another one:

How can I, for example detect that a variable has changed, for instance a boolean, without having to ask for it? I mean this: I dont have to be asking for a button if it has been pressed in a crazy never-ending loop.

it is something like this
I have a class, lets call it Reader and it has a method that reads, some input (a serial port) and holds data in a Vector (I dont know if it is correct to do that, but that is not important right now).

From some other class I want to detect that Reader has data in the Vector and it is advisable to process its data. I dont want to have an infinite loop like:
while (true){
ask if the vector has data
}
to avoid eatting the processor.

Some of you could, please help me???

Thanks a lot!!!

Good bye!
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it might be helpful if you study the Observer pattern. Java also offers a set of classes that implements this pattern: java.util.Observer and java.util.Observable.

These classes might help you implement the model-view-controller paradigm that you seem to be looking for.

Another option could be following the Java Beans convention for handling events. In other words make a JavaBean out of your classes, and by doing so, capable of handling and firing events when properties change.

Could this be helpful?
[ December 12, 2006: Message edited by: Edwin Dalorzo ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to discount what Edwin has said, but I think your term "event" here is misleading. For something to be waiting for input, and something else to be collecting input to be provided, you have to have multiple threads involved, yes? At which point it's an inter-thread communication problem.

What you're talking about sounds to me like the classic Producer/Consumer pattern, a very common way for multiple threads to interact. It's covered nicely in the threads chapter of Sun's Java Tutorial.

I don't think you ask weird questions -- you're thinking ahead, and that's a good thing!
 
Jessid Leon Velez Gutierrez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin and Ernest...i am very thankfull. I am going to review what you have told me, and will let you about my progress.
 
Jessid Leon Velez Gutierrez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends. I am writing not to post a question, but to say thanks to Ernest and Edwin who gave me some lights in soving my little problem. The programm I am writing is now working as I wanted to and now I can continue with my app.

I solved it using the Observer Interface and Observable Class in java.util. Wonderful API, isnt it?

Good bye, and certainly, we will be watching us around (I dont know how that sounds in English). In Spanish I would say: Nos estaremos viendo por aqui, jejeje

Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!