HW2: done Ex2

This commit is contained in:
Claudio Maggioni 2019-11-10 23:17:28 +01:00
parent c80deb247a
commit 171a7aa764
10 changed files with 536 additions and 36 deletions

View File

@ -2,9 +2,13 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b727764e-c290-4ab7-a944-02a848fa4a41" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/bintree/BinTreeFullSync.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../hw1/Ex1/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../hw1/Ex1/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../hw1/Ex2/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../hw1/Ex2/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../Ex2/.idea/Ex2.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../Ex2/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../Ex2/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../Ex2/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../Ex2/src/ConcurrentVoteCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../submission.tex" beforeDir="false" afterPath="$PROJECT_DIR$/../submission.tex" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -20,8 +24,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/bintree/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="495">
<caret line="33" column="13" lean-forward="true" selection-start-line="33" selection-start-column="13" selection-end-line="33" selection-end-column="13" />
<state>
<caret lean-forward="true" selection-end-line="97" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
@ -42,7 +46,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeCAS.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="705">
<state relative-caret-position="720">
<caret line="48" selection-start-line="30" selection-end-line="48" />
<folding>
<element signature="imports" expanded="true" />
@ -55,7 +59,7 @@
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="17" lean-forward="true" selection-start-line="17" selection-end-line="17" />
<caret line="17" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
@ -73,7 +77,7 @@
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeSimple.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="825">
<caret line="55" column="5" lean-forward="true" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
<caret line="55" column="5" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
</state>
</provider>
</entry>
@ -121,8 +125,6 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="PackagesPane" />
<pane id="ProjectPane">
<subPane>
<expand>
@ -145,10 +147,13 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
@ -208,18 +213,19 @@
<option name="presentableId" value="Default" />
<updated>1573115638539</updated>
<workItem from="1573115639742" duration="3855000" />
<workItem from="1573398260802" duration="3267000" />
<workItem from="1573398260802" duration="3728000" />
<workItem from="1573416058033" duration="28000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="7122000" />
<option name="totallyTimeSpent" value="7611000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1680" height="1050" extended-state="0" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.22039072" />
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.22283272" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Designer" order="2" />
<window_info id="Image Layers" order="3" />
@ -228,8 +234,8 @@
<window_info id="Favorites" order="6" side_tool="true" />
<window_info id="Learn" order="7" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32959184" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info active="true" anchor="bottom" id="Find" order="1" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Run" order="2" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -254,7 +260,30 @@
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/bintree/Tester.java</url>
<line>52</line>
<properties />
<option name="timeStamp" value="3" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/bintree/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state>
<caret lean-forward="true" selection-end-line="97" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTree.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
@ -264,7 +293,7 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeCAS.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="705">
<state relative-caret-position="720">
<caret line="48" selection-start-line="30" selection-end-line="48" />
<folding>
<element signature="imports" expanded="true" />
@ -272,6 +301,13 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="17" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSync.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
@ -282,25 +318,7 @@
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeSimple.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="825">
<caret line="55" column="5" lean-forward="true" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/BinTreeFullSyncEdited.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="17" lean-forward="true" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/bintree/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="495">
<caret line="33" column="13" lean-forward="true" selection-start-line="33" selection-start-column="13" selection-end-line="33" selection-end-column="13" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
<caret line="55" column="5" selection-start-line="55" selection-start-column="5" selection-end-line="55" selection-end-column="5" />
</state>
</provider>
</entry>

11
hw2/Ex2/.idea/Ex2.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

9
hw2/Ex2/.idea/misc.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_12" default="false" project-jdk-name="12.0.1" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Ex2.iml" filepath="$PROJECT_DIR$/.idea/Ex2.iml" />
</modules>
</component>
</project>

6
hw2/Ex2/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

319
hw2/Ex2/.idea/workspace.xml Normal file
View File

