|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Multitasking in Java
Hi,
For next year we are planning to use threads so the robot can multitask better. Do you think this is a good idea and if so how would you go about doing it thanks. I know you have to create a thread and runnable (like the one below) but where do you put them? Code:
Thread nameOfThread = new Thread();
Runnable trackingJob = new Runnable()
{
//Particle Analysis Report
public void run()
{
//Code here
}
};
|
|
#2
|
||||
|
||||
|
Re: Multitasking in Java
The argument to the Thread constructor takes a Runnable. To start the Thread use the start() method, not run() (If you call run(), it won't be threaded at all).
I usually write a separate class that implements Runnable, then creates and starts a thread in the constructor. Take this simple class for an example: Code:
public class ThreadedHello implements Runnable{
public ThreadedHello(){
new Thread(this).start();
}
public void run(){
System.out.println("Hello World!");
}
}
Code:
ThreadedHello hello = new ThreadedHello(); //if it were a real object I would do something else besides create it |
|
#3
|
|||
|
|||
|
Re: Multitasking in Java
Ok, that makes sense. Then would this (below) be how you would use threads with the simple robot template. Would you still need a while loop or once you start the thread it will keep running and call the Runnable. Thanks.
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
public class RobotTemplate extends SimpleRobot
{
Joystick stickL = new Joystick(1);
Joystick stickR = new Joystick(2);
public void autonomous()
{
}
public void operatorControl()
{
Thread drive = new Thread(driveJob);
drive.start();
}
Runnable driveJob = new Runnable()
{
RobotDrive myDrive = new RobotDrive(1, 2, 3, 4);
public void run()
{
myDrive.tankDrive(-stickL.getAxis(Joystick.AxisType.kY), -stickR.getAxis(Joystick.AxisType.kY));
}
};
}
|
|
#4
|
||||
|
||||
|
Re: Multitasking in Java
If you wanted to use it for something like driving the robot, it would probably be easiest to use the built-in Command classes, but if you have your heart set on using Thread or want to use it for something like socket communication (like I did with our robot), then you would need to have a while(true) loop inside your run method, otherwise it would only run once. If you would like an example of using the Command based model, I would be happy to post one, just let me know.
|
|
#5
|
|||
|
|||
|
Re: Multitasking in Java
Does anyone know exactly where someone could find the threading guarantees we have with the WPILIB library? Can we set motors from separate threads? Can we call I/O functions(both networking and storage) concurrently? Are the constructors for the PWM-like classes free from contention? Etc.
Some of the newer programmers on my team want to look into multithreading next year and I am wondering whether we need to mutex the world in order for it to work well. |
|
#6
|
||||
|
||||
|
Re: Multitasking in Java
Quote:
I have definitely read files and sockets within a Thread and that works fine. |
|
#7
|
|||
|
|||
|
Re: Multitasking in Java
Ok so in each runnable just put while(true). Could you also put while(isTeleop())?
Code:
Runnable driveJob = new Runnable()
{
RobotDrive myDrive = new RobotDrive(1, 2, 3, 4);
public void run()
{
while(true)
{
myDrive.tankDrive(-stickL.getAxis(Joystick.AxisType.kY), -stickR.getAxis(Joystick.AxisType.kY));
}
}
};
Thanks |
|
#8
|
|||
|
|||
|
Re: Multitasking in Java
Quote:
Alternatively, you could try to figure out the Command System which is better for doing the kind of job you've written down (although for UDP stuff, you'll have to use a thread). |
|
#9
|
|||
|
|||
|
Re: Multitasking in Java
Ok thanks. Does it matter how many milliseconds you put in for Thread.sleep(...). Could it be really small? Also would you mind posting an example of using the Command classes. Thanks.
|
|
#10
|
||||
|
||||
|
Re: Multitasking in Java
Quote:
It should be as large as acceptable, and only as small as necessary, for the specific purpose of the thread. |
|
#11
|
||||
|
||||
|
Re: Multitasking in Java
Regarding UDP, I do not believe that it is supported on the cRIO in Java (as of last year at least). The classes exist for it, but when I tried to run my UDP code, it said that datagram sockets were not supported.
|
|
#12
|
|||
|
|||
|
Re: Multitasking in Java
Quote:
|
|
#13
|
|||
|
|||
|
Re: Multitasking in Java
The "PIDcontroller" class creates a thread to run its calculations.
It's code looks like this (extra stuff removed so that the thread code remains): Code:
java.util.Timer controlLoop;
...
private class PIDTask extends TimerTask {
...
public void run() {
m_controller.calculate();
}
}
public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput[] output, double iperiod) {
controlLoop = new java.util.Timer();
...
controlLoop.schedule(new PIDController.PIDTask(this), 0L, (long) (period * 1000)); //starts thread
}
|
|
#14
|
|||
|
|||
|
Re: Multitasking in Java
Quote:
When the user's code runs the Scheduler's run() method, the scheduler iterates through a list of active commands and calls execute() on each one (sequentially, not parallel). So there's no issues with race conditions or anything complicated like that with the new command-based system, since there's no threads. I thought commands were useful because we could easily make the robot perform actions by binding button presses to Commands. It also made it easy for our robot to sequentially fire balls during autonomous just by scheduling three of our "shoot ball" Commands to run using a CommandGroup. Last edited by Slix : 09-06-2012 at 21:01. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|