• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

advice sampling thread

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

situation:
I'm currently working with some fellow students on a "school" project involving Lego Mindstorms. We're using Lejos to run java programs on the Lego robot.

Through Libraries we have an object 'sensor' that updates a variable every 20 ms. I can read this variable using the method 'readValue()'. I've written the following thread to sample this sensor at 20ms:



problem
Our mentor however, isn't happy with the "Thread.sleep(20)" method we're using to achieve the sampling effect (because of the inaccuracy of the sleep method). He hinted that there was a better way to do this. Could you suggest some other better method or at least give me a hint in which direction to search? I'm very ill experienced in Threads.

Thanks in advance for your advice!
 
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using some kind of Timer or StopWatch class?

BTW, you should declare your finished variable as volatile.
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I'll look in to that!

After I looked it up, 'finished' should be indeed volatile.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given your mentor's concern over the accuracy of the sleep timer, I am not sure the Timer, or a stop watch would be any better. Does the leJos tutorial provide an alternate to polling?

[hint] yes ;-) [/hint]
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve, that was exactly what I'm looking for! I was searching in the wrong direction, it has nothing to do with the threat architecture, just a simple Listener suffices.

Moral of this Topic:
Read the damn documentation!

Again thanks!
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic