|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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)
|
|
#3
|
|||
|
|||
|
Re: Java Threads
Quote:
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Solenoid;
public class Pulser implements Runnable {
Solenoid firstSolenoid = new Solenoid(1);
public void run() {
while (true) {
try {
firstSolenoid.set(true);
Thread.sleep(1000); // sleep thread for 1 second
firstSolenoid.set(false);
Thread.sleep(1000); // sleep thread for another second
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
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;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
public class Pulser implements Runnable {
Solenoid firstSolenoid = new Solenoid(1);
public void run() {
while (true) {
firstSolenoid.set(true);
Timer.delay(1);
firstSolenoid.set(false);
Timer.delay(1);
}
}
}
Code:
Pulser pulser = new Pulser(); new Thread(pulser).start(); Last edited by wdell : 22-02-2011 at 20:51. |
|
#4
|
|||||
|
|||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
Re: Java Threads
What are three ways in which a thread can enter the waiting state?
|
|
#7
|
||||
|
||||
|
Re: Java Threads
Quote:
|
|
#8
|
||||
|
||||
|
Re: Java Threads
Quote:
How is Timer.delay() implemented? |
|
#9
|
|||
|
|||
|
Re: Java Threads
Quote:
Code:
public static void delay(final double seconds) {
try {
Thread.sleep((long) (seconds * 1e3));
} catch (final InterruptedException e) {
}
}
|
|
#10
|
|||
|
|||
|
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. |
|
#11
|
||||
|
||||
|
Re: Java Threads
Quote:
|
|
#12
|
|||
|
|||
|
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.... |
|
#13
|
||||
|
||||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|