Assignment1: done Ex1 1-6
This commit is contained in:
commit
a483b4b08b
31 changed files with 1709 additions and 0 deletions
344
hw1/.gitignore
vendored
Normal file
344
hw1/.gitignore
vendored
Normal file
|
@ -0,0 +1,344 @@
|
||||||
|
*.pdf
|
||||||
|
|
||||||
|
## Core latex/pdflatex auxiliary files:
|
||||||
|
*.aux
|
||||||
|
*.lof
|
||||||
|
*.log
|
||||||
|
*.lot
|
||||||
|
*.fls
|
||||||
|
*.out
|
||||||
|
*.toc
|
||||||
|
*.fmt
|
||||||
|
*.fot
|
||||||
|
*.cb
|
||||||
|
*.cb2
|
||||||
|
.*.lb
|
||||||
|
|
||||||
|
## Intermediate documents:
|
||||||
|
*.dvi
|
||||||
|
*.xdv
|
||||||
|
*-converted-to.*
|
||||||
|
# these rules might exclude image files for figures etc.
|
||||||
|
# *.ps
|
||||||
|
# *.eps
|
||||||
|
# *.pdf
|
||||||
|
|
||||||
|
## Generated if empty string is given at "Please type another file name for output:"
|
||||||
|
.pdf
|
||||||
|
|
||||||
|
## Bibliography auxiliary files (bibtex/biblatex/biber):
|
||||||
|
*.bbl
|
||||||
|
*.bcf
|
||||||
|
*.blg
|
||||||
|
*-blx.aux
|
||||||
|
*-blx.bib
|
||||||
|
*.run.xml
|
||||||
|
|
||||||
|
## Build tool auxiliary files:
|
||||||
|
*.fdb_latexmk
|
||||||
|
*.synctex
|
||||||
|
*.synctex(busy)
|
||||||
|
*.synctex.gz
|
||||||
|
*.synctex.gz(busy)
|
||||||
|
*.pdfsync
|
||||||
|
|
||||||
|
## Build tool directories for auxiliary files
|
||||||
|
# latexrun
|
||||||
|
latex.out/
|
||||||
|
|
||||||
|
## Auxiliary and intermediate files from other packages:
|
||||||
|
# algorithms
|
||||||
|
*.alg
|
||||||
|
*.loa
|
||||||
|
|
||||||
|
# achemso
|
||||||
|
acs-*.bib
|
||||||
|
|
||||||
|
# amsthm
|
||||||
|
*.thm
|
||||||
|
|
||||||
|
# beamer
|
||||||
|
*.nav
|
||||||
|
*.pre
|
||||||
|
*.snm
|
||||||
|
*.vrb
|
||||||
|
|
||||||
|
# changes
|
||||||
|
*.soc
|
||||||
|
|
||||||
|
# comment
|
||||||
|
*.cut
|
||||||
|
|
||||||
|
# cprotect
|
||||||
|
*.cpt
|
||||||
|
|
||||||
|
# elsarticle (documentclass of Elsevier journals)
|
||||||
|
*.spl
|
||||||
|
|
||||||
|
# endnotes
|
||||||
|
*.ent
|
||||||
|
|
||||||
|
# fixme
|
||||||
|
*.lox
|
||||||
|
|
||||||
|
# feynmf/feynmp
|
||||||
|
*.mf
|
||||||
|
*.mp
|
||||||
|
*.t[1-9]
|
||||||
|
*.t[1-9][0-9]
|
||||||
|
*.tfm
|
||||||
|
|
||||||
|
#(r)(e)ledmac/(r)(e)ledpar
|
||||||
|
*.end
|
||||||
|
*.?end
|
||||||
|
*.[1-9]
|
||||||
|
*.[1-9][0-9]
|
||||||
|
*.[1-9][0-9][0-9]
|
||||||
|
*.[1-9]R
|
||||||
|
*.[1-9][0-9]R
|
||||||
|
*.[1-9][0-9][0-9]R
|
||||||
|
*.eledsec[1-9]
|
||||||
|
*.eledsec[1-9]R
|
||||||
|
*.eledsec[1-9][0-9]
|
||||||
|
*.eledsec[1-9][0-9]R
|
||||||
|
*.eledsec[1-9][0-9][0-9]
|
||||||
|
*.eledsec[1-9][0-9][0-9]R
|
||||||
|
|
||||||
|
# glossaries
|
||||||
|
*.acn
|
||||||
|
*.acr
|
||||||
|
*.glg
|
||||||
|
*.glo
|
||||||
|
*.gls
|
||||||
|
*.glsdefs
|
||||||
|
*.lzo
|
||||||
|
*.lzs
|
||||||
|
|
||||||
|
# uncomment this for glossaries-extra (will ignore makeindex's style files!)
|
||||||
|
# *.ist
|
||||||
|
|
||||||
|
# gnuplottex
|
||||||
|
*-gnuplottex-*
|
||||||
|
|
||||||
|
# gregoriotex
|
||||||
|
*.gaux
|
||||||
|
*.gtex
|
||||||
|
|
||||||
|
# htlatex
|
||||||
|
*.4ct
|
||||||
|
*.4tc
|
||||||
|
*.idv
|
||||||
|
*.lg
|
||||||
|
*.trc
|
||||||
|
*.xref
|
||||||
|
|
||||||
|
# hyperref
|
||||||
|
*.brf
|
||||||
|
|
||||||
|
# knitr
|
||||||
|
*-concordance.tex
|
||||||
|
# TODO Comment the next line if you want to keep your tikz graphics files
|
||||||
|
*.tikz
|
||||||
|
*-tikzDictionary
|
||||||
|
|
||||||
|
# listings
|
||||||
|
*.lol
|
||||||
|
|
||||||
|
# luatexja-ruby
|
||||||
|
*.ltjruby
|
||||||
|
|
||||||
|
# makeidx
|
||||||
|
*.idx
|
||||||
|
*.ilg
|
||||||
|
*.ind
|
||||||
|
|
||||||
|
# minitoc
|
||||||
|
*.maf
|
||||||
|
*.mlf
|
||||||
|
*.mlt
|
||||||
|
*.mtc[0-9]*
|
||||||
|
*.slf[0-9]*
|
||||||
|
*.slt[0-9]*
|
||||||
|
*.stc[0-9]*
|
||||||
|
|
||||||
|
# minted
|
||||||
|
_minted*
|
||||||
|
*.pyg
|
||||||
|
|
||||||
|
# morewrites
|
||||||
|
*.mw
|
||||||
|
|
||||||
|
# nomencl
|
||||||
|
*.nlg
|
||||||
|
*.nlo
|
||||||
|
*.nls
|
||||||
|
|
||||||
|
# pax
|
||||||
|
*.pax
|
||||||
|
|
||||||
|
# pdfpcnotes
|
||||||
|
*.pdfpc
|
||||||
|
|
||||||
|
# sagetex
|
||||||
|
*.sagetex.sage
|
||||||
|
*.sagetex.py
|
||||||
|
*.sagetex.scmd
|
||||||
|
|
||||||
|
# scrwfile
|
||||||
|
*.wrt
|
||||||
|
|
||||||
|
# sympy
|
||||||
|
*.sout
|
||||||
|
*.sympy
|
||||||
|
sympy-plots-for-*.tex/
|
||||||
|
|
||||||
|
# pdfcomment
|
||||||
|
*.upa
|
||||||
|
*.upb
|
||||||
|
|
||||||
|
# pythontex
|
||||||
|
*.pytxcode
|
||||||
|
pythontex-files-*/
|
||||||
|
|
||||||
|
# tcolorbox
|
||||||
|
*.listing
|
||||||
|
|
||||||
|
# thmtools
|
||||||
|
*.loe
|
||||||
|
|
||||||
|
# TikZ & PGF
|
||||||
|
*.dpth
|
||||||
|
*.md5
|
||||||
|
*.auxlock
|
||||||
|
|
||||||
|
# todonotes
|
||||||
|
*.tdo
|
||||||
|
|
||||||
|
# vhistory
|
||||||
|
*.hst
|
||||||
|
*.ver
|
||||||
|
|
||||||
|
# easy-todo
|
||||||
|
*.lod
|
||||||
|
|
||||||
|
# xcolor
|
||||||
|
*.xcp
|
||||||
|
|
||||||
|
# xmpincl
|
||||||
|
*.xmpi
|
||||||
|
|
||||||
|
# xindy
|
||||||
|
*.xdy
|
||||||
|
|
||||||
|
# xypic precompiled matrices and outlines
|
||||||
|
*.xyc
|
||||||
|
*.xyd
|
||||||
|
|
||||||
|
# endfloat
|
||||||
|
*.ttt
|
||||||
|
*.fff
|
||||||
|
|
||||||
|
# Latexian
|
||||||
|
TSWLatexianTemp*
|
||||||
|
|
||||||
|
## Editors:
|
||||||
|
# WinEdt
|
||||||
|
*.bak
|
||||||
|
*.sav
|
||||||
|
|
||||||
|
# Texpad
|
||||||
|
.texpadtmp
|
||||||
|
|
||||||
|
# LyX
|
||||||
|
*.lyx~
|
||||||
|
|
||||||
|
# Kile
|
||||||
|
*.backup
|
||||||
|
|
||||||
|
# gummi
|
||||||
|
.*.swp
|
||||||
|
|
||||||
|
# KBibTeX
|
||||||
|
*~[0-9]*
|
||||||
|
|
||||||
|
# auto folder when using emacs and auctex
|
||||||
|
./auto/*
|
||||||
|
*.el
|
||||||
|
|
||||||
|
# expex forward references with \gathertags
|
||||||
|
*-tags.tex
|
||||||
|
|
||||||
|
# standalone packages
|
||||||
|
*.sta
|
||||||
|
|
||||||
|
# Makeindex log files
|
||||||
|
*.lpz
|
||||||
|
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff
|
||||||
|
.idea/**/workspace.xml
|
||||||
|
.idea/**/tasks.xml
|
||||||
|
.idea/**/usage.statistics.xml
|
||||||
|
.idea/**/dictionaries
|
||||||
|
.idea/**/shelf
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
.idea/**/contentModel.xml
|
||||||
|
|
||||||
|
# Sensitive or high-churn files
|
||||||
|
.idea/**/dataSources/
|
||||||
|
.idea/**/dataSources.ids
|
||||||
|
.idea/**/dataSources.local.xml
|
||||||
|
.idea/**/sqlDataSources.xml
|
||||||
|
.idea/**/dynamic.xml
|
||||||
|
.idea/**/uiDesigner.xml
|
||||||
|
.idea/**/dbnavigator.xml
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.idea/**/gradle.xml
|
||||||
|
.idea/**/libraries
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
cmake-build-*/
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
.idea/**/mongoSettings.xml
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
.idea/replstate.xml
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
.idea/httpRequests
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
.idea/caches/build_file_checksums.ser
|
12
hw1/Ex1/.idea/Ex1.iml
Normal file
12
hw1/Ex1/.idea/Ex1.iml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?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" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/out" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
11
hw1/Ex1/.idea/inspectionProfiles/Project_Default.xml
Normal file
11
hw1/Ex1/.idea/inspectionProfiles/Project_Default.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Duplicates" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<Java>
|
||||||
|
<minSize>83</minSize>
|
||||||
|
<isEnabled>true</isEnabled>
|
||||||
|
</Java>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
9
hw1/Ex1/.idea/misc.xml
Normal file
9
hw1/Ex1/.idea/misc.xml
Normal 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>
|
8
hw1/Ex1/.idea/modules.xml
Normal file
8
hw1/Ex1/.idea/modules.xml
Normal 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/Ex1.iml" filepath="$PROJECT_DIR$/.idea/Ex1.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
hw1/Ex1/.idea/vcs.xml
Normal file
6
hw1/Ex1/.idea/vcs.xml
Normal 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>
|
464
hw1/Ex1/.idea/workspace.xml
Normal file
464
hw1/Ex1/.idea/workspace.xml
Normal file
|
@ -0,0 +1,464 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="d712734b-6034-468c-b553-7e4d8f6128c3" name="Default Changelist" comment="">
|
||||||
|
<change afterPath="$PROJECT_DIR$/.idea/Ex1.iml" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" 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" />
|
||||||
|
</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="testRunner" value="GRADLE" />
|
||||||
|
<option name="delegatedBuild" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="FileEditorManager">
|
||||||
|
<leaf>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/sequential/SequentialSum.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="180">
|
||||||
|
<caret line="12" column="30" selection-start-line="12" selection-start-column="30" selection-end-line="12" selection-end-column="30" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/synchronized1/MultipleUpdatesPerThreadSynch.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="317">
|
||||||
|
<caret line="28" column="9" selection-start-line="19" selection-start-column="8" selection-end-line="28" selection-end-column="9" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#301#302#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/simple1/MultipleUpdatesPerThread.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="75">
|
||||||
|
<caret line="13" column="18" selection-start-line="13" selection-start-column="18" selection-end-line="13" selection-end-column="18" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="true">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/atomic1/MultipleUpdatesPerThreadAtomic.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="450">
|
||||||
|
<caret line="31" lean-forward="true" selection-start-line="31" selection-end-line="31" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#312#313#0" expanded="true" />
|
||||||
|
<element signature="e#344#345#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/test/Test.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="175">
|
||||||
|
<caret line="61" column="8" lean-forward="true" selection-start-line="61" selection-start-column="8" selection-end-line="61" selection-end-column="8" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/parallel/CollectingResults.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="30">
|
||||||
|
<caret line="2" column="17" lean-forward="true" selection-start-line="2" selection-start-column="17" selection-end-line="2" selection-end-column="17" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/simple2/SingleUpdatePerThread.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="285">
|
||||||
|
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/synchronized2/SingleUpdatePerThreadSynch.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="180">
|
||||||
|
<caret line="12" column="31" selection-start-line="12" selection-start-column="31" selection-end-line="12" selection-end-column="31" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file pinned="false" current-in-tab="false">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/atomic2/SingleUpdatePerThreadAtomic.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="175">
|
||||||
|
<caret line="29" column="8" selection-start-line="29" selection-start-column="8" selection-end-line="29" selection-end-column="8" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#300#301#0" expanded="true" />
|
||||||
|
<element signature="e#324#325#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
</leaf>
|
||||||
|
</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/synchronized1/MultipleUpdatesPerThreadSynch.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/simple1/MultipleUpdatesPerThread.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/synchronized2/SingleUpdatePerThreadSynch.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/atomic1/MultipleUpdatesPerThreadAtomic.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/atomic2/SingleUpdatePerThreadAtomic.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/parallel/CollectingResults.java" />
|
||||||
|
<option value="$PROJECT_DIR$/src/test/Test.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="PackagesPane" />
|
||||||
|
<pane id="Scope" />
|
||||||
|
<pane id="ProjectPane">
|
||||||
|
<subPane>
|
||||||
|
<expand>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="atomic1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="atomic2" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="parallel" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="sequential" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="simple1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="simple2" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="synchronized1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="synchronized2" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<item name="Ex1" type="b2602c69:ProjectViewProjectNode" />
|
||||||
|
<item name="Ex1" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||||
|
<item name="test" type="462c0819:PsiDirectoryNode" />
|
||||||
|
</path>
|
||||||
|
</expand>
|
||||||
|
<select />
|
||||||
|
</subPane>
|
||||||
|
</pane>
|
||||||
|
</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="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="Project" />
|
||||||
|
<property name="project.structure.proportion" value="0.15" />
|
||||||
|
<property name="project.structure.side.proportion" value="0.2" />
|
||||||
|
</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="Test" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||||
|
<option name="MAIN_CLASS_NAME" value="test.Test" />
|
||||||
|
<module name="Ex1" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="test.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
<recent_temporary>
|
||||||
|
<list>
|
||||||
|
<item itemvalue="Application.Test" />
|
||||||
|
</list>
|
||||||
|
</recent_temporary>
|
||||||
|
</component>
|
||||||
|
<component name="SvnConfiguration">
|
||||||
|
<configuration />
|
||||||
|
</component>
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="d712734b-6034-468c-b553-7e4d8f6128c3" name="Default Changelist" comment="" />
|
||||||
|
<created>1570613444806</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1570613444806</updated>
|
||||||
|
<workItem from="1570613448664" duration="3062000" />
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
<component name="TimeTrackingManager">
|
||||||
|
<option name="totallyTimeSpent" value="3062000" />
|
||||||
|
</component>
|
||||||
|
<component name="ToolWindowManager">
|
||||||
|
<frame x="0" y="0" width="1059" height="1050" extended-state="0" />
|
||||||
|
<layout>
|
||||||
|
<window_info id="Designer" />
|
||||||
|
<window_info id="Image Layers" />
|
||||||
|
<window_info id="Capture Tool" />
|
||||||
|
<window_info id="UI Designer" />
|
||||||
|
<window_info id="Favorites" side_tool="true" />
|
||||||
|
<window_info id="Learn" />
|
||||||
|
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.23500492" />
|
||||||
|
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
|
||||||
|
<window_info anchor="bottom" id="Terminal" />
|
||||||
|
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
|
||||||
|
<window_info anchor="bottom" id="Event Log" side_tool="true" />
|
||||||
|
<window_info anchor="bottom" id="Database Changes" />
|
||||||
|
<window_info anchor="bottom" id="Version Control" />
|
||||||
|
<window_info anchor="bottom" id="Messages" />
|
||||||
|
<window_info anchor="bottom" id="Message" order="0" />
|
||||||
|
<window_info anchor="bottom" id="Find" order="1" />
|
||||||
|
<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" />
|
||||||
|
<window_info anchor="bottom" id="TODO" order="6" />
|
||||||
|
<window_info anchor="right" id="Maven" />
|
||||||
|
<window_info anchor="right" id="Palette" />
|
||||||
|
<window_info anchor="right" id="Capture Analysis" />
|
||||||
|
<window_info anchor="right" id="Database" />
|
||||||
|
<window_info anchor="right" id="Palette	" />
|
||||||
|
<window_info anchor="right" id="Theme Preview" />
|
||||||
|
<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" />
|
||||||
|
</layout>
|
||||||
|
</component>
|
||||||
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
<option name="version" value="1" />
|
||||||
|
</component>
|
||||||
|
<component name="editorHistoryManager">
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/sequential/SequentialSum.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="180">
|
||||||
|
<caret line="12" column="30" selection-start-line="12" selection-start-column="30" selection-end-line="12" selection-end-column="30" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/simple2/SingleUpdatePerThread.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="285">
|
||||||
|
<caret line="21" column="24" selection-start-line="21" selection-start-column="24" selection-end-line="21" selection-end-column="24" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/synchronized2/SingleUpdatePerThreadSynch.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="180">
|
||||||
|
<caret line="12" column="31" selection-start-line="12" selection-start-column="31" selection-end-line="12" selection-end-column="31" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/simple1/MultipleUpdatesPerThread.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="75">
|
||||||
|
<caret line="13" column="18" selection-start-line="13" selection-start-column="18" selection-end-line="13" selection-end-column="18" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/atomic2/SingleUpdatePerThreadAtomic.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="175">
|
||||||
|
<caret line="29" column="8" selection-start-line="29" selection-start-column="8" selection-end-line="29" selection-end-column="8" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#300#301#0" expanded="true" />
|
||||||
|
<element signature="e#324#325#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/synchronized1/MultipleUpdatesPerThreadSynch.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="317">
|
||||||
|
<caret line="28" column="9" selection-start-line="19" selection-start-column="8" selection-end-line="28" selection-end-column="9" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#301#302#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/parallel/CollectingResults.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="30">
|
||||||
|
<caret line="2" column="17" lean-forward="true" selection-start-line="2" selection-start-column="17" selection-end-line="2" selection-end-column="17" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/test/Test.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="175">
|
||||||
|
<caret line="61" column="8" lean-forward="true" selection-start-line="61" selection-start-column="8" selection-end-line="61" selection-end-column="8" />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
<entry file="file://$PROJECT_DIR$/src/atomic1/MultipleUpdatesPerThreadAtomic.java">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state relative-caret-position="450">
|
||||||
|
<caret line="31" lean-forward="true" selection-start-line="31" selection-end-line="31" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#312#313#0" expanded="true" />
|
||||||
|
<element signature="e#344#345#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>Ex1</last-edited>
|
||||||
|
<splitter-proportions>
|
||||||
|
<option name="proportions">
|
||||||
|
<list>
|
||||||
|
<option value="0.2" />
|
||||||
|
</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>
|
50
hw1/Ex1/src/atomic1/MultipleUpdatesPerThreadAtomic.java
Normal file
50
hw1/Ex1/src/atomic1/MultipleUpdatesPerThreadAtomic.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package atomic1;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class MultipleUpdatesPerThreadAtomic {
|
||||||
|
private static AtomicLong result;
|
||||||
|
|
||||||
|
private MultipleUpdatesPerThreadAtomic() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addPartialResult(long partial) {
|
||||||
|
result.getAndAdd(partial);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
result = new AtomicLong(0);
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumMultipleUpdates(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result.longValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumMultipleUpdates implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumMultipleUpdates(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
MultipleUpdatesPerThreadAtomic.addPartialResult(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
hw1/Ex1/src/atomic2/SingleUpdatePerThreadAtomic.java
Normal file
52
hw1/Ex1/src/atomic2/SingleUpdatePerThreadAtomic.java
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package atomic2;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class SingleUpdatePerThreadAtomic {
|
||||||
|
private static AtomicLong result;
|
||||||
|
|
||||||
|
private SingleUpdatePerThreadAtomic() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addPartialResult(long partial) {
|
||||||
|
result.getAndAdd(partial);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
result = new AtomicLong(0);
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumSingleUpdate(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result.longValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumSingleUpdate implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumSingleUpdate(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
long partialSum = 0;
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
partialSum += nums[i];
|
||||||
|
}
|
||||||
|
SingleUpdatePerThreadAtomic.addPartialResult(partialSum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
hw1/Ex1/src/parallel/CollectingResults.java
Normal file
48
hw1/Ex1/src/parallel/CollectingResults.java
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package parallel;
|
||||||
|
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @ThreadSafe
|
||||||
|
public class CollectingResults {
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
long total = 0;
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
SumCollected[] collected = new SumCollected[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
collected[i] = new SumCollected(nums, i * size, (i + 1) * size);
|
||||||
|
threads[i] = new Thread(collected[i]);
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i].join();
|
||||||
|
total += collected[i].getPartialSum();
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumCollected implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
private long partialSum;
|
||||||
|
|
||||||
|
public SumCollected(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
this.partialSum = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getPartialSum() {
|
||||||
|
return partialSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
for(int i = low; i < high; i++) {
|
||||||
|
partialSum += nums[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
hw1/Ex1/src/sequential/SequentialSum.java
Normal file
17
hw1/Ex1/src/sequential/SequentialSum.java
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package sequential;
|
||||||
|
|
||||||
|
// @ThreadSafe
|
||||||
|
public class SequentialSum {
|
||||||
|
|
||||||
|
private SequentialSum() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int nums[]) {
|
||||||
|
long result = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
result += nums[i];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
50
hw1/Ex1/src/simple1/MultipleUpdatesPerThread.java
Normal file
50
hw1/Ex1/src/simple1/MultipleUpdatesPerThread.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package simple1;
|
||||||
|
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class MultipleUpdatesPerThread {
|
||||||
|
|
||||||
|
private static long result;
|
||||||
|
|
||||||
|
private MultipleUpdatesPerThread() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addPartialResult(long partial) {
|
||||||
|
result += partial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
result = 0;
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumMultipleUpdates(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumMultipleUpdates implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumMultipleUpdates(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
MultipleUpdatesPerThread.addPartialResult(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
hw1/Ex1/src/simple2/SingleUpdatePerThread.java
Normal file
51
hw1/Ex1/src/simple2/SingleUpdatePerThread.java
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package simple2;
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class SingleUpdatePerThread {
|
||||||
|
|
||||||
|
private static long result;
|
||||||
|
|
||||||
|
private SingleUpdatePerThread() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addPartialResult(long partial) {
|
||||||
|
result += partial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
result = 0;
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumSingleUpdate(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumSingleUpdate implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumSingleUpdate(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
long partialSum = 0;
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
partialSum += nums[i];
|
||||||
|
}
|
||||||
|
SingleUpdatePerThread.addPartialResult(partialSum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
hw1/Ex1/src/synchronized1/MultipleUpdatesPerThreadSynch.java
Normal file
50
hw1/Ex1/src/synchronized1/MultipleUpdatesPerThreadSynch.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package synchronized1;
|
||||||
|
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class MultipleUpdatesPerThreadSynch {
|
||||||
|
|
||||||
|
private static long result;
|
||||||
|
|
||||||
|
private MultipleUpdatesPerThreadSynch() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static synchronized void addPartialResult(long partial) {
|
||||||
|
result += partial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(final int[] nums) throws Exception {
|
||||||
|
result = 0;
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumMultipleUpdates(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumMultipleUpdates implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumMultipleUpdates(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
MultipleUpdatesPerThreadSynch.addPartialResult(nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
hw1/Ex1/src/synchronized2/SingleUpdatePerThreadSynch.java
Normal file
51
hw1/Ex1/src/synchronized2/SingleUpdatePerThreadSynch.java
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package synchronized2;
|
||||||
|
import test.Test;
|
||||||
|
|
||||||
|
// @NotThreadSafe
|
||||||
|
public class SingleUpdatePerThreadSynch {
|
||||||
|
|
||||||
|
private static long result;
|
||||||
|
|
||||||
|
private SingleUpdatePerThreadSynch() {
|
||||||
|
// Cannot be instantiated
|
||||||
|
}
|
||||||
|
|
||||||
|
private static synchronized void addPartialResult(long partial) {
|
||||||
|
result += partial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(int[] nums) throws Exception {
|
||||||
|
result = 0;
|
||||||
|
Thread[] threads = new Thread[Test.NUM_THREADS];
|
||||||
|
int size = (int) Math.ceil((double) nums.length / Test.NUM_THREADS);
|
||||||
|
for (int i = 0; i < Test.NUM_THREADS; i++) {
|
||||||
|
threads[i] = new Thread(
|
||||||
|
new SumSingleUpdated(nums, i * size, (i + 1) * size));
|
||||||
|
threads[i].start();
|
||||||
|
}
|
||||||
|
for (Thread thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SumSingleUpdated implements Runnable {
|
||||||
|
private final int[] nums;
|
||||||
|
private final int low;
|
||||||
|
private final int high;
|
||||||
|
|
||||||
|
public SumSingleUpdated(int[] nums, int low, int high) {
|
||||||
|
this.nums = nums;
|
||||||
|
this.low = low;
|
||||||
|
this.high = Math.min(high, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
long partialSum = 0;
|
||||||
|
for (int i = low; i < high; i++) {
|
||||||
|
partialSum += nums[i];
|
||||||
|
}
|
||||||
|
SingleUpdatePerThreadSynch.addPartialResult(partialSum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
hw1/Ex1/src/test/SumFunction.java
Normal file
6
hw1/Ex1/src/test/SumFunction.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package test;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SumFunction {
|
||||||
|
long sum(int[] nums) throws Exception;
|
||||||
|
}
|
84
hw1/Ex1/src/test/Test.java
Normal file
84
hw1/Ex1/src/test/Test.java
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
package test;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import sequential.SequentialSum;
|
||||||
|
import parallel.CollectingResults;
|
||||||
|
import simple2.SingleUpdatePerThread;
|
||||||
|
import simple1.MultipleUpdatesPerThread;
|
||||||
|
import atomic2.SingleUpdatePerThreadAtomic;
|
||||||
|
import atomic1.MultipleUpdatesPerThreadAtomic;
|
||||||
|
import synchronized2.SingleUpdatePerThreadSynch;
|
||||||
|
import synchronized1.MultipleUpdatesPerThreadSynch;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
private static final Random generator = new Random();
|
||||||
|
public static final int NUM_THREADS = 256;
|
||||||
|
private static final int SIZE = 99999999;
|
||||||
|
private static final int RUNS = 15;
|
||||||
|
|
||||||
|
private static long getSum(int[] array) {
|
||||||
|
return SequentialSum.sum(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testSumImplementation(
|
||||||
|
SumFunction implementation,
|
||||||
|
int[] array, String name) throws Exception {
|
||||||
|
|
||||||
|
// Cloning the array with the random numbers
|
||||||
|
int[] copy = array.clone();
|
||||||
|
// Getting a time-stamp when the sum starts
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
// Executing the implementation to sum the numbers
|
||||||
|
long result = implementation.sum(copy);
|
||||||
|
// Getting a time-stamp when the sum finishes
|
||||||
|
long endTime = System.nanoTime();
|
||||||
|
long correct = getSum(array);
|
||||||
|
if (correct != result) {
|
||||||
|
// Informing if the implementation failed
|
||||||
|
System.err.println("- ERROR: " + name + " result was: "
|
||||||
|
+ result + " but " + correct + " was expected.");
|
||||||
|
} else {
|
||||||
|
// Showing the time taken by the implementation
|
||||||
|
System.out.printf("- " + name + " time: %d ms \n"
|
||||||
|
, ((endTime - startTime) / 1000000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Generating random numbers to be added
|
||||||
|
int[] numbers = new int[SIZE];
|
||||||
|
for (int i = 0; i < SIZE; i++) {
|
||||||
|
numbers[i] = generator.nextInt();
|
||||||
|
}
|
||||||
|
// Executing several runs
|
||||||
|
for (int i = 0; i < RUNS; i++) {
|
||||||
|
System.out.println("Run " + (i + 1) + "/" + RUNS + ":");
|
||||||
|
// Testing different implementations:
|
||||||
|
// Sequential
|
||||||
|
testSumImplementation(SequentialSum::sum, numbers,
|
||||||
|
"Sequential version");
|
||||||
|
//Parallel implementation - Multiple updates per thread
|
||||||
|
testSumImplementation(MultipleUpdatesPerThread::sum, numbers,
|
||||||
|
"Parallel - Multiple updates per thread");
|
||||||
|
// Parallel - Single update per thread
|
||||||
|
testSumImplementation(SingleUpdatePerThread::sum, numbers,
|
||||||
|
"Parallel - Single update per thread");
|
||||||
|
// Parallel - Multiple updates per thread - Synchronized
|
||||||
|
testSumImplementation(MultipleUpdatesPerThreadSynch::sum, numbers,
|
||||||
|
"Parallel - Multiple updates per thread - Synchronized");
|
||||||
|
// Parallel - Single update per thread - Synchronized
|
||||||
|
testSumImplementation(SingleUpdatePerThreadSynch::sum, numbers,
|
||||||
|
"Parallel - Single update per thread - Synchronized");
|
||||||
|
// Parallel - Multiple updates per thread - Atomic
|
||||||
|
testSumImplementation(MultipleUpdatesPerThreadAtomic::sum, numbers,
|
||||||
|
"Parallel - Multiple updates per thread - Atomic");
|
||||||
|
// Parallel - Single update per thread - Atomic
|
||||||
|
testSumImplementation(SingleUpdatePerThreadAtomic::sum, numbers,
|
||||||
|
"Parallel - Single update per thread - Atomic");
|
||||||
|
// Parallel - Collecting partial results
|
||||||
|
testSumImplementation(CollectingResults::sum, numbers,
|
||||||
|
"Parallel - Collecting partial results");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
hw1/Ex2/src/semaphore/Car.java
Normal file
25
hw1/Ex2/src/semaphore/Car.java
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
|
||||||
|
// Simulated max time taken by a valet to park a car
|
||||||
|
private static final int MAX_PARKING_TIME = 3000;
|
||||||
|
|
||||||
|
private final int plate;
|
||||||
|
private static final Random generator = new Random();
|
||||||
|
|
||||||
|
public Car(int plate) {
|
||||||
|
this.plate = plate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPlate() {
|
||||||
|
return plate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void park() throws Exception {
|
||||||
|
int time = generator.nextInt(MAX_PARKING_TIME);
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
|
}
|
20
hw1/Ex2/src/semaphore/CarSimulation.java
Normal file
20
hw1/Ex2/src/semaphore/CarSimulation.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
public class CarSimulation implements Runnable {
|
||||||
|
private final Queue<Car> valetQueue;
|
||||||
|
private final Car car;
|
||||||
|
|
||||||
|
public CarSimulation(Queue<Car> valetQueue, Car car) {
|
||||||
|
this.valetQueue = valetQueue;
|
||||||
|
this.car = car;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
valetQueue.put(car);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
hw1/Ex2/src/semaphore/Main.java
Normal file
23
hw1/Ex2/src/semaphore/Main.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
// Maximum number of cars on the queue for a valet parking service
|
||||||
|
private static final int QUEUE_SIZE = 10;
|
||||||
|
// Simulated number of cars
|
||||||
|
public static final int NUM_CARS = 150;
|
||||||
|
// Simulated number of valets
|
||||||
|
private static final int NUM_VALETS = 5;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// The shared queue
|
||||||
|
final Queue<Car> valetQueue = new ValetQueue(QUEUE_SIZE);
|
||||||
|
// Starting the threads simulating the cars
|
||||||
|
for (int i = 0; i < NUM_CARS; i++) {
|
||||||
|
new Thread(new CarSimulation(valetQueue, new Car(i))).start();
|
||||||
|
}
|
||||||
|
// Starting the threads simulating the valets
|
||||||
|
for (int i = 0; i < NUM_VALETS; i++) {
|
||||||
|
new Thread(new ValetSimulation(valetQueue), "Valet " + i ).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
hw1/Ex2/src/semaphore/Queue.java
Normal file
20
hw1/Ex2/src/semaphore/Queue.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
public interface Queue<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts an element into the queue, waiting if necessary for
|
||||||
|
* space to become available
|
||||||
|
* @param the element to be put into the queue
|
||||||
|
* @throws InterruptedException if interrupted while waiting
|
||||||
|
*/
|
||||||
|
void put (T element) throws InterruptedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves and removes the first element in this queue (in FIFO order),
|
||||||
|
* waiting if necessary until a element enters the queue
|
||||||
|
* @return the first element on this queue
|
||||||
|
* @throws InterruptedException if interrupted while waiting
|
||||||
|
*/
|
||||||
|
T take() throws InterruptedException;
|
||||||
|
}
|
24
hw1/Ex2/src/semaphore/ValetQueue.java
Normal file
24
hw1/Ex2/src/semaphore/ValetQueue.java
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
public class ValetQueue implements Queue<Car> {
|
||||||
|
private final List<Car> waitingCars;
|
||||||
|
private final int queueMaxSize;
|
||||||
|
private final Semaphore full, empty, mutex;
|
||||||
|
|
||||||
|
public ValetQueue(int queueMaxSize) {
|
||||||
|
this.queueMaxSize = queueMaxSize;
|
||||||
|
waitingCars = new ArrayList<>(queueMaxSize);
|
||||||
|
empty = new Semaphore (queueMaxSize);
|
||||||
|
full = new Semaphore(0);
|
||||||
|
mutex = new Semaphore(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement method put
|
||||||
|
|
||||||
|
// TODO: Implement method take
|
||||||
|
|
||||||
|
}
|
21
hw1/Ex2/src/semaphore/ValetSimulation.java
Normal file
21
hw1/Ex2/src/semaphore/ValetSimulation.java
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package semaphore;
|
||||||
|
|
||||||
|
public class ValetSimulation implements Runnable {
|
||||||
|
private final Queue<Car> valetQueue;
|
||||||
|
|
||||||
|
public ValetSimulation(Queue<Car> valetQueue) {
|
||||||
|
this.valetQueue = valetQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
Car car = valetQueue.take();
|
||||||
|
car.park();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
hw1/Ex5/src/reentrantrwlock/CreatorSimulation.java
Normal file
20
hw1/Ex5/src/reentrantrwlock/CreatorSimulation.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CreatorSimulation implements Runnable {
|
||||||
|
|
||||||
|
private final MovieCatalog myCatalog;
|
||||||
|
private final List<Movie> movies;
|
||||||
|
|
||||||
|
public CreatorSimulation(MovieCatalog myCatalog, List<Movie> movies) {
|
||||||
|
this.myCatalog = myCatalog;
|
||||||
|
this.movies = movies;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
for (Movie movie : movies) {
|
||||||
|
myCatalog.addMovie(movie);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
hw1/Ex5/src/reentrantrwlock/Main.java
Normal file
49
hw1/Ex5/src/reentrantrwlock/Main.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
// Initial number of movies in the catalog
|
||||||
|
private static final int INITIAL_SIZE = 500;
|
||||||
|
// Maximum duration of a movie in the catalog
|
||||||
|
private static final int MAX_DURATION = 3000;
|
||||||
|
// Number of users watching movies
|
||||||
|
private static final int NUM_WATCHERS = 150;
|
||||||
|
// Number of users reading the catalog
|
||||||
|
private static final int NUM_READERS = 100;
|
||||||
|
// Number of publishers adding movies
|
||||||
|
private static final int NUM_ADDERS = 30;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// The shared catalog
|
||||||
|
MovieCatalog myCatalog = new MovieCatalog();
|
||||||
|
// Populating the catalog with a initial set of movies
|
||||||
|
List<Movie> movies = new ArrayList<>(INITIAL_SIZE);
|
||||||
|
Random generator = new Random();
|
||||||
|
for (int i = 0; i < INITIAL_SIZE; i++) {
|
||||||
|
movies.add(
|
||||||
|
new Movie("Movie" + i, generator.nextInt(MAX_DURATION)));
|
||||||
|
}
|
||||||
|
Thread creator = new Thread(
|
||||||
|
new CreatorSimulation(myCatalog, movies), "Creator");
|
||||||
|
creator.start();
|
||||||
|
creator.join();
|
||||||
|
System.out.println("The movie catalog has been created.");
|
||||||
|
// Starting the threads simulating the movie watchers
|
||||||
|
for (int i = 0; i < NUM_WATCHERS; i++) {
|
||||||
|
new Thread(new WatcherSimulation(myCatalog), "Watcher" + i).start();
|
||||||
|
}
|
||||||
|
// Starting the threads simulating the readers of movie titles
|
||||||
|
for (int i = 0; i < NUM_READERS; i++) {
|
||||||
|
new Thread(new ReaderSimulation(myCatalog), "Reader" + i).start();
|
||||||
|
}
|
||||||
|
// Starting the threads simulating the publishers adding movies
|
||||||
|
for (int i = 0; i < NUM_ADDERS; i++) {
|
||||||
|
Movie movie = new Movie("NewMovie" + i, generator.nextInt(MAX_DURATION));
|
||||||
|
new Thread(
|
||||||
|
new PublisherSimulation(myCatalog, movie), "Publisher" + i).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
hw1/Ex5/src/reentrantrwlock/Movie.java
Normal file
26
hw1/Ex5/src/reentrantrwlock/Movie.java
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
public class Movie {
|
||||||
|
|
||||||
|
private final String title;
|
||||||
|
private final int duration;
|
||||||
|
|
||||||
|
public Movie(String title, int duration) {
|
||||||
|
this.title = title;
|
||||||
|
this.duration = duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDuration() {
|
||||||
|
return duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void play() throws Exception {
|
||||||
|
Thread.sleep(duration);
|
||||||
|
System.out.println("[" + Thread.currentThread().getName()
|
||||||
|
+ "]: I've finished watching movie: " + title);
|
||||||
|
}
|
||||||
|
}
|
66
hw1/Ex5/src/reentrantrwlock/MovieCatalog.java
Normal file
66
hw1/Ex5/src/reentrantrwlock/MovieCatalog.java
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
|
||||||
|
public class MovieCatalog {
|
||||||
|
|
||||||
|
private final Map<String, Movie> movies;
|
||||||
|
private final ReentrantReadWriteLock rwlock;
|
||||||
|
private final Lock rLock, wLock;
|
||||||
|
private static final Random generator = new Random();
|
||||||
|
|
||||||
|
public MovieCatalog() {
|
||||||
|
movies = new TreeMap<>();
|
||||||
|
rwlock = new ReentrantReadWriteLock();
|
||||||
|
rLock = rwlock.readLock();
|
||||||
|
wLock = rwlock.writeLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current size of this catalog
|
||||||
|
* @return the size of this catalog
|
||||||
|
*/
|
||||||
|
public int getSize() {
|
||||||
|
// TODO: Implement method getSize
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a movie from this catalog
|
||||||
|
* @param the title of movie to be obtained
|
||||||
|
* @return the movie with the title provided or
|
||||||
|
* null if the movie is not in the catalog
|
||||||
|
*/
|
||||||
|
public Movie getMovie(String title) {
|
||||||
|
//TODO: Implement method getMovie
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of all movie titles in this catalog
|
||||||
|
* @return a list of the movie titles in this catalog
|
||||||
|
*/
|
||||||
|
public List<String> getTitles() {
|
||||||
|
//TODO: Implement method getTitles
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random movie from this catalog
|
||||||
|
* @return a randomly selected movie from this catalog
|
||||||
|
*/
|
||||||
|
public Movie getRandomMovie() {
|
||||||
|
// TODO: Implement method getRandomMovie
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a movie to the catalog
|
||||||
|
* @param the movie to be added to the catalog
|
||||||
|
*/
|
||||||
|
public void addMovie(Movie movie) {
|
||||||
|
//TODO: Implement method addMovie
|
||||||
|
}
|
||||||
|
}
|
16
hw1/Ex5/src/reentrantrwlock/PublisherSimulation.java
Normal file
16
hw1/Ex5/src/reentrantrwlock/PublisherSimulation.java
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
public class PublisherSimulation implements Runnable {
|
||||||
|
|
||||||
|
private final MovieCatalog myCatalog;
|
||||||
|
private final Movie movie;
|
||||||
|
|
||||||
|
public PublisherSimulation(MovieCatalog myCatalog, Movie movie) {
|
||||||
|
this.myCatalog = myCatalog;
|
||||||
|
this.movie = movie;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
myCatalog.addMovie(movie);
|
||||||
|
}
|
||||||
|
}
|
34
hw1/Ex5/src/reentrantrwlock/ReaderSimulation.java
Normal file
34
hw1/Ex5/src/reentrantrwlock/ReaderSimulation.java
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class ReaderSimulation implements Runnable {
|
||||||
|
|
||||||
|
// Maximum time taken by a user to read a single title
|
||||||
|
private static final int MAX_READING_TIME = 10;
|
||||||
|
private static final Random generator = new Random();
|
||||||
|
private final MovieCatalog myCatalog;
|
||||||
|
|
||||||
|
public ReaderSimulation(MovieCatalog myCatalog) {
|
||||||
|
this.myCatalog = myCatalog;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void read() throws Exception {
|
||||||
|
int time = generator.nextInt(MAX_READING_TIME);
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
List<String> titles = myCatalog.getTitles();
|
||||||
|
for (int i = 0; i < titles.size(); i++) {
|
||||||
|
read();
|
||||||
|
}
|
||||||
|
System.out.println("[" + Thread.currentThread().getName()
|
||||||
|
+ "]: I've finished reading movie titles");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
hw1/Ex5/src/reentrantrwlock/WatcherSimulation.java
Normal file
20
hw1/Ex5/src/reentrantrwlock/WatcherSimulation.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package reentrantrwlock;
|
||||||
|
|
||||||
|
public class WatcherSimulation implements Runnable {
|
||||||
|
|
||||||
|
private final MovieCatalog myCatalog;
|
||||||
|
|
||||||
|
public WatcherSimulation(MovieCatalog myCatalog) {
|
||||||
|
this.myCatalog = myCatalog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Movie movie = myCatalog.getRandomMovie();
|
||||||
|
try {
|
||||||
|
movie.play();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
hw1/submission.tex
Normal file
32
hw1/submission.tex
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
\documentclass[12pt]{article}
|
||||||
|
|
||||||
|
\usepackage[utf8]{inputenc}
|
||||||
|
%\usepackage[margin=2cm]{geometry}
|
||||||
|
|
||||||
|
\title{Howework 1 -- Programming Fundamentals 3}
|
||||||
|
\author{Claudio Maggioni}
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
\maketitle
|
||||||
|
\section{Exercise 1}
|
||||||
|
\subsection{Question 1}
|
||||||
|
\texttt{MultipleUpdatesPerThread} is neither correct nor efficient. The reason for its uncorrectness is the unsynchronized access
|
||||||
|
of \emph{result}, which assigns unconsistent values to it: non-atomic evaluation of the statement \texttt{result += partialSum;}
|
||||||
|
can make one thread evaluate the new value for \emph{result} before another has finished writing to it, thus producing incorrect
|
||||||
|
results.
|
||||||
|
|
||||||
|
By synchronising the access to \emph{result}, we effectively make the application sequential, since only one thread at a time can
|
||||||
|
change result. Therefore, \texttt{MultipleUpdatesPerThread} runs even worse than \texttt{SequentialSum} since it has to cope with
|
||||||
|
all the synchronization overhead.
|
||||||
|
\subsection{Question 2}
|
||||||
|
While more efficient, \texttt{SingleUpdatePerThread} is still wrong because the access to \emph{result} is still not synchronized.
|
||||||
|
Again, non-atomic evaluation of the statement \texttt{result += partialSum;} can make one thread evaluate the new value for \emph{result} before another has finished writing to it, thus producing incorrect
|
||||||
|
results.
|
||||||
|
\subsection{Question 6}
|
||||||
|
\texttt{CollectingResults} is the only Thread-safe implementation because it is the only one not to use a static field to compute
|
||||||
|
the final result. If multiple threads use anyone of the other classes concurrently, \emph{result} will be shared between the
|
||||||
|
threads and all the results will be inconsistent. In order to solve this problem, either the entire \texttt{sum(...)} method must
|
||||||
|
be considered a critical section (defeating the point of concurrent access to the summing class) or the scope of result must be
|
||||||
|
bound to the thread (e.g. by making \emph{result} either a private field and making the inner classes non-static, or by making it
|
||||||
|
a local variable, as \texttt{CollectingResults} does).
|
||||||
|
\end{document}
|
Reference in a new issue