<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=KaiAng</id>
	<title>CSE231 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=KaiAng"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Special:Contributions/KaiAng"/>
	<updated>2026-11-26T23:22:54Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=4576</id>
		<title>Not Thread Safe Hash Table Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=4576"/>
		<updated>2023-02-16T19:50:42Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
We gain experience implementing an abstract data type.  Specifically, we will build a {{NotThreadSafeLink}} class which implements the java.util.Map&amp;lt;K,V&amp;gt; interface.  Later in the semester, we will implement a {{ThreadSafeLink}} version: [[Concurrent_Hash_Table_Assignment|ConcurrentHashTable]].&lt;br /&gt;
&lt;br /&gt;
We gain experience with generics since the Map interface is parameterized with multiple generic types: K for the key type and V for the value type.&lt;br /&gt;
&lt;br /&gt;
We will build some utilities to support hashing and key-value pairs which we will use throughout the semester.&lt;br /&gt;
&lt;br /&gt;
Finally, the selection of methods we will implement includes two which you may not be familiar with: compute() and computeIfAbsent().  Each of these methods have special roles to play throughout the semester.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
== Hash Tables==&lt;br /&gt;
* [https://en.wikipedia.org/wiki/Hash_table Wikipedia Hash Table]&lt;br /&gt;
* [https://visualgo.net/en/hashtable VisuAlgo Hash Table]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|How To Use java.util.Map|&amp;lt;youtube&amp;gt;MwPrMAG4bLg&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|CS50 Shorts: Hash Tables|&amp;lt;youtube&amp;gt;nvzVHwrrub0&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|HackerRank Hash Tables|&amp;lt;youtube&amp;gt;shs0KM3wKv8&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Java Utilities To Use==&lt;br /&gt;
===Map.Entry&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapEntryWithMethodLinks}}&lt;br /&gt;
===Map&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapWithMethodLinks}}&lt;br /&gt;
===Optional&amp;lt;T&amp;gt;===&lt;br /&gt;
{{OptionalWithMethodsLinks}}&lt;br /&gt;
===ToIntFunction&amp;lt;T&amp;gt;===&lt;br /&gt;
{{ToIntFunctionWithMethodLinks}}&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
Each of the example clients operate on the words of the [https://rmc.library.cornell.edu/gettysburg/good_cause/transcript.htm Gettysburg Address].&lt;br /&gt;
&lt;br /&gt;
==PutClient==&lt;br /&gt;
{{Client|PutClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapPutLink}}.&lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with the word.  Since put replaces a previous association, if it exists, the last word starting with a particular letter wins.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|PutClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, String&amp;gt; mapFirstCharacterToLastEncounteredWord = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            mapFirstCharacterToLastEncounteredWord.put(firstCharacterLowerCased, word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    String value = mapFirstCharacterToLastEncounteredWord.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|PutClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: and&lt;br /&gt;
b: by&lt;br /&gt;
c: cause&lt;br /&gt;
d: died&lt;br /&gt;
e: earth&lt;br /&gt;
f: from&lt;br /&gt;
g: government&lt;br /&gt;
h: have&lt;br /&gt;
i: in&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: last&lt;br /&gt;
m: measure&lt;br /&gt;
n: not&lt;br /&gt;
o: of&lt;br /&gt;
p: perish&lt;br /&gt;
q: &lt;br /&gt;
r: resolve&lt;br /&gt;
s: shall&lt;br /&gt;
t: the&lt;br /&gt;
u: under&lt;br /&gt;
v: vain&lt;br /&gt;
w: we&lt;br /&gt;
x: &lt;br /&gt;
y: years&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeIfAbsentClient==&lt;br /&gt;
{{Client|ComputeIfAbsentClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
This client uses repeated calls to {{MapComputeIfAbsentLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with a list.  The first time a letter is encountered the mappingFunction will be invoked which returns the new list to associate with the letter.  Conveniently, whether it has been associated before or the current iteration, the List associated with the letter is returned from computeIfAbsent.  The current word is then added to that list.  Since the returned list is a reference to mutable data there is no need to re-associate the list with the letter.  When the loops are completed, each letter holds a list of all the encountered words (including any duplicates). &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeIfAbsentClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, List&amp;lt;String&amp;gt;&amp;gt; mapFirstCharacterToListOfWords = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            List&amp;lt;String&amp;gt; list = mapFirstCharacterToListOfWords.computeIfAbsent(firstCharacterLowerCased,&lt;br /&gt;
                    (unusedKey) -&amp;gt; {&lt;br /&gt;
                        return new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
                    });&lt;br /&gt;
            list.add(word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    List&amp;lt;String&amp;gt; value = mapFirstCharacterToListOfWords.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeIfAbsentClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: [and, ago, a, and, all, are, are, a, any, and, are, a, a, as, a, altogether, and, a, and, above, add, advanced, a, and]&lt;br /&gt;
b: [brought, battle, But, brave, but, be, be, before, birth, by]&lt;br /&gt;
c: [continent, conceived, created, civil, conceived, can, come, can, can, consecrate, can, consecrated, can, cause]&lt;br /&gt;
d: [dedicated, dedicated, dedicate, do, dedicate, dead, detract, did, dedicated, dedicated, dead, devotion, devotion, dead, died]&lt;br /&gt;
e: [equal, engaged, endure, earth]&lt;br /&gt;
f: [Four, fathers, forth, field, field, final, for, fitting, far, forget, for, fought, far, for, from, for, full, freedom, for, from]&lt;br /&gt;
g: [great, great, gave, ground, great, gave, God, government]&lt;br /&gt;
h: [have, here, hallow, here, have, here, here, here, here, have, here, honored, here, highly, have, have]&lt;br /&gt;
i: [in, in, It, is, in, it, it, It, is, It, is, increased, in]&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: [Liberty, long, lives, live, larger, living, little, long, living, last]&lt;br /&gt;
m: [men, met, might, men, measure]&lt;br /&gt;
n: [new, nation, Now, nation, nation, nation, not, not, not, note, nor, never, nobly, not, nation, new, not]&lt;br /&gt;
o: [our, on, or, on, of, of, our, or, of, of, of]&lt;br /&gt;
p: [proposition, portion, place, proper, poor, power, people, people, people, perish]&lt;br /&gt;
q: &lt;br /&gt;
r: [resting, remember, rather, rather, remaining, resolve]&lt;br /&gt;
s: [score, seven, so, so, should, sense, struggled, say, so, shall, shall, shall]&lt;br /&gt;
t: [this, to, the, that, testing, that, that, to, that, those, their, that, that, that, this, this, The, to, The, they, the, to, to, the, they, thus, to, to, the, task, that, these, take, to, that, they, the, that, that, these, that, this, that, the, the, the, the]&lt;br /&gt;
u: [us, unfinished, us, us, under]&lt;br /&gt;
v: [vain]&lt;br /&gt;
w: [we, war, whether, We, war, We, who, we, we, we, we, who, world, will, what, we, what, work, which, who, we, which, we]&lt;br /&gt;
x: &lt;br /&gt;
y: [years]&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeClient==&lt;br /&gt;
{{Client|ComputeClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapComputeLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by computing the number of times the (lower case version of the) word has been encountered.  Each time compute is called the remappingFunction is passed the key (which is (as is often the case) unused) and the previous value associated with the key, or null if has no association.  The remapping function returns the value to associate with the key.  Since we are counting the occurrences, we add 1 to the previous value, or simply return 1 if that previous value is null.&lt;br /&gt;
&lt;br /&gt;
When the loops are completed, each word holds the number of times it was encountered.. &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; mapWordToCount = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
	for(String word : textSection.words()) {&lt;br /&gt;
		if (word.length() &amp;gt; 0) {&lt;br /&gt;
			String lowerCaseWord = word.toLowerCase();&lt;br /&gt;
			mapWordToCount.compute(lowerCaseWord, (unusedKey, prevValue) -&amp;gt; {&lt;br /&gt;
				if (prevValue != null) {&lt;br /&gt;
					return prevValue + 1;&lt;br /&gt;
				} else {&lt;br /&gt;
					return 1;&lt;br /&gt;
				}&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
System.out.println(toString(mapWordToCount));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;that =&amp;gt; 13&lt;br /&gt;
the =&amp;gt; 11&lt;br /&gt;
we =&amp;gt; 10&lt;br /&gt;
here =&amp;gt; 8&lt;br /&gt;
to =&amp;gt; 8&lt;br /&gt;
a =&amp;gt; 7&lt;br /&gt;
and =&amp;gt; 6&lt;br /&gt;
can =&amp;gt; 5&lt;br /&gt;
have =&amp;gt; 5&lt;br /&gt;
for =&amp;gt; 5&lt;br /&gt;
it =&amp;gt; 5&lt;br /&gt;
not =&amp;gt; 5&lt;br /&gt;
nation =&amp;gt; 5&lt;br /&gt;
of =&amp;gt; 5&lt;br /&gt;
in =&amp;gt; 4&lt;br /&gt;
this =&amp;gt; 4&lt;br /&gt;
dedicated =&amp;gt; 4&lt;br /&gt;
they =&amp;gt; 3&lt;br /&gt;
is =&amp;gt; 3&lt;br /&gt;
are =&amp;gt; 3&lt;br /&gt;
so =&amp;gt; 3&lt;br /&gt;
dead =&amp;gt; 3&lt;br /&gt;
shall =&amp;gt; 3&lt;br /&gt;
great =&amp;gt; 3&lt;br /&gt;
who =&amp;gt; 3&lt;br /&gt;
us =&amp;gt; 3&lt;br /&gt;
people =&amp;gt; 3&lt;br /&gt;
new =&amp;gt; 2&lt;br /&gt;
conceived =&amp;gt; 2&lt;br /&gt;
war =&amp;gt; 2&lt;br /&gt;
rather =&amp;gt; 2&lt;br /&gt;
gave =&amp;gt; 2&lt;br /&gt;
but =&amp;gt; 2&lt;br /&gt;
living =&amp;gt; 2&lt;br /&gt;
field =&amp;gt; 2&lt;br /&gt;
from =&amp;gt; 2&lt;br /&gt;
or =&amp;gt; 2&lt;br /&gt;
be =&amp;gt; 2&lt;br /&gt;
these =&amp;gt; 2&lt;br /&gt;
our =&amp;gt; 2&lt;br /&gt;
long =&amp;gt; 2&lt;br /&gt;
what =&amp;gt; 2&lt;br /&gt;
dedicate =&amp;gt; 2&lt;br /&gt;
which =&amp;gt; 2&lt;br /&gt;
men =&amp;gt; 2&lt;br /&gt;
devotion =&amp;gt; 2&lt;br /&gt;
on =&amp;gt; 2&lt;br /&gt;
far =&amp;gt; 2&lt;br /&gt;
created =&amp;gt; 1&lt;br /&gt;
testing =&amp;gt; 1&lt;br /&gt;
battle =&amp;gt; 1&lt;br /&gt;
proper =&amp;gt; 1&lt;br /&gt;
brave =&amp;gt; 1&lt;br /&gt;
vain =&amp;gt; 1&lt;br /&gt;
under =&amp;gt; 1&lt;br /&gt;
ago =&amp;gt; 1&lt;br /&gt;
all =&amp;gt; 1&lt;br /&gt;
hallow =&amp;gt; 1&lt;br /&gt;
add =&amp;gt; 1&lt;br /&gt;
nor =&amp;gt; 1&lt;br /&gt;
work =&amp;gt; 1&lt;br /&gt;
honored =&amp;gt; 1&lt;br /&gt;
cause =&amp;gt; 1&lt;br /&gt;
government =&amp;gt; 1&lt;br /&gt;
perish =&amp;gt; 1&lt;br /&gt;
score =&amp;gt; 1&lt;br /&gt;
as =&amp;gt; 1&lt;br /&gt;
their =&amp;gt; 1&lt;br /&gt;
poor =&amp;gt; 1&lt;br /&gt;
world =&amp;gt; 1&lt;br /&gt;
will =&amp;gt; 1&lt;br /&gt;
note =&amp;gt; 1&lt;br /&gt;
thus =&amp;gt; 1&lt;br /&gt;
advanced =&amp;gt; 1&lt;br /&gt;
increased =&amp;gt; 1&lt;br /&gt;
earth =&amp;gt; 1&lt;br /&gt;
engaged =&amp;gt; 1&lt;br /&gt;
civil =&amp;gt; 1&lt;br /&gt;
should =&amp;gt; 1&lt;br /&gt;
do =&amp;gt; 1&lt;br /&gt;
detract =&amp;gt; 1&lt;br /&gt;
say =&amp;gt; 1&lt;br /&gt;
unfinished =&amp;gt; 1&lt;br /&gt;
continent =&amp;gt; 1&lt;br /&gt;
equal =&amp;gt; 1&lt;br /&gt;
any =&amp;gt; 1&lt;br /&gt;
met =&amp;gt; 1&lt;br /&gt;
come =&amp;gt; 1&lt;br /&gt;
live =&amp;gt; 1&lt;br /&gt;
never =&amp;gt; 1&lt;br /&gt;
resolve =&amp;gt; 1&lt;br /&gt;
died =&amp;gt; 1&lt;br /&gt;
god =&amp;gt; 1&lt;br /&gt;
seven =&amp;gt; 1&lt;br /&gt;
brought =&amp;gt; 1&lt;br /&gt;
forth =&amp;gt; 1&lt;br /&gt;
whether =&amp;gt; 1&lt;br /&gt;
endure =&amp;gt; 1&lt;br /&gt;
those =&amp;gt; 1&lt;br /&gt;
altogether =&amp;gt; 1&lt;br /&gt;
consecrated =&amp;gt; 1&lt;br /&gt;
above =&amp;gt; 1&lt;br /&gt;
power =&amp;gt; 1&lt;br /&gt;
remember =&amp;gt; 1&lt;br /&gt;
forget =&amp;gt; 1&lt;br /&gt;
task =&amp;gt; 1&lt;br /&gt;
four =&amp;gt; 1&lt;br /&gt;
years =&amp;gt; 1&lt;br /&gt;
proposition =&amp;gt; 1&lt;br /&gt;
now =&amp;gt; 1&lt;br /&gt;
final =&amp;gt; 1&lt;br /&gt;
resting =&amp;gt; 1&lt;br /&gt;
sense =&amp;gt; 1&lt;br /&gt;
little =&amp;gt; 1&lt;br /&gt;
nobly =&amp;gt; 1&lt;br /&gt;
remaining =&amp;gt; 1&lt;br /&gt;
last =&amp;gt; 1&lt;br /&gt;
measure =&amp;gt; 1&lt;br /&gt;
freedom =&amp;gt; 1&lt;br /&gt;
fathers =&amp;gt; 1&lt;br /&gt;
liberty =&amp;gt; 1&lt;br /&gt;
portion =&amp;gt; 1&lt;br /&gt;
place =&amp;gt; 1&lt;br /&gt;
lives =&amp;gt; 1&lt;br /&gt;
might =&amp;gt; 1&lt;br /&gt;
fitting =&amp;gt; 1&lt;br /&gt;
larger =&amp;gt; 1&lt;br /&gt;
consecrate =&amp;gt; 1&lt;br /&gt;
ground =&amp;gt; 1&lt;br /&gt;
struggled =&amp;gt; 1&lt;br /&gt;
did =&amp;gt; 1&lt;br /&gt;
fought =&amp;gt; 1&lt;br /&gt;
before =&amp;gt; 1&lt;br /&gt;
take =&amp;gt; 1&lt;br /&gt;
full =&amp;gt; 1&lt;br /&gt;
highly =&amp;gt; 1&lt;br /&gt;
birth =&amp;gt; 1&lt;br /&gt;
by =&amp;gt; 1 &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
{{ Tip | It is good practice to declare variables, parameters, and fields to be of the least specific type possible (but no less specific :). This is commonly an interface. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt; when comparing Objects (except when comparing to null).  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use % which can return a negative number.  Use [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(x,y)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT parallelize. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT simply remove and add for put on a Map.  Look for the entry and set its value if you find it.  Otherwise add it. }}&lt;br /&gt;
&lt;br /&gt;
==DefaultHashFunction==&lt;br /&gt;
{{CodeToImplement|DefaultHashFunction|applyAsInt|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
Hashing is a powerful tool.  We will use it to build our NotThreadSafeHashTable, as well as the [[Matrix_MapReduce_Framework_Assignment|MatrixMapReduceFramework]] later in the semester.  Hashing relies on evenly distributing keys to provide performance improvements.  As such, it will be useful to specify a particular hash function for our Hash Table (and MatrixMapReduceFramework later) as they will want to be different.&lt;br /&gt;
&lt;br /&gt;
Here we create a default hash function which implements {{ToIntFunctionLink}} which can serve as the hash function for our NotThreadSafeHashTable or wherever we need one.&lt;br /&gt;
&lt;br /&gt;
We will just use the standard mechanism for acquiring a hashCode for an Object.  Investigate [[SmearHashFunction|SmearHashFunction]] which [https://github.com/mraad/dbscan-spark/blob/master/src/main/java/com/esri/dbscan/Smear.java smears] the hash code to provide a different result.&lt;br /&gt;
&lt;br /&gt;
===applyAsInt(key)===&lt;br /&gt;
{{Sequential|public int applyAsInt(K key)}}&lt;br /&gt;
&lt;br /&gt;
Return the result of calling [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#hashCode-java.lang.Object- Objects.hashCode(key)] which nicely handles null.&lt;br /&gt;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
{{CodeToImplement|HashUtils|hashCodeOf&amp;lt;br/&amp;gt;arrayIndexForKey&amp;lt;br/&amp;gt;itemInArrayForKey|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
=== hashCodeOf(key, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int hashCodeOf(K key, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Invoke the hashFunction's [https://docs.oracle.com/javase/8/docs/api/java/util/function/ToIntFunction.html#applyAsInt-T- applyAsInt(value)] method and return the result.&lt;br /&gt;
&lt;br /&gt;
=== arrayIndexForKey(key, arrayLength, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int arrayIndexForKey(K key, int arrayLength, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Using [[Not_Thread_Safe_Hash_Table_Assignment#hashCodeOf.28key.2C_hashFunction.29|hashCodeOf]] with the provided key will return an int value between (inclusive) Integer.MIN_VALUE and Integer.MAX_VALUE, that is: between (inclusive) -2147483648 and 2147483647.&lt;br /&gt;
&lt;br /&gt;
To convert this to a suitable array index, we would like to take the positive remainder of the value and the arrayLength.&lt;br /&gt;
&lt;br /&gt;
Simply using the % operator will not do, as a negative hashCode would result in a negative array index (which would, of course, produce an array [https://docs.oracle.com/javase/8/docs/api/java/lang/IndexOutOfBoundsException.html IndexOutOfBoundsException]).&lt;br /&gt;
&lt;br /&gt;
Using [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(dividend,divisor)] solves this problem by always returning a remainder with the sign of the divisor.&lt;br /&gt;
&lt;br /&gt;
=== itemInArrayForKey(key, array, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K, E&amp;gt; E itemInArrayForKey(K key, E[] array, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Leverage [[Not_Thread_Safe_Hash_Table_Assignment#arrayIndexForKey.28key.2C_arrayLength.2C_hashFunction.29|arrayIndexForKey]] to find the index in the array, then return the item at that index.&lt;br /&gt;
&lt;br /&gt;
==DefaultEntry&amp;lt;K,V&amp;gt;==&lt;br /&gt;
{{Warning | This is in student-pre-aspect!!!}} &lt;br /&gt;
Whatever language you happen to be programming in there will be a way to associate keys with values.  Whether the data structure is called a [https://docs.python.org/3/tutorial/datastructures.html#dictionaries Dictionary] in Python, a [https://ruby-doc.org/core-3.1.2/Hash.html Hash] in Ruby, an [https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node153.html Association List] in Lisp, or a [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt;] in Java, there will be a way to store the (key, value) pairs.&lt;br /&gt;
&lt;br /&gt;
Java provides the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html interface Entry&amp;lt;K, V&amp;gt;] which is used on all of its Maps.  We will implement this interface in class DefaultEntry&amp;lt;K,V&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultEntry|constructor&amp;lt;br&amp;gt;getKey&amp;lt;br&amp;gt;getValue&amp;lt;br&amp;gt;setValue|entry.exercise|student-pre-aspect}}&lt;br /&gt;
&lt;br /&gt;
===constructor DefaultEntry(key, value)===&lt;br /&gt;
{{Sequential|public DefaultEntry(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the key and value specified as parameters in instance variables to be returned (and perhaps mutated in the case of value) later.&lt;br /&gt;
&lt;br /&gt;
===getKey()===&lt;br /&gt;
{{Sequential|public K getKey()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetKeyLink}} specification&lt;br /&gt;
&lt;br /&gt;
===getValue()===&lt;br /&gt;
{{Sequential|public V getValue()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
===setValue()===&lt;br /&gt;
{{Sequential|public V setValue(V value)}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntrySetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
==NotThreadSafeHashTable==&lt;br /&gt;
NotThreadSafeHashTable will use an entry’s key to place an entry into one of several buckets.  Each bucket will hold a chain of entries.  We maintain a chain of entries for when collisions naturally occur when multiple keys want to be placed in the same bucket.&lt;br /&gt;
&lt;br /&gt;
HashTables are a popular data structure because we can cut down the runtime of searching for entries within the map by adding more buckets/chains.  The time required to search for a particular entry drops to roughly 1/total_number_of_buckets (in comparison to one long chain which contains every entry).&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Hash_table hash table on wikipedia]&lt;br /&gt;
&lt;br /&gt;
[[File:Hash_table_5_0_1_1_1_1_1_LL.svg|frame|[https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg mediawiki hash table image]]]&lt;br /&gt;
&lt;br /&gt;
In this class, you will use the entry’s key’s hashcode to determine which of the buckets it should fall into. In order to calculate the index of the bucket that the entry belongs in, we will make use of the hash function, which in turn uses Math.floorMod to calculate a modulo (remainder or % in Java) based on the number of buckets and the key’s hashcode. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A Map (also sometimes referred to as a Dictionary or a Hashtable) is an abstract data structure which has the capabilities of associating a value with a key: &amp;lt;code&amp;gt;map.put(key, value)&amp;lt;/code&amp;gt;, looking up the associated value with a key: &amp;lt;code&amp;gt;map.get(key)&amp;lt;/code&amp;gt;, and removing &amp;lt;code&amp;gt;map.remove(key)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The standard Java Collections Framework [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map] provides a number of other nice methods, but put, get, and remove are the core of any Map in any programming language.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since your class will implement Map&amp;lt;K,V&amp;gt;.  We will leave many of the methods as &amp;lt;code&amp;gt;throw new NotRequiredForTheAssignmentException();&amp;lt;/code&amp;gt; while we focus on some critical methods of import. Specifications for these methods can be found in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt; javadocs]&lt;br /&gt;
&lt;br /&gt;
{{Warning | Note the specifications of the return values.}}&lt;br /&gt;
&lt;br /&gt;
Further, the standard interface for a key value pair is [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html Map's Entry] with the critical methods:&lt;br /&gt;
&lt;br /&gt;
* K getKey()&lt;br /&gt;
* V getValue()&lt;br /&gt;
* V setValue(V value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
We have provided an implementation of Entry for you called [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/util/KeyMutableValuePair.html KeyMutableValuePair&amp;lt;K,V&amp;gt;].&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|NotThreadSafeHashTable|constructor&amp;lt;br/&amp;gt;hashFunction&amp;lt;br/&amp;gt;chains&amp;lt;br/&amp;gt;entryOf&amp;lt;br/&amp;gt;put&amp;lt;br/&amp;gt;get&amp;lt;br/&amp;gt;computeIfAbsent&amp;lt;br/&amp;gt;compute|NotThreadSafeHashTable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor: NotThreadSafeHashTable(chainCount, hashFunction)===&lt;br /&gt;
{{Sequential|public NotThreadSafeHashTable(int chainCount, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Initialize &amp;lt;code&amp;gt;chainCount&amp;lt;/code&amp;gt; chains.  Additionally, hang onto the &amp;lt;code&amp;gt;hashFunction&amp;lt;/code&amp;gt; for later use.&lt;br /&gt;
&lt;br /&gt;
{{Requirement | In order to facilitate testing and debugging, create instances of either [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/ArrayList.html java.util.ArrayList] or [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/LinkedList.html java.util.LinkedList].}}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Initializing the contents of an array is one of the few cases where the indexing for loop is superior to the for-each loop.}}&lt;br /&gt;
&lt;br /&gt;
Use: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;for (int i = 0; i &amp;lt; buckets.length; ++i)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in this case.&lt;br /&gt;
&lt;br /&gt;
Implementing the constructor has a couple of tricky parts.&lt;br /&gt;
&lt;br /&gt;
====Parameterized Type Array Tip====&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Creating arrays of parameterized types in Java is madness inducing.  Some details are available in [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays Java Generics Restrictions].&lt;br /&gt;
&lt;br /&gt;
The example below creates an array of List&amp;lt;String&amp;gt;.  The &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt; annotation is optional.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
List&amp;lt;String&amp;gt;[] result = new List[length];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, you will encounter a much dreaded problem in Java.  One cannot instantiate a new generic array.  While there is [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays a reason for this limitation], it is still frustrating.&lt;br /&gt;
&lt;br /&gt;
So while you might think you should enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[capacity]; // NOTE: this will not compile&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the error reported says it all &amp;quot;Cannot create a generic array of Collection&amp;lt;Map.Entry&amp;lt;K,V&amp;gt;&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
You must instead create the array without the generic type parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection[capacity];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in a warning which the @SuppressWarnings(&amp;quot;unchecked&amp;quot;) annotation we added to the constructor takes care of.&lt;br /&gt;
&lt;br /&gt;
===hashFunction()===&lt;br /&gt;
{{Sequential|public ToIntFunction&amp;lt;K&amp;gt; hashFunction()}}&lt;br /&gt;
&lt;br /&gt;
Return the hashFunction provided to the constructor.&lt;br /&gt;
&lt;br /&gt;
===chains()===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[] chains()}}&lt;br /&gt;
&lt;br /&gt;
Return the chains created in the constructor.  This method is required for the provided entrySet() method and provides the additional benefit of allowing the creation of the chains to be tested before moving on to the public methods (get, put, computeIfAbsent, compute) which have little hope of working correctly if the chains are not correct.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===chainIndexOf(key)===&lt;br /&gt;
{{Sequential|private int chainIndexOf(K key)}}&lt;br /&gt;
&lt;br /&gt;
For a given key return the index into the array of buckets.  The correct implementation should leverage the [https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode-- key's hash code] to generate a [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- valid index of the bucket array].&lt;br /&gt;
&lt;br /&gt;
{{Tip | You should have just built a utility method which should be useful here.}}&lt;br /&gt;
&lt;br /&gt;
===chainOf(key)===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chainOf(Object key)}}&lt;br /&gt;
&lt;br /&gt;
chainIndexOf(key) returns the index.  chainOf(key) returns the chain at that index.&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===entryOf(chain, key)===&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; entryOf(Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chain, K key)}}&lt;br /&gt;
&lt;br /&gt;
Many of the methods listed above exist solely for the purpose of testing to assist in debugging.  It can be argued that the abstraction is past any point of reasonable abstraction.  Not so for entryOf(chain, key).  It is perfectly reasonable and useful to build this method as it will be well used by get, put, computeIfAbsent, compute, and many other methods on Map.&lt;br /&gt;
&lt;br /&gt;
{{Tip|Use the sequential for each loop to iterate through the chain.  This is both good style and necessary for the testing.}}&lt;br /&gt;
&lt;br /&gt;
===put(key, value)===&lt;br /&gt;
{{Sequential|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Note the required return value in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- put(key,value) specification].&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
===get(key)===&lt;br /&gt;
{{Sequential|public V get(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object- get(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
===computeIfAbsent(key, mappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function- computeIfAbsent(key, mappingFunction) specification] uses invocations of get and put, you should do neither.  You must achieve the computeIfAbsent specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
===compute(key, remappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- compute(key, remappingFunction) specification] uses invocations of get, put, and remove, you should do none of those things.  You must achieve the compute specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===remove(key)===&lt;br /&gt;
{{Sequential|public V remove(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#remove-java.lang.Object- remove(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Warning | For an efficient solution (and to avoid a possible ConcurrentModificationException) you will want to use the bucket's iterator directly and leverage its remove method.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only a single bucket's collection should be iterated over once via the iterator() method on appropriate bucket. }}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===size()===&lt;br /&gt;
{{Sequential|public int size()}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size-- size() spec]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__NotThreadSafeHashTableTestSuite|hashtable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-hash-table}}&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=4575</id>
		<title>Not Thread Safe Hash Table Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=4575"/>
		<updated>2023-02-16T19:49:39Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
We gain experience implementing an abstract data type.  Specifically, we will build a {{NotThreadSafeLink}} class which implements the java.util.Map&amp;lt;K,V&amp;gt; interface.  Later in the semester, we will implement a {{ThreadSafeLink}} version: [[Concurrent_Hash_Table_Assignment|ConcurrentHashTable]].&lt;br /&gt;
&lt;br /&gt;
We gain experience with generics since the Map interface is parameterized with multiple generic types: K for the key type and V for the value type.&lt;br /&gt;
&lt;br /&gt;
We will build some utilities to support hashing and key-value pairs which we will use throughout the semester.&lt;br /&gt;
&lt;br /&gt;
Finally, the selection of methods we will implement includes two which you may not be familiar with: compute() and computeIfAbsent().  Each of these methods have special roles to play throughout the semester.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
== Hash Tables==&lt;br /&gt;
* [https://en.wikipedia.org/wiki/Hash_table Wikipedia Hash Table]&lt;br /&gt;
* [https://visualgo.net/en/hashtable VisuAlgo Hash Table]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|How To Use java.util.Map|&amp;lt;youtube&amp;gt;MwPrMAG4bLg&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|CS50 Shorts: Hash Tables|&amp;lt;youtube&amp;gt;nvzVHwrrub0&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|HackerRank Hash Tables|&amp;lt;youtube&amp;gt;shs0KM3wKv8&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Java Utilities To Use==&lt;br /&gt;
===Map.Entry&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapEntryWithMethodLinks}}&lt;br /&gt;
===Map&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapWithMethodLinks}}&lt;br /&gt;
===Optional&amp;lt;T&amp;gt;===&lt;br /&gt;
{{OptionalWithMethodsLinks}}&lt;br /&gt;
===ToIntFunction&amp;lt;T&amp;gt;===&lt;br /&gt;
{{ToIntFunctionWithMethodLinks}}&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
Each of the example clients operate on the words of the [https://rmc.library.cornell.edu/gettysburg/good_cause/transcript.htm Gettysburg Address].&lt;br /&gt;
&lt;br /&gt;
==PutClient==&lt;br /&gt;
{{Client|PutClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapPutLink}}.&lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with the word.  Since put replaces a previous association, if it exists, the last word starting with a particular letter wins.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|PutClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, String&amp;gt; mapFirstCharacterToLastEncounteredWord = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            mapFirstCharacterToLastEncounteredWord.put(firstCharacterLowerCased, word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    String value = mapFirstCharacterToLastEncounteredWord.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|PutClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: and&lt;br /&gt;
b: by&lt;br /&gt;
c: cause&lt;br /&gt;
d: died&lt;br /&gt;
e: earth&lt;br /&gt;
f: from&lt;br /&gt;
g: government&lt;br /&gt;
h: have&lt;br /&gt;
i: in&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: last&lt;br /&gt;
m: measure&lt;br /&gt;
n: not&lt;br /&gt;
o: of&lt;br /&gt;
p: perish&lt;br /&gt;
q: &lt;br /&gt;
r: resolve&lt;br /&gt;
s: shall&lt;br /&gt;
t: the&lt;br /&gt;
u: under&lt;br /&gt;
v: vain&lt;br /&gt;
w: we&lt;br /&gt;
x: &lt;br /&gt;
y: years&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeIfAbsentClient==&lt;br /&gt;
{{Client|ComputeIfAbsentClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
This client uses repeated calls to {{MapComputeIfAbsentLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with a list.  The first time a letter is encountered the mappingFunction will be invoked which returns the new list to associate with the letter.  Conveniently, whether it has been associated before or the current iteration, the List associated with the letter is returned from computeIfAbsent.  The current word is then added to that list.  Since the returned list is a reference to mutable data there is no need to re-associate the list with the letter.  When the loops are completed, each letter holds a list of all the encountered words (including any duplicates). &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeIfAbsentClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, List&amp;lt;String&amp;gt;&amp;gt; mapFirstCharacterToListOfWords = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            List&amp;lt;String&amp;gt; list = mapFirstCharacterToListOfWords.computeIfAbsent(firstCharacterLowerCased,&lt;br /&gt;
                    (unusedKey) -&amp;gt; {&lt;br /&gt;
                        return new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
                    });&lt;br /&gt;
            list.add(word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    List&amp;lt;String&amp;gt; value = mapFirstCharacterToListOfWords.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeIfAbsentClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: [and, ago, a, and, all, are, are, a, any, and, are, a, a, as, a, altogether, and, a, and, above, add, advanced, a, and]&lt;br /&gt;
b: [brought, battle, But, brave, but, be, be, before, birth, by]&lt;br /&gt;
c: [continent, conceived, created, civil, conceived, can, come, can, can, consecrate, can, consecrated, can, cause]&lt;br /&gt;
d: [dedicated, dedicated, dedicate, do, dedicate, dead, detract, did, dedicated, dedicated, dead, devotion, devotion, dead, died]&lt;br /&gt;
e: [equal, engaged, endure, earth]&lt;br /&gt;
f: [Four, fathers, forth, field, field, final, for, fitting, far, forget, for, fought, far, for, from, for, full, freedom, for, from]&lt;br /&gt;
g: [great, great, gave, ground, great, gave, God, government]&lt;br /&gt;
h: [have, here, hallow, here, have, here, here, here, here, have, here, honored, here, highly, have, have]&lt;br /&gt;
i: [in, in, It, is, in, it, it, It, is, It, is, increased, in]&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: [Liberty, long, lives, live, larger, living, little, long, living, last]&lt;br /&gt;
m: [men, met, might, men, measure]&lt;br /&gt;
n: [new, nation, Now, nation, nation, nation, not, not, not, note, nor, never, nobly, not, nation, new, not]&lt;br /&gt;
o: [our, on, or, on, of, of, our, or, of, of, of]&lt;br /&gt;
p: [proposition, portion, place, proper, poor, power, people, people, people, perish]&lt;br /&gt;
q: &lt;br /&gt;
r: [resting, remember, rather, rather, remaining, resolve]&lt;br /&gt;
s: [score, seven, so, so, should, sense, struggled, say, so, shall, shall, shall]&lt;br /&gt;
t: [this, to, the, that, testing, that, that, to, that, those, their, that, that, that, this, this, The, to, The, they, the, to, to, the, they, thus, to, to, the, task, that, these, take, to, that, they, the, that, that, these, that, this, that, the, the, the, the]&lt;br /&gt;
u: [us, unfinished, us, us, under]&lt;br /&gt;
v: [vain]&lt;br /&gt;
w: [we, war, whether, We, war, We, who, we, we, we, we, who, world, will, what, we, what, work, which, who, we, which, we]&lt;br /&gt;
x: &lt;br /&gt;
y: [years]&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeClient==&lt;br /&gt;
{{Client|ComputeClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapComputeLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by computing the number of times the (lower case version of the) word has been encountered.  Each time compute is called the remappingFunction is passed the key (which is (as is often the case) unused) and the previous value associated with the key, or null if has no association.  The remapping function returns the value to associate with the key.  Since we are counting the occurrences, we add 1 to the previous value, or simply return 1 if that previous value is null.&lt;br /&gt;
&lt;br /&gt;
When the loops are completed, each word holds the number of times it was encountered.. &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; mapWordToCount = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
	for(String word : textSection.words()) {&lt;br /&gt;
		if (word.length() &amp;gt; 0) {&lt;br /&gt;
			String lowerCaseWord = word.toLowerCase();&lt;br /&gt;
			mapWordToCount.compute(lowerCaseWord, (unusedKey, prevValue) -&amp;gt; {&lt;br /&gt;
				if (prevValue != null) {&lt;br /&gt;
					return prevValue + 1;&lt;br /&gt;
				} else {&lt;br /&gt;
					return 1;&lt;br /&gt;
				}&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
System.out.println(toString(mapWordToCount));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;that =&amp;gt; 13&lt;br /&gt;
the =&amp;gt; 11&lt;br /&gt;
we =&amp;gt; 10&lt;br /&gt;
here =&amp;gt; 8&lt;br /&gt;
to =&amp;gt; 8&lt;br /&gt;
a =&amp;gt; 7&lt;br /&gt;
and =&amp;gt; 6&lt;br /&gt;
can =&amp;gt; 5&lt;br /&gt;
have =&amp;gt; 5&lt;br /&gt;
for =&amp;gt; 5&lt;br /&gt;
it =&amp;gt; 5&lt;br /&gt;
not =&amp;gt; 5&lt;br /&gt;
nation =&amp;gt; 5&lt;br /&gt;
of =&amp;gt; 5&lt;br /&gt;
in =&amp;gt; 4&lt;br /&gt;
this =&amp;gt; 4&lt;br /&gt;
dedicated =&amp;gt; 4&lt;br /&gt;
they =&amp;gt; 3&lt;br /&gt;
is =&amp;gt; 3&lt;br /&gt;
are =&amp;gt; 3&lt;br /&gt;
so =&amp;gt; 3&lt;br /&gt;
dead =&amp;gt; 3&lt;br /&gt;
shall =&amp;gt; 3&lt;br /&gt;
great =&amp;gt; 3&lt;br /&gt;
who =&amp;gt; 3&lt;br /&gt;
us =&amp;gt; 3&lt;br /&gt;
people =&amp;gt; 3&lt;br /&gt;
new =&amp;gt; 2&lt;br /&gt;
conceived =&amp;gt; 2&lt;br /&gt;
war =&amp;gt; 2&lt;br /&gt;
rather =&amp;gt; 2&lt;br /&gt;
gave =&amp;gt; 2&lt;br /&gt;
but =&amp;gt; 2&lt;br /&gt;
living =&amp;gt; 2&lt;br /&gt;
field =&amp;gt; 2&lt;br /&gt;
from =&amp;gt; 2&lt;br /&gt;
or =&amp;gt; 2&lt;br /&gt;
be =&amp;gt; 2&lt;br /&gt;
these =&amp;gt; 2&lt;br /&gt;
our =&amp;gt; 2&lt;br /&gt;
long =&amp;gt; 2&lt;br /&gt;
what =&amp;gt; 2&lt;br /&gt;
dedicate =&amp;gt; 2&lt;br /&gt;
which =&amp;gt; 2&lt;br /&gt;
men =&amp;gt; 2&lt;br /&gt;
devotion =&amp;gt; 2&lt;br /&gt;
on =&amp;gt; 2&lt;br /&gt;
far =&amp;gt; 2&lt;br /&gt;
created =&amp;gt; 1&lt;br /&gt;
testing =&amp;gt; 1&lt;br /&gt;
battle =&amp;gt; 1&lt;br /&gt;
proper =&amp;gt; 1&lt;br /&gt;
brave =&amp;gt; 1&lt;br /&gt;
vain =&amp;gt; 1&lt;br /&gt;
under =&amp;gt; 1&lt;br /&gt;
ago =&amp;gt; 1&lt;br /&gt;
all =&amp;gt; 1&lt;br /&gt;
hallow =&amp;gt; 1&lt;br /&gt;
add =&amp;gt; 1&lt;br /&gt;
nor =&amp;gt; 1&lt;br /&gt;
work =&amp;gt; 1&lt;br /&gt;
honored =&amp;gt; 1&lt;br /&gt;
cause =&amp;gt; 1&lt;br /&gt;
government =&amp;gt; 1&lt;br /&gt;
perish =&amp;gt; 1&lt;br /&gt;
score =&amp;gt; 1&lt;br /&gt;
as =&amp;gt; 1&lt;br /&gt;
their =&amp;gt; 1&lt;br /&gt;
poor =&amp;gt; 1&lt;br /&gt;
world =&amp;gt; 1&lt;br /&gt;
will =&amp;gt; 1&lt;br /&gt;
note =&amp;gt; 1&lt;br /&gt;
thus =&amp;gt; 1&lt;br /&gt;
advanced =&amp;gt; 1&lt;br /&gt;
increased =&amp;gt; 1&lt;br /&gt;
earth =&amp;gt; 1&lt;br /&gt;
engaged =&amp;gt; 1&lt;br /&gt;
civil =&amp;gt; 1&lt;br /&gt;
should =&amp;gt; 1&lt;br /&gt;
do =&amp;gt; 1&lt;br /&gt;
detract =&amp;gt; 1&lt;br /&gt;
say =&amp;gt; 1&lt;br /&gt;
unfinished =&amp;gt; 1&lt;br /&gt;
continent =&amp;gt; 1&lt;br /&gt;
equal =&amp;gt; 1&lt;br /&gt;
any =&amp;gt; 1&lt;br /&gt;
met =&amp;gt; 1&lt;br /&gt;
come =&amp;gt; 1&lt;br /&gt;
live =&amp;gt; 1&lt;br /&gt;
never =&amp;gt; 1&lt;br /&gt;
resolve =&amp;gt; 1&lt;br /&gt;
died =&amp;gt; 1&lt;br /&gt;
god =&amp;gt; 1&lt;br /&gt;
seven =&amp;gt; 1&lt;br /&gt;
brought =&amp;gt; 1&lt;br /&gt;
forth =&amp;gt; 1&lt;br /&gt;
whether =&amp;gt; 1&lt;br /&gt;
endure =&amp;gt; 1&lt;br /&gt;
those =&amp;gt; 1&lt;br /&gt;
altogether =&amp;gt; 1&lt;br /&gt;
consecrated =&amp;gt; 1&lt;br /&gt;
above =&amp;gt; 1&lt;br /&gt;
power =&amp;gt; 1&lt;br /&gt;
remember =&amp;gt; 1&lt;br /&gt;
forget =&amp;gt; 1&lt;br /&gt;
task =&amp;gt; 1&lt;br /&gt;
four =&amp;gt; 1&lt;br /&gt;
years =&amp;gt; 1&lt;br /&gt;
proposition =&amp;gt; 1&lt;br /&gt;
now =&amp;gt; 1&lt;br /&gt;
final =&amp;gt; 1&lt;br /&gt;
resting =&amp;gt; 1&lt;br /&gt;
sense =&amp;gt; 1&lt;br /&gt;
little =&amp;gt; 1&lt;br /&gt;
nobly =&amp;gt; 1&lt;br /&gt;
remaining =&amp;gt; 1&lt;br /&gt;
last =&amp;gt; 1&lt;br /&gt;
measure =&amp;gt; 1&lt;br /&gt;
freedom =&amp;gt; 1&lt;br /&gt;
fathers =&amp;gt; 1&lt;br /&gt;
liberty =&amp;gt; 1&lt;br /&gt;
portion =&amp;gt; 1&lt;br /&gt;
place =&amp;gt; 1&lt;br /&gt;
lives =&amp;gt; 1&lt;br /&gt;
might =&amp;gt; 1&lt;br /&gt;
fitting =&amp;gt; 1&lt;br /&gt;
larger =&amp;gt; 1&lt;br /&gt;
consecrate =&amp;gt; 1&lt;br /&gt;
ground =&amp;gt; 1&lt;br /&gt;
struggled =&amp;gt; 1&lt;br /&gt;
did =&amp;gt; 1&lt;br /&gt;
fought =&amp;gt; 1&lt;br /&gt;
before =&amp;gt; 1&lt;br /&gt;
take =&amp;gt; 1&lt;br /&gt;
full =&amp;gt; 1&lt;br /&gt;
highly =&amp;gt; 1&lt;br /&gt;
birth =&amp;gt; 1&lt;br /&gt;
by =&amp;gt; 1 &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
{{ Tip | It is good practice to declare variables, parameters, and fields to be of the least specific type possible (but no less specific :). This is commonly an interface. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt; when comparing Objects (except when comparing to null).  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use % which can return a negative number.  Use [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(x,y)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT parallelize. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT simply remove and add for put on a Map.  Look for the entry and set its value if you find it.  Otherwise add it. }}&lt;br /&gt;
&lt;br /&gt;
==DefaultHashFunction==&lt;br /&gt;
{{CodeToImplement|DefaultHashFunction|applyAsInt|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
Hashing is a powerful tool.  We will use it to build our NotThreadSafeHashTable, as well as the [[Matrix_MapReduce_Framework_Assignment|MatrixMapReduceFramework]] later in the semester.  Hashing relies on evenly distributing keys to provide performance improvements.  As such, it will be useful to specify a particular hash function for our Hash Table (and MatrixMapReduceFramework later) as they will want to be different.&lt;br /&gt;
&lt;br /&gt;
Here we create a default hash function which implements {{ToIntFunctionLink}} which can serve as the hash function for our NotThreadSafeHashTable or wherever we need one.&lt;br /&gt;
&lt;br /&gt;
We will just use the standard mechanism for acquiring a hashCode for an Object.  Investigate [[SmearHashFunction|SmearHashFunction]] which [https://github.com/mraad/dbscan-spark/blob/master/src/main/java/com/esri/dbscan/Smear.java smears] the hash code to provide a different result.&lt;br /&gt;
&lt;br /&gt;
===applyAsInt(key)===&lt;br /&gt;
{{Sequential|public int applyAsInt(K key)}}&lt;br /&gt;
&lt;br /&gt;
Return the result of calling [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#hashCode-java.lang.Object- Objects.hashCode(key)] which nicely handles null.&lt;br /&gt;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
{{CodeToImplement|HashUtils|hashCodeOf&amp;lt;br/&amp;gt;arrayIndexForKey&amp;lt;br/&amp;gt;itemInArrayForKey|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
=== hashCodeOf(key, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int hashCodeOf(K key, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Invoke the hashFunction's [https://docs.oracle.com/javase/8/docs/api/java/util/function/ToIntFunction.html#applyAsInt-T- applyAsInt(value)] method and return the result.&lt;br /&gt;
&lt;br /&gt;
=== arrayIndexForKey(key, arrayLength, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int arrayIndexForKey(K key, int arrayLength, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Using [[Not_Thread_Safe_Hash_Table_Assignment#hashCodeOf.28key.2C_hashFunction.29|hashCodeOf]] with the provided key will return an int value between (inclusive) Integer.MIN_VALUE and Integer.MAX_VALUE, that is: between (inclusive) -2147483648 and 2147483647.&lt;br /&gt;
&lt;br /&gt;
To convert this to a suitable array index, we would like to take the positive remainder of the value and the arrayLength.&lt;br /&gt;
&lt;br /&gt;
Simply using the % operator will not do, as a negative hashCode would result in a negative array index (which would, of course, produce an array [https://docs.oracle.com/javase/8/docs/api/java/lang/IndexOutOfBoundsException.html IndexOutOfBoundsException]).&lt;br /&gt;
&lt;br /&gt;
Using [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(dividend,divisor)] solves this problem by always returning a remainder with the sign of the divisor.&lt;br /&gt;
&lt;br /&gt;
=== itemInArrayForKey(key, array, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K, E&amp;gt; E itemInArrayForKey(K key, E[] array, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Leverage [[Not_Thread_Safe_Hash_Table_Assignment#arrayIndexForKey.28key.2C_arrayLength.2C_hashFunction.29|arrayIndexForKey]] to find the index in the array, then return the item at that index.&lt;br /&gt;
&lt;br /&gt;
==DefaultEntry&amp;lt;K,V&amp;gt;==&lt;br /&gt;
{{Alert | this is in student-pre-aspect}} &lt;br /&gt;
Whatever language you happen to be programming in there will be a way to associate keys with values.  Whether the data structure is called a [https://docs.python.org/3/tutorial/datastructures.html#dictionaries Dictionary] in Python, a [https://ruby-doc.org/core-3.1.2/Hash.html Hash] in Ruby, an [https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node153.html Association List] in Lisp, or a [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt;] in Java, there will be a way to store the (key, value) pairs.&lt;br /&gt;
&lt;br /&gt;
Java provides the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html interface Entry&amp;lt;K, V&amp;gt;] which is used on all of its Maps.  We will implement this interface in class DefaultEntry&amp;lt;K,V&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultEntry|constructor&amp;lt;br&amp;gt;getKey&amp;lt;br&amp;gt;getValue&amp;lt;br&amp;gt;setValue|entry.exercise|student-pre-aspect}}&lt;br /&gt;
&lt;br /&gt;
===constructor DefaultEntry(key, value)===&lt;br /&gt;
{{Sequential|public DefaultEntry(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the key and value specified as parameters in instance variables to be returned (and perhaps mutated in the case of value) later.&lt;br /&gt;
&lt;br /&gt;
===getKey()===&lt;br /&gt;
{{Sequential|public K getKey()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetKeyLink}} specification&lt;br /&gt;
&lt;br /&gt;
===getValue()===&lt;br /&gt;
{{Sequential|public V getValue()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
===setValue()===&lt;br /&gt;
{{Sequential|public V setValue(V value)}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntrySetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
==NotThreadSafeHashTable==&lt;br /&gt;
NotThreadSafeHashTable will use an entry’s key to place an entry into one of several buckets.  Each bucket will hold a chain of entries.  We maintain a chain of entries for when collisions naturally occur when multiple keys want to be placed in the same bucket.&lt;br /&gt;
&lt;br /&gt;
HashTables are a popular data structure because we can cut down the runtime of searching for entries within the map by adding more buckets/chains.  The time required to search for a particular entry drops to roughly 1/total_number_of_buckets (in comparison to one long chain which contains every entry).&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Hash_table hash table on wikipedia]&lt;br /&gt;
&lt;br /&gt;
[[File:Hash_table_5_0_1_1_1_1_1_LL.svg|frame|[https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg mediawiki hash table image]]]&lt;br /&gt;
&lt;br /&gt;
In this class, you will use the entry’s key’s hashcode to determine which of the buckets it should fall into. In order to calculate the index of the bucket that the entry belongs in, we will make use of the hash function, which in turn uses Math.floorMod to calculate a modulo (remainder or % in Java) based on the number of buckets and the key’s hashcode. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A Map (also sometimes referred to as a Dictionary or a Hashtable) is an abstract data structure which has the capabilities of associating a value with a key: &amp;lt;code&amp;gt;map.put(key, value)&amp;lt;/code&amp;gt;, looking up the associated value with a key: &amp;lt;code&amp;gt;map.get(key)&amp;lt;/code&amp;gt;, and removing &amp;lt;code&amp;gt;map.remove(key)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The standard Java Collections Framework [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map] provides a number of other nice methods, but put, get, and remove are the core of any Map in any programming language.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since your class will implement Map&amp;lt;K,V&amp;gt;.  We will leave many of the methods as &amp;lt;code&amp;gt;throw new NotRequiredForTheAssignmentException();&amp;lt;/code&amp;gt; while we focus on some critical methods of import. Specifications for these methods can be found in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt; javadocs]&lt;br /&gt;
&lt;br /&gt;
{{Warning | Note the specifications of the return values.}}&lt;br /&gt;
&lt;br /&gt;
Further, the standard interface for a key value pair is [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html Map's Entry] with the critical methods:&lt;br /&gt;
&lt;br /&gt;
* K getKey()&lt;br /&gt;
* V getValue()&lt;br /&gt;
* V setValue(V value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
We have provided an implementation of Entry for you called [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/util/KeyMutableValuePair.html KeyMutableValuePair&amp;lt;K,V&amp;gt;].&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|NotThreadSafeHashTable|constructor&amp;lt;br/&amp;gt;hashFunction&amp;lt;br/&amp;gt;chains&amp;lt;br/&amp;gt;entryOf&amp;lt;br/&amp;gt;put&amp;lt;br/&amp;gt;get&amp;lt;br/&amp;gt;computeIfAbsent&amp;lt;br/&amp;gt;compute|NotThreadSafeHashTable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor: NotThreadSafeHashTable(chainCount, hashFunction)===&lt;br /&gt;
{{Sequential|public NotThreadSafeHashTable(int chainCount, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Initialize &amp;lt;code&amp;gt;chainCount&amp;lt;/code&amp;gt; chains.  Additionally, hang onto the &amp;lt;code&amp;gt;hashFunction&amp;lt;/code&amp;gt; for later use.&lt;br /&gt;
&lt;br /&gt;
{{Requirement | In order to facilitate testing and debugging, create instances of either [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/ArrayList.html java.util.ArrayList] or [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/LinkedList.html java.util.LinkedList].}}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Initializing the contents of an array is one of the few cases where the indexing for loop is superior to the for-each loop.}}&lt;br /&gt;
&lt;br /&gt;
Use: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;for (int i = 0; i &amp;lt; buckets.length; ++i)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in this case.&lt;br /&gt;
&lt;br /&gt;
Implementing the constructor has a couple of tricky parts.&lt;br /&gt;
&lt;br /&gt;
====Parameterized Type Array Tip====&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Creating arrays of parameterized types in Java is madness inducing.  Some details are available in [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays Java Generics Restrictions].&lt;br /&gt;
&lt;br /&gt;
The example below creates an array of List&amp;lt;String&amp;gt;.  The &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt; annotation is optional.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
List&amp;lt;String&amp;gt;[] result = new List[length];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, you will encounter a much dreaded problem in Java.  One cannot instantiate a new generic array.  While there is [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays a reason for this limitation], it is still frustrating.&lt;br /&gt;
&lt;br /&gt;
So while you might think you should enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[capacity]; // NOTE: this will not compile&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the error reported says it all &amp;quot;Cannot create a generic array of Collection&amp;lt;Map.Entry&amp;lt;K,V&amp;gt;&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
You must instead create the array without the generic type parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection[capacity];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in a warning which the @SuppressWarnings(&amp;quot;unchecked&amp;quot;) annotation we added to the constructor takes care of.&lt;br /&gt;
&lt;br /&gt;
===hashFunction()===&lt;br /&gt;
{{Sequential|public ToIntFunction&amp;lt;K&amp;gt; hashFunction()}}&lt;br /&gt;
&lt;br /&gt;
Return the hashFunction provided to the constructor.&lt;br /&gt;
&lt;br /&gt;
===chains()===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[] chains()}}&lt;br /&gt;
&lt;br /&gt;
Return the chains created in the constructor.  This method is required for the provided entrySet() method and provides the additional benefit of allowing the creation of the chains to be tested before moving on to the public methods (get, put, computeIfAbsent, compute) which have little hope of working correctly if the chains are not correct.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===chainIndexOf(key)===&lt;br /&gt;
{{Sequential|private int chainIndexOf(K key)}}&lt;br /&gt;
&lt;br /&gt;
For a given key return the index into the array of buckets.  The correct implementation should leverage the [https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode-- key's hash code] to generate a [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- valid index of the bucket array].&lt;br /&gt;
&lt;br /&gt;
{{Tip | You should have just built a utility method which should be useful here.}}&lt;br /&gt;
&lt;br /&gt;
===chainOf(key)===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chainOf(Object key)}}&lt;br /&gt;
&lt;br /&gt;
chainIndexOf(key) returns the index.  chainOf(key) returns the chain at that index.&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===entryOf(chain, key)===&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; entryOf(Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chain, K key)}}&lt;br /&gt;
&lt;br /&gt;
Many of the methods listed above exist solely for the purpose of testing to assist in debugging.  It can be argued that the abstraction is past any point of reasonable abstraction.  Not so for entryOf(chain, key).  It is perfectly reasonable and useful to build this method as it will be well used by get, put, computeIfAbsent, compute, and many other methods on Map.&lt;br /&gt;
&lt;br /&gt;
{{Tip|Use the sequential for each loop to iterate through the chain.  This is both good style and necessary for the testing.}}&lt;br /&gt;
&lt;br /&gt;
===put(key, value)===&lt;br /&gt;
{{Sequential|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Note the required return value in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- put(key,value) specification].&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
===get(key)===&lt;br /&gt;
{{Sequential|public V get(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object- get(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
===computeIfAbsent(key, mappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function- computeIfAbsent(key, mappingFunction) specification] uses invocations of get and put, you should do neither.  You must achieve the computeIfAbsent specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
===compute(key, remappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- compute(key, remappingFunction) specification] uses invocations of get, put, and remove, you should do none of those things.  You must achieve the compute specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only the lone correct chain of entries should be searched (and searched only once). }}&lt;br /&gt;
&lt;br /&gt;
{{Tip | When you find an already existing Entry for a key, do not create a new Entry.  Simply set the value of the existing Entry.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===remove(key)===&lt;br /&gt;
{{Sequential|public V remove(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#remove-java.lang.Object- remove(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Warning | For an efficient solution (and to avoid a possible ConcurrentModificationException) you will want to use the bucket's iterator directly and leverage its remove method.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only a single bucket's collection should be iterated over once via the iterator() method on appropriate bucket. }}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===size()===&lt;br /&gt;
{{Sequential|public int size()}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size-- size() spec]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__NotThreadSafeHashTableTestSuite|hashtable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-hash-table}}&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4494</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4494"/>
		<updated>2023-02-10T07:19:21Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Join ==&lt;br /&gt;
&amp;lt;p&amp;gt;The various &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; functions gets the value of the &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; or multiple &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; objects passed in and returns the value(s) in a convenient manner. You can pass in any number of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; objects, collections of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, arrays of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; and in the cases that you passed in multiple values, join can return either a list or an array, so most of the heavy-lifting should be taken care of by the compiler. Here are some general reminders/pointers regarding the &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; method. All &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods are a blocking methods, so if the value isn't ready, the program just sits there. &amp;lt;insert this is fine dog&amp;gt; Also, all &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods may throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;. You may notice that these are the same errors as are thrown in the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; method of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;...&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
&amp;lt;p&amp;gt;1 down, 26 to go! Don't worry, things will get faster now as there is less to explain.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Indices ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html fork_loop](int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. It takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. Then, when the values return from the branches, the &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; loop bundles them back up in an array, such that the return value of the &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt;th branch is the 0th element in the returned array, and the return value of the &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;th branch is the last element of the returned array. Isn't that handy? This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt;[] updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;So &amp;lt;code&amp;gt;parallel_double(input)[0] = input[0] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[1] = input[1] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[2] = input[2] * 2&amp;lt;/code&amp;gt;, etc. Notice that we did not have to declare the type of &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;. This is because the definition of &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; defines the third parameter, the lambda function, as a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, which we already know takes in a value of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Iterables and Arrays ===&lt;br /&gt;
&amp;lt;p&amp;gt;We will now explore the following versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html List]&amp;lt;[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;&amp;gt; [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Iterable,fj.api.TaskFunction) fork_loop]([https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Iterable.html Iterable]&amp;lt;T&amp;gt; iterable, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Object[],fj.api.TaskFunction) fork_loop](T[] array, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The first version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;List&amp;lt;Future&amp;lt;R&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that takes in a parameter of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. So for each of the elements of the given &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;, a parallel call is made to the given function with that element passed in as the parameter. The second version does much the same, except it operates using arrays.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork list example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(List&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt; list) {&lt;br /&gt;
    List&amp;lt;Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt;&amp;gt; myFuture = join_fork(list, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[][] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of the generics! The element type of a list is just whatever is within the angled brackets.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork array example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(ScaryType&amp;lt;Integer&amp;gt;[][] array) {&lt;br /&gt;
    Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[]&amp;gt;[] myFuture = join_fork(array, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of multiple arrays. The element of an array is just the type if you ignore the last pair of braces. If you ever forget, ask yourself &amp;quot;What do I get when I index into this array.&amp;quot; That is an element.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;These versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; have a lot in common with a for each loop, and thus comes with many of the same programming considerations. If you need to index into an array, this might not be your best choice. But if you are simply iterating through some kind of collection, these could be a great choice.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop with Index ==&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;fork_loop_with_index&amp;lt;/code&amp;gt; is like a combination of the iterable &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; and the indexed &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunctionWithIndex&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Looking at the documentation for a &amp;lt;code&amp;gt;TaskFunctionWithIndex&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;, we can see that it takes in two parameters, the first being an argument of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and the second being an integer value, and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It supplies an index along with each element of the &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. Use this function if you want to index while iterating across an entire &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; without having to specify &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4493</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4493"/>
		<updated>2023-02-10T07:14:12Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Join ==&lt;br /&gt;
&amp;lt;p&amp;gt;The various &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; functions gets the value of the &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; or multiple &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; objects passed in and returns the value(s) in a convenient manner. You can pass in any number of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; objects, collections of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, arrays of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; and in the cases that you passed in multiple values, join can return either a list or an array, so most of the heavy-lifting should be taken care of by the compiler. Here are some general reminders/pointers regarding the &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; method. All &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods are a blocking methods, so if the value isn't ready, the program just sits there. &amp;lt;insert this is fine dog&amp;gt; Also, all &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods may throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;. You may notice that these are the same errors as are thrown in the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; method of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;...&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
&amp;lt;p&amp;gt;1 down, 26 to go! Don't worry, things will get faster now as there is less to explain.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Indices ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html fork_loop](int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. It takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. Then, when the values return from the branches, the &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; loop bundles them back up in an array, such that the return value of the &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt;th branch is the 0th element in the returned array, and the return value of the &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;th branch is the last element of the returned array. Isn't that handy? This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt;[] updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;So &amp;lt;code&amp;gt;parallel_double(input)[0] = input[0] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[1] = input[1] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[2] = input[2] * 2&amp;lt;/code&amp;gt;, etc. Notice that we did not have to declare the type of &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;. This is because the definition of &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; defines the third parameter, the lambda function, as a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, which we already know takes in a value of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Iterables and Arrays ===&lt;br /&gt;
&amp;lt;p&amp;gt;We will now explore the following versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html List]&amp;lt;[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;&amp;gt; [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Iterable,fj.api.TaskFunction) fork_loop]([https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Iterable.html Iterable]&amp;lt;T&amp;gt; iterable, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Object[],fj.api.TaskFunction) fork_loop](T[] array, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The first version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;List&amp;lt;Future&amp;lt;R&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that takes in a parameter of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. So for each of the elements of the given &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;, a parallel call is made to the given function with that element passed in as the parameter. The second version does much the same, except it operates using arrays.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork list example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(List&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt; list) {&lt;br /&gt;
    List&amp;lt;Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt;&amp;gt; myFuture = join_fork(list, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[][] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of the generics! The element type of a list is just whatever is within the angled brackets.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork array example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(ScaryType&amp;lt;Integer&amp;gt;[][] array) {&lt;br /&gt;
    Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[]&amp;gt;[] myFuture = join_fork(array, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of multiple arrays. The element of an array is just the type if you ignore the last pair of braces. If you ever forget, ask yourself &amp;quot;What do I get when I index into this array.&amp;quot; That is an element.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;These versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; have a lot in common with a for each loop, and thus comes with many of the same programming considerations. If you need to index into an array, this might not be your best choice. But if you are simply iterating through some kind of collection, these could be a great choice.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop with Index ==&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;fork_loop_with_index&amp;lt;/code&amp;gt; is like a combination of the iterable &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; and the indexed &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunctionWithIndex&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Looking at the documentation for a &amp;lt;code&amp;gt;TaskFunctionWithIndex&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;, we can see that it takes in two parameters, the first being an argument of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and the second being an integer value, and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It supplies an index along with each element of the &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. Use this function if you want to index while iterating across an entire &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; without having to specify &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4492</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4492"/>
		<updated>2023-02-10T06:44:17Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Join ==&lt;br /&gt;
&amp;lt;p&amp;gt;The various &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; functions gets the value of the &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; or multiple &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; objects passed in and returns the value(s) in a convenient manner. You can pass in any number of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; objects, collections of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, arrays of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; and in the cases that you passed in multiple values, join can return either a list or an array, so most of the heavy-lifting should be taken care of by the compiler. Here are some general reminders/pointers regarding the &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; method. All &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods are a blocking methods, so if the value isn't ready, the program just sits there. &amp;lt;insert this is fine dog&amp;gt; Also, all &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods may throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;. You may notice that these are the same errors as are thrown in the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; method of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;...&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
&amp;lt;p&amp;gt;1 down, 26 to go! Don't worry, things will get faster now as there is less to explain.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Indices ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html fork_loop](int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. It takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. Then, when the values return from the branches, the &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; loop bundles them back up in an array, such that the return value of the &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt;th branch is the 0th element in the returned array, and the return value of the &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;th branch is the last element of the returned array. Isn't that handy? This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt;[] updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;So &amp;lt;code&amp;gt;parallel_double(input)[0] = input[0] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[1] = input[1] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[2] = input[2] * 2&amp;lt;/code&amp;gt;, etc. Notice that we did not have to declare the type of &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;. This is because the definition of &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; defines the third parameter, the lambda function, as a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, which we already know takes in a value of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Iterables and Arrays ===&lt;br /&gt;
&amp;lt;p&amp;gt;We will now explore the following versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html List]&amp;lt;[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;&amp;gt; [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Iterable,fj.api.TaskFunction) fork_loop]([https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Iterable.html Iterable]&amp;lt;T&amp;gt; iterable, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Object[],fj.api.TaskFunction) fork_loop](T[] array, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The first version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;List&amp;lt;Future&amp;lt;R&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that takes in a parameter of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;The second version does much the same, except it operates using arrays.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork list example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(List&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt; list) {&lt;br /&gt;
    List&amp;lt;Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[][]&amp;gt;&amp;gt; myFuture = join_fork(list, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[][] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of the generics! The element type of a list is just whatever is within the angled brackets.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork array example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(ScaryType&amp;lt;Integer&amp;gt;[][] array) {&lt;br /&gt;
    Future&amp;lt;ScaryType&amp;lt;Integer&amp;gt;[]&amp;gt;[] myFuture = join_fork(array, (element) -&amp;gt; {&lt;br /&gt;
        /* element is of type ScaryType&amp;lt;Integer&amp;gt;[] */&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;Don't be afraid of multiple arrays. The element of an array is just the type if you ignore the last pair of braces. If you ever forget, ask yourself &amp;quot;What do I get when I index into this array.&amp;quot; That is an element.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;These versions of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; have a lot in common with a for each loop, and thus comes with many of the same programming considerations. If you need to index into an array, this might not be your best choice. But if you are simply iterating through some kind of collection, these could be a great choice.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4491</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4491"/>
		<updated>2023-02-10T06:33:09Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Join ==&lt;br /&gt;
&amp;lt;p&amp;gt;The various &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; functions gets the value of the &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; or multiple &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; objects passed in and returns the value(s) in a convenient manner. You can pass in any number of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; objects, collections of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, arrays of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; and in the cases that you passed in multiple values, join can return either a list or an array, so most of the heavy-lifting should be taken care of by the compiler. Here are some general reminders/pointers regarding the &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; method. All &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods are a blocking methods, so if the value isn't ready, the program just sits there. &amp;lt;insert this is fine dog&amp;gt; Also, all &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods may throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;. You may notice that these are the same errors as are thrown in the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; method of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;...&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
&amp;lt;p&amp;gt;1 down, 26 to go! Don't worry, things will get faster now as there is less to explain.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Indices ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html fork_loop](int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. It takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. Then, when the values return from the branches, the &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; loop bundles them back up in an array, such that the return value of the &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt;th branch is the 0th element in the returned array, and the return value of the &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;th branch is the last element of the returned array. Isn't that handy? This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt;[] updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;So &amp;lt;code&amp;gt;parallel_double(input)[0] = input[0] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[1] = input[1] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[2] = input[2] * 2&amp;lt;/code&amp;gt;, etc. Notice that we did not have to declare the type of &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;. This is because the definition of &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; defines the third parameter, the lambda function, as a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, which we already know takes in a value of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Iterables ===&lt;br /&gt;
&amp;lt;p&amp;gt;The next &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html List]&amp;lt;[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;&amp;gt; [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Iterable,fj.api.TaskFunction) fork_loop]([https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Iterable.html Iterable]&amp;lt;T&amp;gt; iterable, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;List&amp;lt;Future&amp;lt;R&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that takes in a parameter of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(List&amp;lt;Integer&amp;gt; list) {&lt;br /&gt;
    List&amp;lt;Future&amp;lt;Integer&amp;gt;&amp;gt; updatedFuture = join_fork(list, (index) -&amp;gt; {&lt;br /&gt;
        return list[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== Fork-Looping with Arrays ===&lt;br /&gt;
&amp;lt;p&amp;gt;The next &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Object[],fj.api.TaskFunction) fork_loop](T[] array, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in a &amp;lt;code&amp;gt;T[]&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. The &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; is the same one as in the above section.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4490</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4490"/>
		<updated>2023-02-10T06:26:45Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Join ==&lt;br /&gt;
&amp;lt;p&amp;gt;The various &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; functions gets the value of the &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; or multiple &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; objects passed in and returns the value(s) in a convenient manner. You can pass in any number of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; objects, collections of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, arrays of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; and in the cases that you passed in multiple values, join can return either a list or an array, so most of the heavy-lifting should be taken care of by the compiler. Here are some general reminders/pointers regarding the &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; method. All &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods are a blocking methods, so if the value isn't ready, the program just sits there. &amp;lt;insert this is fine dog&amp;gt; Also, all &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; methods may throw &amp;lt;InterruptedException&amp;gt; or &amp;lt;ExecutionException&amp;gt;. You may notice that these are the same errors as are thrown in the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; method of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;...&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
&amp;lt;p&amp;gt;1 down, 26 to go! Don't worry, things will get faster now as there is less to explain.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Forking by Index ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html fork_loop](int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. It takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. Then, when the values return from the branches, the &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; loop bundles them back up in an array, such that the return value of the &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt;th branch is the 0th element in the returned array, and the return value of the &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;th branch is the last element of the returned array. Isn't that handy? This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;So &amp;lt;code&amp;gt;parallel_double(input)[0] = input[0] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[1] = input[1] * 2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;parallel_double(input)[2] = input[2] * 2&amp;lt;/code&amp;gt;, etc. Notice that we did not have to declare the type of &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;. This is because the definition of &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; defines the third parameter, the lambda function, as a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;, which we already know takes in a value of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Forking with Iterables ===&lt;br /&gt;
&amp;lt;p&amp;gt;The next &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;T,R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html List]&amp;lt;[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;&amp;gt; [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork_loop(java.lang.Iterable,fj.api.TaskFunction) fork_loop]([https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Iterable.html Iterable]&amp;lt;T&amp;gt; iterable, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html TaskFunction]&amp;lt;T,R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;List&amp;lt;Future&amp;lt;R&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;Futures&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. It takes in an &amp;lt;code&amp;gt;Iterable&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; and a &amp;lt;code&amp;gt;TaskFunction&amp;lt;T,R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskFunction&amp;lt;T, R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that takes in a parameter of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public List&amp;lt;Integer&amp;gt; parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4489</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4489"/>
		<updated>2023-02-10T05:47:19Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you! In fact, this entire library is static, so you should just be calling these functions by their function name.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Generally, in this course, we use &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when referring to the type of the data that is passed into the parallel task, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; when referring to the type of the data that is returned by the parallel task.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== Fork Loop ==&lt;br /&gt;
1 down, 32 to go! Don't worry, things will get faster now as there is less to explain.&lt;br /&gt;
=== Forking by Index ===&lt;br /&gt;
&amp;lt;p&amp;gt;The first &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; we are going to explore has the following signature:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;public static &amp;lt;R&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future]&amp;lt;R&amp;gt;[] fork_loop(int min, int maxExclusive, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskIntFunction.html TaskIntFunction]&amp;lt;R&amp;gt; function)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;So this version of &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; takes in two integers, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;, then a &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. The function as a whole returns a, &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;[]&amp;lt;/code&amp;gt;, an array of &amp;lt;code&amp;gt;Future&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt;. Following the link for the &amp;lt;code&amp;gt;TaskIntFunction&amp;lt;R&amp;gt;&amp;lt;/code&amp;gt; reveals that it is a function that has one parameter of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; and returns a value of type &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Under the hood, the &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; function creates one fork, one parallel branch, for each integer in the range &amp;lt;code&amp;gt;[min, maxExclusive)&amp;lt;/code&amp;gt;, and passes to each branch its corresponding integer. This &amp;lt;code&amp;gt;join_fork&amp;lt;/code&amp;gt; function is very similar to a &amp;quot;standard&amp;quot; for loop &amp;lt;code&amp;gt;for(int i = min; i &amp;lt; maxExclusive; i++)&amp;lt;/code&amp;gt;, just parallel. An example use case of this &amp;lt;code&amp;gt;fork_loop&amp;lt;/code&amp;gt; is as follows:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|join_fork example &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int[] parallel_double(int[] array) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; updatedFuture = join_fork(0, array.length, (index) -&amp;gt; {&lt;br /&gt;
        return array[index] * 2;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return join(updatedFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4488</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4488"/>
		<updated>2023-02-10T05:22:38Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner. There is an assumption that you have attended class and watched the lectures, so the terms are familiar, but there is trouble knowing what everything means/does.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you!&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and itself returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process. Thus, code in the following structure leads to a parallel program:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Fork template &amp;amp;nbsp;|&amp;lt;pre&amp;gt;// Parallel task and Main task will run in parallel to one another&lt;br /&gt;
Future&amp;lt;SomeType&amp;gt; myFuture = fork(() -&amp;gt; {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
});&lt;br /&gt;
/* Main task */&lt;br /&gt;
SomeType myValue = join(myFuture);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4487</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4487"/>
		<updated>2023-02-10T05:18:13Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
=== Reading the Documentation ===&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public static &amp;lt;T&amp;gt; [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/Future.html Future&amp;lt;T&amp;gt;] [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) fork]([https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#fork(fj.api.TaskSupplier) TaskSupplier&amp;lt;T&amp;gt;] task)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;What does this mean? Let's break it down&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Static ====&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you!&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lambda Functions ====&lt;br /&gt;
&amp;lt;p&amp;gt;We see that &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; takes in one parameter of type &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. If we follow the link, we see that a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; is just a single function, which has a return type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and can throw &amp;lt;code&amp;gt;InterruptedException&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ExecutionException&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;T [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskSupplier.html#get() get()] throws [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/InterruptedException.html InterruptedException],[https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/concurrent/ExecutionException.html ExecutionException]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Thus, any function that takes in no parameters and returns anything qualifies as a &amp;lt;code&amp;gt;TaskSupplier&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. In code for this course, you will often see code such as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Lambda function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; {&lt;br /&gt;
        /* Parallel task */&lt;br /&gt;
    });&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;This is the same as:&amp;lt;/p&amp;gt;&lt;br /&gt;
{{CollapsibleCode|Explicit function &amp;amp;nbsp;|&amp;lt;pre&amp;gt;public int myFunc() {&lt;br /&gt;
    /* Parallel task */&lt;br /&gt;
}&lt;br /&gt;
public int count(/* parameters */) {&lt;br /&gt;
    Future&amp;lt;Integer&amp;gt; firstHalfFuture = fork(() -&amp;gt; myFunc());&lt;br /&gt;
    /* Main task */&lt;br /&gt;
    int firstHalf = join(firstHalfFuture);&lt;br /&gt;
    /* more code */&lt;br /&gt;
}&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&amp;lt;p&amp;gt;In the first case, we defined the function inline at the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; method call, while the second case is more akin to what you may be used to&amp;lt;/p&amp;gt;&lt;br /&gt;
==== Generics ====&lt;br /&gt;
&amp;lt;p&amp;gt;You may have the question, what is a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;? &amp;lt;code&amp;gt;&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; means that we have a generic type called &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; isn't any one type in particular, but instead is just a guarantee that in the following code, whenever we see &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, it is referring to the same type. An example you may be more familiar with is the &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt; class. If we look at the [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html documentation] for &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, we see, among other things:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public class [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ArrayList&amp;lt;E&amp;gt;]&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public boolean [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E- add](E e)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;public E [https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int- get](int index)&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;In the case of &amp;lt;code&amp;gt;ArrayList&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is the name of the generic type. And thus, everywhere we see &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt;, it is the same type &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; as was supplied to the class constructor. That is why when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;Integer&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;, but not some other type such as &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;. Then, when you create an &amp;lt;code&amp;gt;ArrayList&amp;lt;String&amp;gt;&amp;lt;/code&amp;gt;, you are able to use it for &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;, but not other types such as &amp;lt;code&amp;gt;Integer&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Coming back to the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function, we next see that the return type is &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt;. We can follow the link to the java documentation for &amp;lt;code&amp;gt;Future&amp;lt;/code&amp;gt; to see that when we call &amp;lt;code&amp;gt;Future.get()&amp;lt;/code&amp;gt;, it will return a value of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Practical Explanation ====&lt;br /&gt;
&amp;lt;p&amp;gt;So putting all of this together, the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is a static function that is tied to the &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; class, and not any specific instance of that class. It takes in a function that returns any type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;, and returns a &amp;lt;code&amp;gt;Future&amp;lt;T&amp;gt;&amp;lt;/code&amp;gt; object, which will return a value of that same type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; when the &amp;lt;code&amp;gt;get&amp;lt;/code&amp;gt; function is called on it. The function that is passed into the &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; function is run in parallel to the main process.&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:TaskSupplier-T-_get.png&amp;diff=4479</id>
		<title>File:TaskSupplier-T- get.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:TaskSupplier-T-_get.png&amp;diff=4479"/>
		<updated>2023-02-10T04:44:07Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_get.png&amp;diff=4475</id>
		<title>File:ArrayList get.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_get.png&amp;diff=4475"/>
		<updated>2023-02-10T04:22:49Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: KaiAng uploaded a new version of File:ArrayList get.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_get.png&amp;diff=4472</id>
		<title>File:ArrayList get.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_get.png&amp;diff=4472"/>
		<updated>2023-02-10T04:19:47Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_add.png&amp;diff=4471</id>
		<title>File:ArrayList add.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_add.png&amp;diff=4471"/>
		<updated>2023-02-10T04:17:31Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: The documentation for ArrayList.add()&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
The documentation for ArrayList.add()&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_class_definition.png&amp;diff=4470</id>
		<title>File:ArrayList class definition.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:ArrayList_class_definition.png&amp;diff=4470"/>
		<updated>2023-02-10T04:16:10Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: The class definition of ArrayList&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
The class definition of ArrayList&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4468</id>
		<title>Understanding Fork Join</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Understanding_Fork_Join&amp;diff=4468"/>
		<updated>2023-02-10T04:11:03Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: Created page with &amp;quot;== Overview == This page will walk you through reading documentation and understanding what the functions do in a practical manner.  == Fork == &amp;lt;p&amp;gt;When we look at the docs, we...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
This page will walk you through reading documentation and understanding what the functions do in a practical manner.&lt;br /&gt;
&lt;br /&gt;
== Fork ==&lt;br /&gt;
&amp;lt;p&amp;gt;When we look at the docs, we are greeted with the following:&amp;lt;/p&amp;gt;&lt;br /&gt;
[[File:Fork docs.png]]&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; means that the function is related to the class, &amp;lt;code&amp;gt;FJ&amp;lt;/code&amp;gt; as opposed to a specific instance of that class. This means that we can call &amp;lt;code&amp;gt;FJ.fork&amp;lt;/code&amp;gt; directly, we don't need to create an instance of that class to do so. In our code, we typically just write &amp;lt;code&amp;gt;fork&amp;lt;/code&amp;gt; because Professor Cosgrove has imported the appropriate dependencies for you!&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:Fork_docs.png&amp;diff=4467</id>
		<title>File:Fork docs.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:Fork_docs.png&amp;diff=4467"/>
		<updated>2023-02-10T04:06:41Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: KaiAng uploaded a new version of File:Fork docs.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
A screenshot of the docs for the fork function&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:Fork_docs.png&amp;diff=4466</id>
		<title>File:Fork docs.png</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=File:Fork_docs.png&amp;diff=4466"/>
		<updated>2023-02-10T04:06:04Z</updated>

		<summary type="html">&lt;p&gt;KaiAng: A screenshot of the docs for the fork function&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
A screenshot of the docs for the fork function&lt;/div&gt;</summary>
		<author><name>KaiAng</name></author>
		
	</entry>
</feed>