@ -0,0 +1,319 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="19fb80a5-3326-4689-b0ba-ecd6ab32d599" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/Ex2.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/ConcurrentVoteCounter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Tester.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/VoteCounter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../Ex1/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../Ex1/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../submission.tex" beforeDir="false" afterPath="$PROJECT_DIR$/../submission.tex" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DefaultGradleProjectSettings">
<option name="isMigrated" value="true" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#454#455#0" expanded="true" />
<element signature="e#500#501#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="14" selection-start-line="2" selection-start-column="14" selection-end-line="2" selection-end-column="14" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/VoteCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="4" selection-start-line="2" selection-start-column="4" selection-end-line="2" selection-end-column="4" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/VoteCounter.java" />
<option value="$PROJECT_DIR$/src/Test.java" />
<option value="$PROJECT_DIR$/src/ConcurrentVoteCounter.java" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" fullScreen="true">
<option name="width" value="1680" />
<option name="height" value="1050" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Ex2" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex2" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="Ex2" type="b2602c69:ProjectViewProjectNode" />
<item name="Ex2" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Tester" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Tester" />
<module name="Ex2" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Tester" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="19fb80a5-3326-4689-b0ba-ecd6ab32d599" name="Default Changelist" comment="" />
<created>1573415904376</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1573415904376</updated>
<workItem from="1573415905557" duration="151000" />
<workItem from="1573416086677" duration="1451000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="1602000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1680" height="1050" extended-state="0" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.25274727" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Designer" order="2" />
<window_info id="Image Layers" order="3" />
<window_info id="Capture Tool" order="4" />
<window_info id="UI Designer" order="5" />
<window_info id="Learn" order="6" />
<window_info id="Favorites" order="7" side_tool="true" />
<window_info anchor="bottom" id="Messages" weight="0.32959184" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32959184" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Database Changes" order="9" />
<window_info anchor="bottom" id="Terminal" order="10" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Theme Preview" order="4" />
<window_info anchor="right" id="Maven" order="5" />
<window_info anchor="right" id="Capture Analysis" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
<window_info anchor="right" id="Database" order="8" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/VoteCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="4" selection-start-line="2" selection-start-column="4" selection-end-line="2" selection-end-column="4" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Tester.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="14" selection-start-line="2" selection-start-column="14" selection-end-line="2" selection-end-column="14" />
<folding>
<element signature="e#441#442#0" expanded="true" />
<element signature="e#478#479#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/ConcurrentVoteCounter.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="11" column="64" selection-start-line="11" selection-start-column="64" selection-end-line="11" selection-end-column="64" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#454#455#0" expanded="true" />
<element signature="e#500#501#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>Ex2</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
<option value="0.6" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

@ -0,0 +1,20 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
public class ConcurrentVoteCounter implements VoteCounter {
private final ConcurrentMap<Integer, AtomicInteger> electionResults = new ConcurrentHashMap<>();
public void addVote(Integer id) {
// Alternative implementation (using plain Integers, slower due to synchronization costs):
// electionResults.compute(id, (k, v) -> v != null ? v + 1 : 1);
// Faster version due to architecture-level optimization
electionResults.putIfAbsent(id, new AtomicInteger(0));
electionResults.get(id).addAndGet(1);
}
public Integer getVoteCount(Integer id) {
return electionResults.get(id).intValue();
}
}

93
hw2/Ex2/src/Tester.java Normal file
View File

