• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

advice sampling thread

 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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!
 
Tom Reilly
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks I'll look in to that!

After I looked it up, 'finished' should be indeed volatile.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic