Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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!
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic