Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP - HW2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
moataz.dbaneh
PP - HW2
Commits
52d97608
Commit
52d97608
authored
May 21, 2023
by
MoutazDebbaneh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Solved all questions
parent
ef4a140b
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
208 additions
and
6 deletions
+208
-6
vcs.xml
.idea/vcs.xml
+6
-0
Entry.java
src/main/java/Entry.java
+4
-0
LockList.java
src/main/java/LockList.java
+6
-0
RWLockList.java
src/main/java/RWLockList.java
+54
-0
SortList.java
src/main/java/SortList.java
+16
-1
SyncList.java
src/main/java/SyncList.java
+6
-0
RemoveAllThread.java
src/test/java/RemoveAllThread.java
+10
-0
SyncListTest.java
src/test/java/SyncListTest.java
+106
-5
No files found.
.idea/vcs.xml
0 → 100644
View file @
52d97608
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"VcsDirectoryMappings"
>
<mapping
directory=
"$PROJECT_DIR$"
vcs=
"Git"
/>
</component>
</project>
\ No newline at end of file
src/main/java/Entry.java
View file @
52d97608
import
java.util.concurrent.locks.ReentrantReadWriteLock
;
public
class
Entry
{
public
class
Entry
{
public
Integer
object
;
public
Integer
object
;
public
Entry
next
;
public
Entry
next
;
public
ReentrantReadWriteLock
lock
=
new
ReentrantReadWriteLock
();
public
Entry
(
Integer
object
)
{
public
Entry
(
Integer
object
)
{
this
.
object
=
object
;
this
.
object
=
object
;
}
}
...
...
src/main/java/LockList.java
View file @
52d97608
...
@@ -25,6 +25,7 @@ public class LockList extends SortList {
...
@@ -25,6 +25,7 @@ public class LockList extends SortList {
Entry
newEntry
=
new
Entry
(
obj
);
Entry
newEntry
=
new
Entry
(
obj
);
newEntry
.
next
=
curr
;
newEntry
.
next
=
curr
;
prev
.
next
=
newEntry
;
prev
.
next
=
newEntry
;
this
.
length
++;
return
true
;
return
true
;
}
}
}
finally
{
}
finally
{
...
@@ -44,8 +45,11 @@ public class LockList extends SortList {
...
@@ -44,8 +45,11 @@ public class LockList extends SortList {
}
}
if
(
curr
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
))
{
prev
.
next
=
curr
.
next
;
prev
.
next
=
curr
.
next
;
this
.
length
--;
successfulRemoves
++;
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulRemoves
++;
return
false
;
return
false
;
}
}
}
finally
{
}
finally
{
...
@@ -65,8 +69,10 @@ public class LockList extends SortList {
...
@@ -65,8 +69,10 @@ public class LockList extends SortList {
curr
=
prev
.
next
;
curr
=
prev
.
next
;
}
}
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
successfulContains
++;
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulContains
++;
return
false
;
return
false
;
}
}
}
finally
{
}
finally
{
...
...
src/main/java/RWLockList.java
View file @
52d97608
...
@@ -2,6 +2,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
...
@@ -2,6 +2,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
public
class
RWLockList
extends
SortList
{
public
class
RWLockList
extends
SortList
{
ReentrantReadWriteLock
lock
=
new
ReentrantReadWriteLock
();
ReentrantReadWriteLock
lock
=
new
ReentrantReadWriteLock
();
ReentrantReadWriteLock
successfulContainsLock
=
new
ReentrantReadWriteLock
();
ReentrantReadWriteLock
unsuccessfulContainsLock
=
new
ReentrantReadWriteLock
();
public
RWLockList
()
{
public
RWLockList
()
{
super
();
super
();
...
@@ -24,6 +26,7 @@ public class RWLockList extends SortList {
...
@@ -24,6 +26,7 @@ public class RWLockList extends SortList {
Entry
newEntry
=
new
Entry
(
obj
);
Entry
newEntry
=
new
Entry
(
obj
);
newEntry
.
next
=
curr
;
newEntry
.
next
=
curr
;
prev
.
next
=
newEntry
;
prev
.
next
=
newEntry
;
this
.
length
++;
return
true
;
return
true
;
}
}
}
finally
{
}
finally
{
...
@@ -43,8 +46,11 @@ public class RWLockList extends SortList {
...
@@ -43,8 +46,11 @@ public class RWLockList extends SortList {
}
}
if
(
curr
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
))
{
prev
.
next
=
curr
.
next
;
prev
.
next
=
curr
.
next
;
this
.
length
--;
successfulRemoves
++;
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulRemoves
++;
return
false
;
return
false
;
}
}
}
finally
{
}
finally
{
...
@@ -64,8 +70,14 @@ public class RWLockList extends SortList {
...
@@ -64,8 +70,14 @@ public class RWLockList extends SortList {
curr
=
prev
.
next
;
curr
=
prev
.
next
;
}
}
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
successfulContainsLock
.
writeLock
().
lock
();
successfulContains
++;
successfulContainsLock
.
writeLock
().
unlock
();
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulContainsLock
.
writeLock
().
lock
();
unsuccessfulContains
++;
unsuccessfulContainsLock
.
writeLock
().
unlock
();
return
false
;
return
false
;
}
}
}
finally
{
}
finally
{
...
@@ -73,4 +85,46 @@ public class RWLockList extends SortList {
...
@@ -73,4 +85,46 @@ public class RWLockList extends SortList {
}
}
}
}
public
boolean
safeRemoveElement
(
Integer
obj
){
Entry
prev
,
curr
;
this
.
head
.
lock
.
readLock
().
lock
();
prev
=
this
.
head
;
prev
.
next
.
lock
.
readLock
().
lock
();
curr
=
prev
.
next
;
while
(
curr
.
object
.
compareTo
(
obj
)
<
0
)
{
curr
.
next
.
lock
.
readLock
().
lock
();
prev
.
lock
.
readLock
().
unlock
();
prev
=
curr
;
curr
=
prev
.
next
;
}
prev
.
lock
.
readLock
().
unlock
();
curr
.
lock
.
readLock
().
unlock
();
prev
.
lock
.
writeLock
().
lock
();
curr
.
lock
.
writeLock
().
lock
();
boolean
isRemoved
;
if
(
curr
.
object
.
equals
(
obj
))
{
prev
.
next
=
curr
.
next
;
isRemoved
=
true
;
}
else
{
isRemoved
=
false
;
}
prev
.
lock
.
writeLock
().
unlock
();
curr
.
lock
.
writeLock
().
unlock
();
return
isRemoved
;
}
public
void
removeList
(
Integer
[]
toRemoveList
){
for
(
Integer
obj:
toRemoveList
){
safeRemoveElement
(
obj
);
}
}
}
}
src/main/java/SortList.java
View file @
52d97608
public
abstract
class
SortList
{
public
abstract
class
SortList
{
public
Entry
head
;
public
Entry
head
;
public
int
length
;
public
int
successfulContains
=
0
;
public
int
unsuccessfulContains
=
0
;
public
int
successfulRemoves
=
0
;
public
int
unsuccessfulRemoves
=
0
;
public
SortList
()
{
public
SortList
()
{
this
.
head
=
new
Entry
(
Integer
.
MIN_VALUE
);
this
.
head
=
new
Entry
(
Integer
.
MIN_VALUE
);
this
.
head
.
next
=
new
Entry
(
Integer
.
MAX_VALUE
);
this
.
head
.
next
=
new
Entry
(
Integer
.
MAX_VALUE
);
this
.
length
=
2
;
}
}
public
abstract
boolean
add
(
Integer
obj
);
public
abstract
boolean
add
(
Integer
obj
);
...
@@ -19,4 +25,13 @@ public abstract class SortList {
...
@@ -19,4 +25,13 @@ public abstract class SortList {
}
}
}
}
public
boolean
isSorted
(){
Entry
curr
=
this
.
head
;
while
(
curr
.
next
!=
null
){
if
(
curr
.
object
>
curr
.
next
.
object
)
return
false
;
curr
=
curr
.
next
;
}
return
true
;
}
}
}
src/main/java/SyncList.java
View file @
52d97608
...
@@ -18,6 +18,7 @@ public class SyncList extends SortList {
...
@@ -18,6 +18,7 @@ public class SyncList extends SortList {
Entry
newEntry
=
new
Entry
(
obj
);
Entry
newEntry
=
new
Entry
(
obj
);
newEntry
.
next
=
curr
;
newEntry
.
next
=
curr
;
prev
.
next
=
newEntry
;
prev
.
next
=
newEntry
;
this
.
length
++;
return
true
;
return
true
;
}
}
}
}
...
@@ -32,8 +33,11 @@ public class SyncList extends SortList {
...
@@ -32,8 +33,11 @@ public class SyncList extends SortList {
}
}
if
(
curr
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
))
{
prev
.
next
=
curr
.
next
;
prev
.
next
=
curr
.
next
;
this
.
length
--;
successfulRemoves
++;
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulRemoves
++;
return
false
;
return
false
;
}
}
...
@@ -48,8 +52,10 @@ public class SyncList extends SortList {
...
@@ -48,8 +52,10 @@ public class SyncList extends SortList {
curr
=
prev
.
next
;
curr
=
prev
.
next
;
}
}
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
if
(
curr
.
object
.
equals
(
obj
)
||
prev
.
object
.
equals
(
obj
))
{
successfulContains
++;
return
true
;
return
true
;
}
else
{
}
else
{
unsuccessfulContains
++;
return
false
;
return
false
;
}
}
}
}
...
...
src/test/java/RemoveAllThread.java
0 → 100644
View file @
52d97608
public
class
RemoveAllThread
extends
TestThread
implements
Runnable
{
public
RemoveAllThread
(
RandomSequenceGenerator
seq
,
RWLockList
setList
,
int
seqPart
)
{
super
(
seq
,
setList
,
seqPart
);
}
@Override
public
void
run
()
{
((
RWLockList
)
list
).
removeList
(
nums
);
}
}
src/test/java/SyncListTest.java
View file @
52d97608
import
junit.framework.TestCase
;
import
junit.framework.TestCase
;
import
org.junit.Assert
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -29,18 +30,20 @@ public class SyncListTest extends TestCase {
...
@@ -29,18 +30,20 @@ public class SyncListTest extends TestCase {
}
}
int
randLength
=
20_000
;
int
randLength
=
20_000
;
int
numOfThreads
=
8
;
int
randNumsRange
=
80_000
;
public
void
testHelp
(
SortList
sortList
,
String
label
)
{
public
void
testHelp
(
SortList
sortList
,
String
label
)
{
RandomSequenceGenerator
randomSeq
=
new
RandomSequenceGenerator
(
0
,
80_000
);
RandomSequenceGenerator
randomSeq
=
new
RandomSequenceGenerator
(
0
,
randNumsRange
);
List
<
Thread
>
addThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
addThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
containThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
containThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
removeThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
removeThreads
=
new
ArrayList
<
Thread
>();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
i
=
0
;
i
<
numOfThreads
;
i
++)
{
AddThread
addThread
=
new
AddThread
(
randomSeq
,
sortList
,
randLength
/
8
);
AddThread
addThread
=
new
AddThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
ContainThread
containThread
=
new
ContainThread
(
randomSeq
,
sortList
,
randLength
/
8
);
ContainThread
containThread
=
new
ContainThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
RemoveThread
removeThread
=
new
RemoveThread
(
randomSeq
,
sortList
,
randLength
/
8
);
RemoveThread
removeThread
=
new
RemoveThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
addThreads
.
add
(
new
Thread
(
addThread
));
addThreads
.
add
(
new
Thread
(
addThread
));
containThreads
.
add
(
new
Thread
(
containThread
));
containThreads
.
add
(
new
Thread
(
containThread
));
...
@@ -62,6 +65,13 @@ public class SyncListTest extends TestCase {
...
@@ -62,6 +65,13 @@ public class SyncListTest extends TestCase {
System
.
out
.
println
(
label
+
" add execution time: "
+
(
end
-
start
));
System
.
out
.
println
(
label
+
" add execution time: "
+
(
end
-
start
));
// Ensure list is still sorted
Assert
.
assertTrue
(
sortList
.
isSorted
());
// List length after add operations
int
listLengthAfterAdds
=
sortList
.
length
;
System
.
out
.
println
(
"List length after add "
+
listLengthAfterAdds
);
start
=
System
.
currentTimeMillis
();
start
=
System
.
currentTimeMillis
();
containThreads
.
stream
().
forEach
(
e
->
e
.
start
());
containThreads
.
stream
().
forEach
(
e
->
e
.
start
());
...
@@ -76,6 +86,9 @@ public class SyncListTest extends TestCase {
...
@@ -76,6 +86,9 @@ public class SyncListTest extends TestCase {
end
=
System
.
currentTimeMillis
();
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
label
+
" contain execution time: "
+
(
end
-
start
));
System
.
out
.
println
(
label
+
" contain execution time: "
+
(
end
-
start
));
int
totalSuccessfulContains
=
sortList
.
successfulContains
;
int
totalUnsuccessfulContains
=
sortList
.
unsuccessfulContains
;
System
.
out
.
println
(
"Total number of successes found "
+
totalSuccessfulContains
+
", failures found: "
+
totalUnsuccessfulContains
);
start
=
System
.
currentTimeMillis
();
start
=
System
.
currentTimeMillis
();
...
@@ -92,7 +105,91 @@ public class SyncListTest extends TestCase {
...
@@ -92,7 +105,91 @@ public class SyncListTest extends TestCase {
System
.
out
.
println
(
label
+
" remove execution time: "
+
(
end
-
start
));
System
.
out
.
println
(
label
+
" remove execution time: "
+
(
end
-
start
));
// Ensure list is still sorted
Assert
.
assertTrue
(
sortList
.
isSorted
());
// List length after remove operations
int
listLengthAfterRemove
=
sortList
.
length
;
System
.
out
.
println
(
"List length after remove "
+
listLengthAfterRemove
);
int
totalSuccessfulRemoves
=
sortList
.
successfulRemoves
;
int
totalUnsuccessfulRemoves
=
sortList
.
unsuccessfulRemoves
;
System
.
out
.
println
(
"Total number of successes removed "
+
totalSuccessfulRemoves
+
", failures found: "
+
totalUnsuccessfulRemoves
);
}
public
void
testWithRemoveAllHelp
(
RWLockList
sortList
,
String
label
)
{
RandomSequenceGenerator
randomSeq
=
new
RandomSequenceGenerator
(
0
,
randNumsRange
);
List
<
Thread
>
addThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
containThreads
=
new
ArrayList
<
Thread
>();
List
<
Thread
>
removeThreads
=
new
ArrayList
<
Thread
>();
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
AddThread
addThread
=
new
AddThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
ContainThread
containThread
=
new
ContainThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
RemoveAllThread
removeAllThread
=
new
RemoveAllThread
(
randomSeq
,
sortList
,
randLength
/
numOfThreads
);
addThreads
.
add
(
new
Thread
(
addThread
));
containThreads
.
add
(
new
Thread
(
containThread
));
removeThreads
.
add
(
new
Thread
(
removeAllThread
));
}
long
start
=
System
.
currentTimeMillis
();
addThreads
.
stream
().
forEach
(
e
->
e
.
start
());
addThreads
.
stream
().
forEach
(
e
->
{
try
{
e
.
join
();
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
});
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
label
+
" add execution time: "
+
(
end
-
start
));
// Ensure list is still sorted
Assert
.
assertTrue
(
sortList
.
isSorted
());
// List length after add operations
int
listLengthAfterAdds
=
sortList
.
length
;
System
.
out
.
println
(
"List length after add "
+
listLengthAfterAdds
);
start
=
System
.
currentTimeMillis
();
containThreads
.
stream
().
forEach
(
e
->
e
.
start
());
containThreads
.
stream
().
forEach
(
e
->
{
try
{
e
.
join
();
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
});
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
label
+
" contain execution time: "
+
(
end
-
start
));
int
totalSuccessfulContains
=
sortList
.
successfulContains
;
int
totalUnsuccessfulContains
=
sortList
.
unsuccessfulContains
;
System
.
out
.
println
(
"Total number of successes found "
+
totalSuccessfulContains
+
", failures found: "
+
totalUnsuccessfulContains
);
start
=
System
.
currentTimeMillis
();
removeThreads
.
stream
().
forEach
(
e
->
e
.
start
());
removeThreads
.
stream
().
forEach
(
e
->
{
try
{
e
.
join
();
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
});
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
label
+
" remove execution time: "
+
(
end
-
start
));
}
}
...
@@ -109,5 +206,9 @@ public class SyncListTest extends TestCase {
...
@@ -109,5 +206,9 @@ public class SyncListTest extends TestCase {
testHelp
(
rwLockList
,
"RWLockList"
);
testHelp
(
rwLockList
,
"RWLockList"
);
}
}
public
void
testWithRemoveAllRun
(){
RWLockList
rwLockList
=
new
RWLockList
();
testWithRemoveAllHelp
(
rwLockList
,
"RWLockList With Remove All"
);
}
}
}
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