Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Lösningsförslag EDAP10 2020-08-19
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patrik Persson
Lösningsförslag EDAP10 2020-08-19
Commits
c10d2ce8
Commit
c10d2ce8
authored
Aug 25, 2020
by
Patrik Persson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lösningsförslag
parent
3e126beb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
121 additions
and
1 deletion
+121
-1
.classpath
.classpath
+12
-0
.project
.project
+17
-0
README.md
README.md
+16
-1
src/liveness/LivenessTracker.java
src/liveness/LivenessTracker.java
+76
-0
No files found.
.classpath
0 → 100644
View file @
c10d2ce8
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry
kind=
"src"
path=
"src"
/>
<classpathentry
combineaccessrules=
"false"
kind=
"src"
path=
"/tenta-200819"
/>
<classpathentry
kind=
"con"
path=
"org.eclipse.jdt.launching.JRE_CONTAINER"
>
<attributes>
<attribute
name=
"module"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"lib"
path=
"/tenta-200819/exam.jar"
/>
<classpathentry
kind=
"output"
path=
"bin"
/>
</classpath>
.project
0 → 100644
View file @
c10d2ce8
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>
tenta-200819-losning
</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>
org.eclipse.jdt.core.javabuilder
</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>
org.eclipse.jdt.core.javanature
</nature>
</natures>
</projectDescription>
README.md
View file @
c10d2ce8
# Lösningsförslag
EDAP10 2020-08-
19
# Lösningsförslag
, EDAP10 Flertrådad programmering 2008
19
### Tåguppgift (hemuppgift 1-3): totalt 14p
Uppgift 1-2 (kod): totalt 8p
Uppgift 3.1-3.3: 2p/fråga (1p för a-uppgift, 1p för b-uppgift), totalt 6p
Vi publicerar inget lösningsförslag eftersom denna uppgift numera är en del av laboration 2 i kursen.
### Watchdog-övervakning (hemuppgift 4): totalt 14p
### Teorifrågor (muntlig tentamen 200821): totalt 14p
7 teorifrågor x 2p.
## Totalsumma 42p (godkänt: 21p)
src/liveness/LivenessTracker.java
0 → 100644
View file @
c10d2ce8
package
liveness
;
import
java.util.HashMap
;
import
java.util.Map
;
// totalt: 14p
// -- lämpligt attribut, mappar tråd till timeout 2p
// -- lagrar rätt timeout i reportActivity 2p
// -- hittar rätt timeout i awaitTimeout 2p
// -- övervakningstråd 2p
// -- rätt signalering (notify-wait) 2p
// -- rätt blockering i awaitTimeout 2p
// -- hanterar fallet timeout <= 0 1p
// -- korrekt monitor i övrigt (synchronized) 1p
public
abstract
class
LivenessTracker
{
private
final
Map
<
Thread
,
Long
>
timeouts
=
new
HashMap
<>();
public
void
initialize
()
{
new
Thread
(()
->
{
try
{
while
(
true
)
{
Thread
t
=
awaitTimeout
();
timeoutAlert
(
t
);
}
}
catch
(
InterruptedException
unexpected
)
{
throw
new
Error
(
unexpected
);
}
}).
start
();
}
public
synchronized
void
reportActivity
(
long
timeout
)
{
long
now
=
System
.
currentTimeMillis
();
Thread
t
=
Thread
.
currentThread
();
timeouts
.
remove
(
t
);
if
(
timeout
>
0
)
{
timeouts
.
put
(
t
,
now
+
timeout
);
notifyAll
();
}
}
protected
abstract
void
timeoutAlert
(
Thread
t
);
private
Map
.
Entry
<
Thread
,
Long
>
mostImminent
()
{
Map
.
Entry
<
Thread
,
Long
>
mostImminent
=
null
;
for
(
Map
.
Entry
<
Thread
,
Long
>
entry
:
timeouts
.
entrySet
())
{
if
(
mostImminent
==
null
||
entry
.
getValue
()
<
mostImminent
.
getValue
())
{
mostImminent
=
entry
;
}
}
return
mostImminent
;
}
private
synchronized
Thread
awaitTimeout
()
throws
InterruptedException
{
long
now
=
System
.
currentTimeMillis
();
while
(
true
)
{
Map
.
Entry
<
Thread
,
Long
>
next
=
mostImminent
();
if
(
next
==
null
)
{
wait
();
}
else
if
(
next
.
getValue
()
>
now
)
{
wait
(
next
.
getValue
()
-
now
);
}
now
=
System
.
currentTimeMillis
();
next
=
mostImminent
();
if
(
next
.
getValue
()
<=
now
)
{
Thread
timedOut
=
next
.
getKey
();
timeouts
.
remove
(
timedOut
);
return
timedOut
;
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment