OCCRA
Go to Post Don't waste the opportunity because you were too focused on "winning" the championship. - Don Wright [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-22-2011, 07:35 PM
Aaron V Aaron V is offline
Registered User
FRC #1325 (Inverse Paradox)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Ontario, Canada
Posts: 13
Aaron V is an unknown quantity at this point
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.
Reply With Quote
  #2   Spotlight this post!  
Unread 02-22-2011, 07:38 PM
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
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)
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.
Reply With Quote
  #3   Spotlight this post!  
Unread 02-22-2011, 08:46 PM
wdell wdell is offline
Registered User
AKA: William Dell
FRC #3999 (Shadetree Mechanics)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Killeen, Texas
Posts: 55
wdell has a spectacular aura aboutwdell has a spectacular aura about
Re: Java Threads

Quote:
Originally Posted by Aaron V View Post
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.
How often are you trying to pulse the solenoids?

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();
            }
        }
    }
}
That will pulse the solenoid on for a second, then off for a second. The sleep() function causes the thread to wait (time in milliseconds).

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);

        }
    }
}
To start the thread, you need to tell it to start() somewhere in your main program:

Code:
Pulser pulser = new Pulser();
new Thread(pulser).start();

Last edited by wdell : 02-22-2011 at 08:51 PM.
Reply With Quote
  #4   Spotlight this post!  
Unread 02-22-2011, 08:47 PM
Robby Unruh's Avatar
Robby Unruh Robby Unruh is offline
beep beep beep beep...
FRC #3266 (Robots R Us)
Team Role: Coach
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Eaton, OH
Posts: 328
Robby Unruh will become famous soon enough
Send a message via Skype™ to Robby Unruh
Re: Java Threads

Quote:
Originally Posted by Aaron V View Post
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.
Pulse solenoids, eh? Take a closer look at the Timer class, more notably getFPGATimestamp(). What I'd do is create a double to store a current timestamp using getFPGATimestamp(). Then in my code I'd make another more recent one that checks for a change in time. If the time has changed, I'd simply set the solenoid to false. Put that baby in a loop and bam!

Now if you'd prefer putting it in a thread than a loop, just make a Runnable interface and thread it.

Hope I helped.
__________________
[Robots R Us #3266]
2014: Crossroads, Queen City, Ohio State Champs, CORI, MVRC, Battle for the Bluegrass
2013: Buckeye, Queen City, Crossroads, CORI, Ohio State Champs, CAGE Match
2012: Buckeye, Queen City, CAGE Match, BPRC
2011: Buckeye, CORI, CAGE Match
2010: Buckeye
Reply With Quote
  #5   Spotlight this post!  
Unread 02-22-2011, 09:22 PM
Aaron V Aaron V is offline
Registered User
FRC #1325 (Inverse Paradox)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Ontario, Canada
Posts: 13
Aaron V is an unknown quantity at this point
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.
Reply With Quote
  #6   Spotlight this post!  
Unread 03-13-2011, 04:46 AM
dale5alfred dale5alfred is offline
Registered User
AKA: Dale Alfred
no team
Team Role: CAD
 
Join Date: Mar 2011
Rookie Year: 2009
Location: US
Posts: 1
dale5alfred is an unknown quantity at this point
Send a message via Yahoo to dale5alfred
Re: Java Threads

What are three ways in which a thread can enter the waiting state?
Reply With Quote
  #7   Spotlight this post!  
Unread 03-13-2011, 03:00 PM
Chris27's Avatar
Chris27 Chris27 is offline
Registered User
AKA: Chris Freeman
FRC #1625 (Winnovation)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Mountain View
Posts: 196
Chris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant future
Re: Java Threads

Quote:
Originally Posted by Aaron V View Post
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.
fyi, Runnable is an interface not a class.
Reply With Quote
  #8   Spotlight this post!  
Unread 03-13-2011, 05:06 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 6,192
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Java Threads

Quote:
Originally Posted by wdell View Post
You might try replacing the sleep() function with Timer.delay(), I think that would get rid of the try..catch statements.
Sleep() makes the thread not runnable so it releases the processor so it doesn't waste resources polling the clock. The sleeping thread becomes runnable again when the sleep period has expired.


How is Timer.delay() implemented?


Reply With Quote
  #9   Spotlight this post!  
Unread 03-13-2011, 07:17 PM
colinwarren colinwarren is offline
King of the Code Monkeys
AKA: Colin Warren
FRC #2456 (Recycle It!)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Marin
Posts: 8
colinwarren is an unknown quantity at this point
Re: Java Threads

Quote:
Originally Posted by Ether View Post
Sleep() makes the thread not runnable so it releases the processor so it doesn't waste resources polling the clock. The sleeping thread becomes runnable again when the sleep period has expired.


How is Timer.delay() implemented?

Code:
public static void delay(final double seconds) {
        try {
            Thread.sleep((long) (seconds * 1e3));
        } catch (final InterruptedException e) {
        }
    }
From WPILibJ's source. I would expect that the C++ threads are implemented on top of usleep(3).
__________________
Reply With Quote
  #10   Spotlight this post!  
Unread 03-13-2011, 10:26 PM
purduephotog purduephotog is offline
Active Defense Design Engineer
AKA: Jason
FRC #3015
Team Role: Leadership
 
Join Date: Jan 2007
Rookie Year: 2004
Location: Rochester, NY
Posts: 160
purduephotog is a jewel in the roughpurduephotog is a jewel in the roughpurduephotog is a jewel in the roughpurduephotog is a jewel in the rough
Send a message via AIM to purduephotog
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.
__________________
http://purduephotog.deviantart.com
Portrait Photography: "I used to say Immortalized in Silver, but now I say Captured and Squeezed by Electrons".
Reply With Quote
  #11   Spotlight this post!  
Unread 03-13-2011, 10:43 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 6,192
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Java Threads

Quote:
Originally Posted by purduephotog View Post
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.
How did this help you understand how threads are being executed? What points were you hoping to drive home?


Reply With Quote
  #12   Spotlight this post!  
Unread 03-14-2011, 11:54 AM
purduephotog purduephotog is offline
Active Defense Design Engineer
AKA: Jason
FRC #3015
Team Role: Leadership
 
Join Date: Jan 2007
Rookie Year: 2004
Location: Rochester, NY
Posts: 160
purduephotog is a jewel in the roughpurduephotog is a jewel in the roughpurduephotog is a jewel in the roughpurduephotog is a jewel in the rough
Send a message via AIM to purduephotog
Re: Java Threads

Quote:
Originally Posted by Ether View Post
How did this help you understand how threads are being executed? What points were you hoping to drive home?

Calls were being done asynchronously. That meant they were not executing in a linear fashion and you could not guarantee which would execute first.

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....
__________________
http://purduephotog.deviantart.com
Portrait Photography: "I used to say Immortalized in Silver, but now I say Captured and Squeezed by Electrons".
Reply With Quote
  #13   Spotlight this post!  
Unread 03-22-2011, 07:57 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 6,192
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Java Threads

Quote:
Originally Posted by wdell View Post
To start the thread, you need to tell it to start() somewhere in your main program
If you start several threads at the same priority, what scheduling policy is used to run them concurrently?

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.


Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 04:43 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi