Commit f7a214e2 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Concurrent Sorted Linked List Implementations

parents
Pipeline #182 failed with stages
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
<?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
<?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
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="3f4b2924-17d7-4794-bacf-d51c4f0be8bb" name="Changes" comment="Concurrent Sorted Linked List Implementations">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/README.MD" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Entry.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/LockList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/RWLockList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/SortList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/SyncList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/SyncListTest.java" afterDir="false" />
</list>
<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="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2NMPKhBDgdyw9qxmyQBX74HdmhD" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0"
}
}]]></component>
<component name="RunManager">
<configuration name="SyncListTest.testAddList" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="SortLinkedList" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SyncListTest" />
<option name="METHOD_NAME" value="testAddList" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.SyncListTest.testAddList" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="3f4b2924-17d7-4794-bacf-d51c4f0be8bb" name="Changes" comment="" />
<created>1679470917075</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1679470917075</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="Concurrent Sorted Linked List Implementations" />
<option name="LAST_COMMIT_MESSAGE" value="Concurrent Sorted Linked List Implementations" />
</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;
}
}
}
import junit.framework.TestCase;
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();
}
}
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