Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Correct use of threads?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am inexperienced with the use of threads, and I just wanted some advice as to whether I should use threads or not in the following situation:

My application records the position of the mouse every time it is moved on the interface.
Each time the MouseMoved method is called, I note the co-ordinates of the mouse, get the currentTimeMillis(), a couple of other things, and do some calculations.
The more things I do at this stage, (ie. the more code to be executed in the mouseMoved method) the less often the mouse position is recorded; thus I have less data to work with.

Basically I want the mouseMoved method to run as quickly as possible, so I capture as many mouse positions as possible.

Would it be a good idea, (or possible even!!?) to capture the mouse co-ordinates with one thread while another concurrently gets the time, and another carries out the computation based on the information captured by the other two threads?
-Is it possible to pass values from one thread to another?

Any thoughs on this would be greatly appreciated,
Thanks in advance,

Eoin.
[ February 23, 2007: Message edited by: Eoin Mac Aoidh ]
 
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Would it be a good idea, (or possible even!!?) to capture the mouse co-ordinates with one thread while another concurrently gets the time, and another carries out the computation based on the information captured by the other two threads?



To have a separate thread get the system time, is a bad idea. That operation is so fast, it should be done by the event dispatching (mouse) thread. Just have the mouse event store the coordinates along with the time, in some sort of queue for processing by another thread.

As for having another thread do the computation, it would depend on the computation. If it *really* takes a long time, then it may be worth it to have a separate thread. It takes time to start another thread, or to dispatch the work to another thread, so it would be silly to do it for really fast computations.

What is deemed fast? What is *really* takes a long time? I am not sure. There is a huge gray area where you can't be sure until you try it.

Henry
 
Eoin Mac Aoidh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just have the mouse event store the coordinates along with the time, in some sort of queue for processing by another thread.


Thank you Henry - That is very helpful advice. I will give it a try.
Much appreciated.

E.
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!