Go to Post And if you are unhappy about me using "robot", let me break your heart over what we've done to the word "cheesecake". - Foster [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 02-10-2014, 03:52 PM
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 02-10-2014, 04:31 PM
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 02-11-2014, 09:09 AM
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 02-11-2014, 09:40 AM
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 02-11-2014, 10:19 AM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,999
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 02-11-2014, 10:47 AM
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: 325
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 02-11-2014, 11:02 AM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,999
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 02-11-2014, 11:08 AM
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
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,712
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 02-11-2014, 11:22 AM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,999
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 02-11-2014, 11:28 AM
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
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,712
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 02-11-2014, 12:21 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,999
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 : 02-11-2014 at 12:29 PM.
Reply With Quote
  #12   Spotlight this post!  
Unread 02-12-2014, 12:22 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,999
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


Thank you everyone for the responses. I've been carefully reviewing them all and I think I now have a reasonably coherent idea of how command-based works.

There are however a few seeming inconsistencies (or perhaps ambiguities) that I'd like to resolve. Here are two:

Firstly, it is necessary but not sufficient that each individual command in the scheduler's queue complete within 20ms. Rather, the sum total of the individual execution times must be less than 20ms. Correct?

Secondly, the scheduler maintains not only a queue of all the commands to be run, but also the state of each command... and only the method corresponding to each command's state is executed. Correct? (Compare post 15 to post 17 for example of apparent inconsistency).


Reply With Quote
  #13   Spotlight this post!  
Unread 02-12-2014, 12:49 PM
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,547
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
Firstly, it is necessary but not sufficient that each individual command in the scheduler's queue complete within 20ms. Rather, the sum total of the individual execution times must be less than 20ms. Correct?
Correct

Quote:
Originally Posted by Ether View Post
Secondly, the scheduler maintains not only a queue of all the commands to be run, but also the state of each command... and only the method corresponding to each command's state is executed. Correct? (Compare post 15 to post 17 for example of apparent inconsistency).
This gets a little bit tricky because of the abstraction. The users creates a "Command" that extends the Command class, which is an abtract class. The Command class implements the state machine, but not the initialize, execute, isFinished, end, and interrupted methods. The user's "Command" implements the initialize, execute, isFinished, end, and interrupted methods. It would be possible for the user to override other methods of the state machine, but I have not seen anyone do it. The scheduler calls the run method of the Command class, which increments through the state machine in the way that notmattlythgoe described. To the casual user, it would appear that the scheduler is doing it because the user doesn't implement any code for the state machine.
Reply With Quote
  #14   Spotlight this post!  
Unread 02-12-2014, 12:52 PM
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
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,712
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

Thank you everyone for the responses. I've been carefully reviewing them all and I think I now have a reasonably coherent idea of how command-based works.

There are however a few seeming inconsistencies (or perhaps ambiguities) that I'd like to resolve. Here are two:

Firstly, it is necessary but not sufficient that each individual command in the scheduler's queue complete within 20ms. Rather, the sum total of the individual execution times must be less than 20ms. Correct?

Secondly, the scheduler maintains not only a queue of all the commands to be run, but also the state of each command... and only the method corresponding to each command's state is executed. Correct? (Compare post 15 to post 17 for example of apparent inconsistency).


Yes to your first point. The goal would be that the sum total of all of the commands be less than the 20ms refresh rate. In general, commands should be pretty simple in their implementation to help with this.

The second point is also correct. The only real state that the scheduler needs to keep track of is which commands are running for their first time. The only thing I'd like to clarify further is that once a command is finished it is removed from the scheduler.
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 09:02 AM.

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