@ -0,0 +1,93 @@
import java.util.concurrent.CountDownLatch;
public class Tester {
private static final int NUM_THREADS = 1024;
private static final int NUM_ITERATIONS = 64;
private static final int OCCUR = 32;
private final VoteCounter counter;
private final CountDownLatch threadsReady,
measurementStarted, threadsCompleted;
private class TesterThread extends Thread {
private final int startValue;
TesterThread(int startValue) {
this.startValue = startValue;
}
public void run() {
int iter = NUM_ITERATIONS * OCCUR;
int nextValue = startValue;
threadsReady.countDown();
try {
measurementStarted.await();
}
catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
for (int i = 0; i < iter; i++) {
counter.addVote(nextValue);
nextValue = (nextValue + 1) % NUM_ITERATIONS;
}
threadsCompleted.countDown();
}
}
public Tester(VoteCounter counter) {
this.counter = counter;
this.threadsReady = new CountDownLatch(NUM_THREADS);
this.measurementStarted = new CountDownLatch(1);
this.threadsCompleted = new CountDownLatch(NUM_THREADS);
}
private long runMeasurement() throws InterruptedException {
for (int i = 0; i < NUM_THREADS; ++i) {
Thread t = new TesterThread( NUM_ITERATIONS / 2);
t.start();
}
threadsReady.await();
long timeBegin = System.nanoTime();
measurementStarted.countDown();
threadsCompleted.await();
long timeEnd = System.nanoTime();
return timeEnd - timeBegin;
}
private void check() {
int expectedOccur = OCCUR * NUM_THREADS;
for (int i = 0; i < NUM_ITERATIONS; i++) {
int foundOccur;
if ((foundOccur = counter.getVoteCount(i)) != expectedOccur) {
System.err.printf(" - ERROR: Expected: %d ; Found: %d\n",
expectedOccur, foundOccur);
}
}
}
private static void testImplementation(ConcurrentVoteCounter implementation,
String name) throws Exception {
System.out.println("Results for implementation: " + name);
Tester tester = new Tester(implementation);
long elapsedTime = tester.runMeasurement();
tester.check();
System.out.printf(" - Test completed, elapsed time: %d ms\n",
(elapsedTime / 1000000));
}
public static void main(String[] args) throws Exception {
//testImplementation(new BinTreeSimple(), "Simple version");
testImplementation(new ConcurrentVoteCounter(), "Concurrent version (correct)");
}
}

View File

@ -0,0 +1,4 @@
public interface VoteCounter {
void addVote(Integer id);
Integer getVoteCount(Integer id);
}

View File

@ -18,6 +18,18 @@ I have implemented the class \texttt{BinTreeFullSync}
as a thread-safe alternative of \texttt{BinTreeSimple} where the only code modification between the two were the addition of \texttt{synchronized} keywords on already existing threads. Since I was not sure how to interpret this specific question, I have implemented \texttt{BinTreeFullSyncEdited}, a thread-safe refactoring of \texttt{BinTreeSimple} where the only synchronization mechanism is method-level \texttt{synchronized} blocks, which are used on new methods created in the subclass \texttt{Node}.
\subsection{Question 5}
\texttt{BinTreeFullSync} has the worst performance of all 3 implementations since it allows only sequential access to the tree structure. \texttt{BinTreeFullSyncEdited} is slightly faster, but \texttt{BinTreeCAS} is the fastest since the first one is a \texttt{synchronized} block imitation of the second one, which is instead based on \texttt{Atomic*} objects and can use the extra speed provided by architecture-based optimization.
\texttt{BinTreeFullSync} has the worst performance of all 3 implementations since it allows only sequential access to the tree structure. \texttt{BinTreeFullSyncEdited} is slightly faster, but \texttt{BinTreeCAS} is the fastest since the first one is a \texttt{synchronized} block imitation of the second one, which is instead based on \texttt{Atomic*} objects and can use the extra speed provided by architecture based optimization.
\section{Exercise 2}
\subsection{Question 1}
\texttt{SimpleVoteCounter} behaves incorrectly since its implementation is not thread-safe: the \texttt{Map} implementation is not thread-safe, and the \texttt{addVote(Integer)} method is also not thread safe since its operation of access, conditional computation and storage is not atomic nor synchronized.
\subsection{Question 2}
\texttt{SyncMapVoteCounter} is still not thread safe since \texttt{addVote(Integer)} is still not synchronized. This means that two threads may assess that a candidate has no vote at almost the same time and count the first vote twice.
\subsection{Question 3}
\texttt{LockVoteCounter} is thread safe since only a thread at a time can either add a vote or get the votes for a candidate. However, this type of synchronization is inefficient since concurrent accesses to the number of votes of a candidate are safe if no other thread is casting a vote.
\subsection{Question 4}
As with \texttt{SyncMapVoteCounter}, \texttt{ConcurrentVoteCounter} is not thread safe since the operation of casting a vote is not atomic nor synchronized.
\end{document}