Commit ef4a140b authored by MoutazDebbaneh's avatar MoutazDebbaneh

Initial commit

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
# Concurrent Sorted Linked List
## Introduction
We try to implement a concurrent sorted linked list in different manners to get thread-safe and suitable performance on this data structure.
## Content
* Data Race.
* Mutual Exclusion.
* Structured Lock (synchronized).
* Unstructured Lock (ReentrantLock, ReentrantReadWriteLock).
* Test different implementations' performance.
## TO-DO
* Test throughput (operations per second) for different Sorted Linked List Implementations.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SortLinkedList</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
public class Entry {
public Integer object;
public Entry next;
public Entry(Integer object) {
this.object = object;
}
}
import java.util.concurrent.locks.ReentrantLock;
public class LockList extends SortList {
ReentrantLock lock = new ReentrantLock();
public LockList() {
super();
}
@Override
public boolean add(Integer obj) {
try {
lock.lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return false;
} else {
Entry newEntry = new Entry(obj);
newEntry.next = curr;
prev.next = newEntry;
return true;
}
} finally {
lock.unlock();
}
}
@Override
public boolean remove(Integer obj) {
try {
lock.lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj)) {
prev.next = curr.next;
return true;
} else {
return false;
}
} finally {
lock.unlock();
}
}
@Override
public boolean contain(Integer obj) {
try {
lock.lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return true;
} else {
return false;
}
} finally {
lock.unlock();
}
}
}
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class RWLockList extends SortList {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public RWLockList() {
super();
}
@Override
public boolean add(Integer obj) {
try {
lock.writeLock().lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return false;
} else {
Entry newEntry = new Entry(obj);
newEntry.next = curr;
prev.next = newEntry;
return true;
}
} finally {
lock.writeLock().unlock();
}
}
@Override
public boolean remove(Integer obj) {
try {
lock.writeLock().lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj)) {
prev.next = curr.next;
return true;
} else {
return false;
}
} finally {
lock.writeLock().unlock();
}
}
@Override
public boolean contain(Integer obj) {
try {
lock.readLock().lock();
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return true;
} else {
return false;
}
} finally {
lock.readLock().unlock();
}
}
}
public abstract class SortList {
public Entry head;
public SortList() {
this.head = new Entry(Integer.MIN_VALUE);
this.head.next =new Entry(Integer.MAX_VALUE);
}
public abstract boolean add(Integer obj);
public abstract boolean remove(Integer obj);
public abstract boolean contain(Integer obj);
public void printList(){
Entry curr = this.head;
while (curr != null){
System.out.println(curr.object);
curr = curr.next;
}
}
}
public class SyncList extends SortList {
public SyncList() {
super();
}
@Override
public synchronized boolean add(Integer obj) {
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return false;
} else {
Entry newEntry = new Entry(obj);
newEntry.next = curr;
prev.next = newEntry;
return true;
}
}
@Override
public synchronized boolean remove(Integer obj) {
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj)) {
prev.next = curr.next;
return true;
} else {
return false;
}
}
@Override
public synchronized boolean contain(Integer obj) {
Entry prev = this.head;
Entry curr = prev.next;
while (curr.object.compareTo(obj) < 0) {
prev = curr;
curr = prev.next;
}
if (curr.object.equals(obj) || prev.object.equals(obj)) {
return true;
} else {
return false;
}
}
}
public class AddThread extends TestThread implements Runnable {
public AddThread(RandomSequenceGenerator seq, SortList setList, int seqPart) {
super(seq, setList, seqPart);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
}
}
public class ContainThread extends TestThread implements Runnable {
public ContainThread(RandomSequenceGenerator seq, SortList setList, int seqPart) {
super(seq, setList, seqPart);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.contain(nums[i]);
}
}
}
import java.util.Random;
public class RandomSequenceGenerator {
int seed;
Random random;
int maxNum;
public RandomSequenceGenerator(int setSeed, int setMaxNum) {
random = new Random(setSeed);
this.seed = setSeed;
this.maxNum = setMaxNum;
}
public int next(){
return random.nextInt(maxNum);
}
}
public class RemoveThread extends TestThread implements Runnable {
public RemoveThread(RandomSequenceGenerator seq, SortList setList, int seqPart) {
super(seq, setList, seqPart);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.remove(nums[i]);
}
}
}
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class SyncListTest extends TestCase {
public void testAddList() {
SyncList syncList = new SyncList();
syncList.add(1);
syncList.add(2);
syncList.add(3);
syncList.add(Integer.MIN_VALUE);
syncList.add(3);
System.out.println(syncList.contain(5));
System.out.println(syncList.contain(2));
syncList.remove(3);
syncList.printList();
}
public void testRandomGenerator() {
RandomSequenceGenerator randomSequenceGenerator = new RandomSequenceGenerator(0, 1000);
for (int i = 0; i < 10; i++) {
System.out.println(randomSequenceGenerator.next() + " ");
}
}
int randLength = 20_000;
public void testHelp(SortList sortList, String label) {
RandomSequenceGenerator randomSeq = new RandomSequenceGenerator(0, 80_000);
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/8 );
ContainThread containThread = new ContainThread(randomSeq, sortList,randLength/8 );
RemoveThread removeThread = new RemoveThread(randomSeq, sortList,randLength/8 );
addThreads.add(new Thread(addThread));
containThreads.add(new Thread(containThread));
removeThreads.add(new Thread(removeThread));
}
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));
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));
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));
}
public void testRun(){
SyncList syncList = new SyncList();
LockList lockList = new LockList();
RWLockList rwLockList = new RWLockList();
testHelp(syncList, "Synchronized");
System.out.println("\n**********************************************************\n");
testHelp(lockList, "Locked");
System.out.println("\n**********************************************************\n");
testHelp(rwLockList, "RWLockList");
}
}
public abstract class TestThread {
protected SortList list;
protected Integer [] nums;
public TestThread(RandomSequenceGenerator seq, SortList setList, int seqPart) {
this.list = setList;
this.nums = new Integer[seqPart];
for(int i=0;i<nums.length;i++){
nums[i] = seq.next();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment