Go to Post Competitions are won or lost far away from witnesses, ... In those long hours, is where we find inspiration in ourselves. For what inspires is not another's feats, but realize we have the power in ourselves to achieve that feat. - Mark Sheridan [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-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 10-02-2014, 15:52
1757 1757 is offline
Registered User
FRC #1757
 
Join Date: Oct 2012
Location: Westwood
Posts: 27
1757 is an unknown quantity at this point
Multithreading on Java

I understand that there's a Task class in C++, but how do I multithread in Java? I can't find any Java counterpart to the C++ class. Thank you
Reply With Quote
  #2   Spotlight this post!  
Unread 10-02-2014, 16:31
BornaE's Avatar
BornaE BornaE is offline
Registered User
FRC #0842 (Formerly 39)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Gilbert, Arizona
Posts: 359
BornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant futureBornaE has a brilliant future
Re: Multithreading on Java

http://docs.oracle.com/javase/tutori...runthread.html
__________________
-Borna Emami
Team 0x27
Reply With Quote
  #3   Spotlight this post!  
Unread 11-02-2014, 09:09
1757 1757 is offline
Registered User
FRC #1757
 
Join Date: Oct 2012
Location: Westwood
Posts: 27
1757 is an unknown quantity at this point
Re: Multithreading on Java

okay thanks, I didn't think to try that because it's not native to the cRIO
Reply With Quote
  #4   Spotlight this post!  
Unread 11-02-2014, 09:40
jwakeman jwakeman is offline
Registered User
FRC #0063 (Red Barons)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: 16510
Posts: 182
jwakeman is just really nicejwakeman is just really nicejwakeman is just really nicejwakeman is just really nicejwakeman is just really nice
Re: Multithreading on Java

The Command Based programming model available through the WPILib is a good way to achieve concurrency in your software.

http://wpilib.screenstepslive.com/s/...ed-programming
Reply With Quote
  #5   Spotlight this post!  
Unread 11-02-2014, 10:19
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Multithreading on Java

Quote:
Originally Posted by jwakeman View Post
The Command Based programming model available through the WPILib is a good way to achieve concurrency in your software.

http://wpilib.screenstepslive.com/s/...ed-programming
Quoting from the linked document:

Quote:
Notice that the apparent concurrent execution is done without the use of threads or tasks
... so how does that work, exactly?


Reply With Quote
  #6   Spotlight this post!  
Unread 11-02-2014, 10:47
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 332
fovea1959 will become famous soon enough
Re: Multithreading on Java

the Scheduler when using command based maintains it's own list of active commands, and (when a DS packet arrives) calls their execute() and isFinished() methods for every command on the list (sequentially).

No preemptive scheduling here, everyone's execute() and isFinished() has to play nice....
Reply With Quote
  #7   Spotlight this post!  
Unread 11-02-2014, 11:02
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Multithreading on Java

Quote:
Originally Posted by fovea1959 View Post
No preemptive scheduling here, everyone's execute() and isFinished() has to play nice....
So it's still up to the programmer to use something like a state machine or perhaps yield() statements to achieve this? Or am I misunderstanding this?


Reply With Quote
  #8   Spotlight this post!  
Unread 11-02-2014, 11:08
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is online now
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Multithreading on Java

Quote:
Originally Posted by Ether View Post
So it's still up to the programmer to use something like a state machine or perhaps yield() statements to achieve this? Or am I misunderstanding this?


Each command needs to finish its execute command quickly, any waits or long executions will cause everything else to come to a halt until it is finished.
Reply With Quote
  #9   Spotlight this post!  
Unread 11-02-2014, 11:22
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Multithreading on Java

Quote:
Originally Posted by notmattlythgoe View Post
Each command needs to finish its execute command quickly, any waits or long executions will cause everything else to come to a halt until it is finished.
So how does the command based programming model help the novice programmer achieve concurrency, like for example when they want to wait for 3 seconds after performing one action before performing the next action, without stalling their processing of the 20ms DS packets ?

Please note: this is not veiled criticism of the command based model; I'm just trying to untangle things.


Reply With Quote
  #10   Spotlight this post!  
Unread 11-02-2014, 11:28
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is online now
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Multithreading on Java

Quote:
Originally Posted by Ether View Post
So how does the command based programming model help the novice programmer achieve concurrency, like for example when they want to wait for 3 seconds after performing one action before performing the next action, without stalling their processing of the 20ms DS packets ?

Please note: this is not veiled criticism of the command based model; I'm just trying to untangle things.


You can create something called a CommandGroup which will run one command after another in a series (much like a series in labview), the Command Based API has a WaitCommand provided which will act as a wait period in seconds. It can also be done in a normal command by noting the initial time the command was started and basing actions off of the time since the start time.
Reply With Quote
  #11   Spotlight this post!  
Unread 11-02-2014, 12:21
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Multithreading on Java

Quote:
Originally Posted by notmattlythgoe View Post
You can create something called a CommandGroup which will run one command after another in a series (much like a series in labview), the Command Based API has a WaitCommand provided which will act as a wait period in seconds.
The way you worded that doesn't clearly answer my question.

Let me try re-wording it:

You can create something called a CommandGroup which contains a sequence of commands, intended to be executed one after another. The scheduler will queue the first of those commands, then queue the next command when the previous one has completed, and so on until all the commands in the group have been so queued (and therefore run in sequence). The Command Based API has a WaitCommand. The WaitCommand is not queued. Rather, when the scheduler encounters a WaitCommand in a CommandGroup, the scheduler does not queue the command following the WaitCommand until the specified time has expired.

I have no idea if the above description is an accurate account of what the Command Based implementation actually does, but is that what you meant?


Quote:
It can also be done in a normal command by noting the initial time the command was started and basing actions off of the time since the start time.
That's a sort of state machine, with time being the state variable.



Last edited by Ether : 11-02-2014 at 12:29.
Reply With Quote
  #12   Spotlight this post!  
Unread 11-02-2014, 12:30
BigJ BigJ is offline
Registered User
AKA: Josh P.
FRC #1675 (Ultimate Protection Squad)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Milwaukee, WI
Posts: 947
BigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond repute
Re: Multithreading on Java

Commands can be running "simultaneously" as long as they don't require writing data to the same Subsystems. CommandGroups are used mainly for automation (ex. I might have a ShootAndReload command group that releases my winch, then proceeds to roll it back up again immediately by calling the different Commands in a certain configuration). You can add Commands to a CommandGroup to run in sequence or parallel.
Reply With Quote
  #13   Spotlight this post!  
Unread 11-02-2014, 12:51
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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: Multithreading on Java

Quote:
Originally Posted by BigJ View Post
Commands can be running "simultaneously" as long as they don't require writing data to the same Subsystems.
That statement taken at face value would seem to allow the following:
Command A requires only Subsystem B and takes 1ms to complete.

Command X requires only Subsystem Y and takes 3 seconds to complete

Since Command A and Command X don't require writing data to the same Subsystems, they can be running simultaneously
My question is, how does the CommandBased model implement this, if not by preemptive time-slicing.

Posts 6 and 8, and post 10 if I interpreted it correctly, would answer that question.


Reply With Quote
  #14   Spotlight this post!  
Unread 11-02-2014, 13:01
BigJ BigJ is offline
Registered User
AKA: Josh P.
FRC #1675 (Ultimate Protection Squad)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Milwaukee, WI
Posts: 947
BigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond repute
Re: Multithreading on Java

They aren't actually running simultaneously. I'm not sure if I'm interpreting "preemptive time slicing" correctly, but any Command-Based program could be written in the IterativeRobot style with a large number of if statements . They're all running on one thread as I understand it, and I am not sure you have any guarantee to the order they run in. That is why Commands can declare that they require exclusive access to a Subsystem to tell other Commands that they will be writing data to it.

Both commands will run the execute() method in their class each robot loop (usually 20ms) until their isFinished() check (also run each loop) returns true. The Scheduler takes care of all the behind-the-scenes work of maintaining command life-cycles.
Reply With Quote
  #15   Spotlight this post!  
Unread 11-02-2014, 13:10
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Multithreading on Java

Quote:
Originally Posted by Ether View Post
That's a sort of state machine, with time being the state variable.
A command is a state machine. The scheduler handles transitioning between states. The states are listed below:

initialize
execute
end
interrupted

The user provides the code that goes in those methods. There is a fifth method (isFinished()) that allows the scheduler to know when to transition from execute to end.

The normal flow is from initialize to execute to end. The scheduler handles interrupting, if a new command is scheduled that requires the same subsystem.

A CommandGroup allows multiple commands to be scheduled sequentially or in parallel (being in parallel requires that they don't require the same subsystem).

The only times we have had to add an explicit state machine inside a command, is when there is decision logic that needs to go backwards in states. In our case, that's been relatively rare.

The scheduler is entirely cooperative. It depends on the commands methods executing quickly and returning. If the user puts an explicit Timer.wait() in a command, it breaks the execution model. The user can either use a WaitCommand, or check a timer in the isFinished method, or implement their own state machine. We've done all 3.
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 13:35.

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


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