![]() |
Java Threads
So I have experience in programming - but one thing I haven't really looked into in detail are threads - I've always just let the compiler deal with them for me. I understand the concept of threads. The different parts of a programmer running essentially at the same time (alternating fast enough so that it doesn't really make a difference). I just don't understand what parts of a program constitutes as a difference thread. I've written a few additional classes for organisational purposes - do they have any effect?
Basically my main problem is that I need to pulse solenoids. It's simple to set it open or closed based on other input, but I don't know how to use time as an input. I figure I need threads because I don't want to stop everything else in the code. Finally I've both seen the wait() function in the object class and the delay() function in the Timer class - I'm not sure which is better to use and which thread it effects. |
Re: Java Threads
There is only one thread unless you explicitly make another thread. So to answer your question: most likely the same thread as the main thread. Unless the WPILib makes threads (I think the pid calculations do it)
|
Re: Java Threads
Quote:
Code:
package edu.wpi.first.wpilibj.templates;wait() causes the thread to sleep until another thread issues a notify() or notifyAll() command. It doesn't set a specific time to wait for. You might try replacing the sleep() function with Timer.delay(), I think that would get rid of the try..catch statements. Code:
package edu.wpi.first.wpilibj.templates;Code:
Pulser pulser = new Pulser(); |
Re: Java Threads
Quote:
Now if you'd prefer putting it in a thread than a loop, just make a Runnable interface and thread it. Hope I helped. |
Re: Java Threads
Alright, thanks everyone - I think I understand.
By default there is only one main thread. If I want to create a thread I can use the Thread class and can pass it a class inherited from "Runnable." Then no pauses in the run function will distract the main code. To pause the thread I can either use the Timer class or the wait function, but the wait function requires a try...catch function. Thanks for the quick responses. |
Re: Java Threads
What are three ways in which a thread can enter the waiting state?
|
Re: Java Threads
Quote:
|
Re: Java Threads
Quote:
How is Timer.delay() implemented? |
Re: Java Threads
Quote:
Code:
public static void delay(final double seconds) { |
Re: Java Threads
We ran a neat little experiment (at least to me) to understand how the underlying threads were being executed.
Set the motor speed to one value- and then immediately set it to another value. What we ended up with is a 'pulse' - sometimes it would hold continuous values... othertimes it would slow/speed up. I had hoped it would help drive some points in with the programmers. |
Re: Java Threads
Quote:
|
Re: Java Threads
Quote:
If they executed linearly and in order, you'd have either no pulse (since the first set would be overridden when the code exited the loop) or a constant pulse rate since both calls were made in sequence, one after another, with no delays other than what the scheduler created. Which mean the scheduler was pausing the robot.main classes and running things in the background- interrupt, so to speak, the main thread transparently. The students didn't (and still don't, I think) understand the implications of this. They think it just runs and runs and runs... they never see the pauses because they aren't presented to them. One of the biggest issues I've had in trying to teach (and even some adults) is that just because you put two lines next to each other in the code- when dealing with hardware- you never know for certain (unless you're VERY careful) that they will execute properly. So it is always best to design with that thought in mind.... |
Re: Java Threads
Quote:
Someone said it was not time-based, but rather based on number of bytecodes executed. Is this correct, and can anyone link to a document where this is discussed? I'm referring to the 2011 FRC Java Framework implementation here specifically. |
| All times are GMT -5. The time now is 11:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi