Commit 65490e00 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Multithreading Lab

parents
# Default ignored files
/shelf/
/workspace.xml
<?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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Multithreading" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/Multithreading/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/Multithreading/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="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</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$/Multithreading/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" 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="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Lab1.iml" filepath="$PROJECT_DIR$/.idea/Lab1.iml" />
</modules>
</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
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_21" default="true" project-jdk-name="21 (5)" 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="6c90059e-2416-4f80-a937-eccbcfc403af" name="Changes" comment="First Commit">
<change afterPath="$PROJECT_DIR$/src/main/java/org/multithreading/StudentServiceRunnable.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/multithreading/StudentServiceThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/StudentServiceTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/multithreading/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/multithreading/Main.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="ProblemsViewState">
<option name="selectedTabId" value="CurrentFile" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 8
}</component>
<component name="ProjectId" id="2mYJsAccO7ZsbFqQYap6Iz52M05" />
<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": {
"Application.Main.executor": "Debug",
"Downloaded.Files.Path.Enabled": "false",
"JUnit.StudentServiceTest.executor": "Run",
"JUnit.StudentServiceTest.testMultiStudentService.executor": "Run",
"JUnit.StudentServiceTest.testStudentService.executor": "Run",
"JUnit.StudentServiceTest.testStudentServiceUsingRunnable.executor": "Debug",
"JUnit.StudentServiceTest.testStudentServiceUsingThread.executor": "Run",
"Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"git-widget-placeholder": "master",
"kotlin-language-version-configured": "true",
"last_directory_selection": "D:/HIAST Library/Teaching/PP/2025/Labs/Multithreading/src/main/java/org/multithreading",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.2"
}
}]]></component>
<component name="RecentsManager">
<key name="MoveClassesOrPackagesDialog.RECENTS_KEY">
<recent name="" />
</key>
</component>
<component name="RunManager" selected="JUnit.StudentServiceTest">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="org.multithreading.Main" />
<module name="Multithreading" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value=".*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudentServiceTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Multithreading" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="StudentServiceTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudentServiceTest.testMultiStudentService" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Multithreading" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="StudentServiceTest" />
<option name="METHOD_NAME" value="testMultiStudentService" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudentServiceTest.testStudentServiceUsingRunnable" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Multithreading" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="StudentServiceTest" />
<option name="METHOD_NAME" value="testStudentServiceUsingRunnable" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="StudentServiceTest.testStudentServiceUsingThread" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Multithreading" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="StudentServiceTest" />
<option name="METHOD_NAME" value="testStudentServiceUsingThread" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.StudentServiceTest" />
<item itemvalue="Application.Main" />
<item itemvalue="JUnit.StudentServiceTest.testStudentServiceUsingRunnable" />
<item itemvalue="JUnit.StudentServiceTest.testStudentServiceUsingThread" />
<item itemvalue="JUnit.StudentServiceTest.testMultiStudentService" />
</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="6c90059e-2416-4f80-a937-eccbcfc403af" name="Changes" comment="" />
<created>1727247780998</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1727247780998</updated>
</task>
<task id="LOCAL-00001" summary="First Commit">
<option name="closed" value="true" />
<created>1727249181181</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1727249181181</updated>
</task>
<option name="localTasksCounter" value="2" />
<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">
<MESSAGE value="First Commit" />
<option name="LAST_COMMIT_MESSAGE" value="First Commit" />
</component>
</project>
\ No newline at end of file
### Multithreading in Java
*topics:*
* Why Multithreading
* Create Thread
- Thread class
- Runnable interface
* Performance Optimization
*Some Useful links:*
- [Automation Test - Junit](https://www.tutorialspoint.com/junit/junit_test_framework.htm)
- [Multithreading in Java](https://www.youtube.com/watch?v=F2TkIojBdws&list=PLXBkY9OPPGU4ahU1AVue3FMd3wNaZU9Fc&index=1&ab_channel=AlternativeDegree)
<?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.multithreading</groupId>
<artifactId>Multithreading</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package org.multithreading;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<StudentServiceThread> studentServiceThreadList = new ArrayList<>();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
Student student = new Student("test " + i);
// System.out.println("Current Thread: "+Thread.currentThread().getName());
StudentService studentService = new StudentService();
StudentServiceThread studentServiceThread = new StudentServiceThread(student, studentService);
studentServiceThread.start();
studentServiceThreadList.add(studentServiceThread);
}
for (StudentServiceThread studentServiceThread : studentServiceThreadList) {
try {
studentServiceThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// for (int i = 0; i < 10; i++) {
//
// studentService.saveStudent(student);
// }
long duration = System.currentTimeMillis() - startTime;
System.out.println("Time Taken: " + duration);
}
}
\ No newline at end of file
package org.multithreading;
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.multithreading;
import java.util.UUID;
public class StudentService {
public String saveStudent(Student std) {
System.out.println("Saving Student: " + std.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return UUID.randomUUID().toString();
}
}
package org.multithreading;
public class StudentServiceRunnable implements Runnable {
private Student student;
private StudentService studentService;
public StudentServiceRunnable(Student student, StudentService studentService) {
this.student = student;
this.studentService = studentService;
}
@Override
public void run() {
System.out.println("Current Thread: " + Thread.currentThread().getName());
studentService.saveStudent(student);
}
}
package org.multithreading;
public class StudentServiceThread extends Thread {
private Student student;
private StudentService studentService;
public StudentServiceThread(Student student, StudentService studentService) {
this.student = student;
this.studentService = studentService;
}
@Override
public void run() {
System.out.println("Current Thread: "+Thread.currentThread().getName());
studentService.saveStudent(student);
}
}
import junit.framework.TestCase;
import org.multithreading.Student;
import org.multithreading.StudentService;
import org.multithreading.StudentServiceRunnable;
import org.multithreading.StudentServiceThread;
import java.util.ArrayList;
import java.util.List;
public class StudentServiceTest extends TestCase {
public void testStudentService()
{
long StartTime= System.currentTimeMillis();
Student student = new Student("test");
StudentService svc = new StudentService();
svc.saveStudent(student);
long end = System.currentTimeMillis()- StartTime;
System.out.println("Time Taken: "+ end);
}
public void testMultiStudentService()
{
long StartTime= System.currentTimeMillis();
StudentService svc = new StudentService();
for (int i=0; i<10;i++) {
Student student = new Student("test "+i);
svc.saveStudent(student);
}
long end = System.currentTimeMillis()- StartTime;
System.out.println("Time Taken: "+ end);
}
public void testStudentServiceUsingThread()
{
long StartTime= System.currentTimeMillis();
System.out.println("The name of the current name is: "+ Thread.currentThread().getName());
StudentService svc = new StudentService();
Student student = new Student("test");
StudentServiceThread studentServiceThread = new StudentServiceThread(student, svc);
studentServiceThread.setName("MyTestThread");
studentServiceThread.start();
// studentServiceThread.run();
try {
studentServiceThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long end = System.currentTimeMillis()- StartTime;
System.out.println("Time Taken: "+ end);
}
public void testStudentServiceUsingThreadsNotReally()
{
long StartTime= System.currentTimeMillis();
StudentService svc = new StudentService();
for (int i= 0; i<10;i++) {
Student student = new Student("test "+i);
StudentServiceThread studentServiceThread = new StudentServiceThread(student, svc);
studentServiceThread.setName("MyTestThread"+i);
studentServiceThread.start();
try {
studentServiceThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// studentServiceThread.run();
long end = System.currentTimeMillis()- StartTime;
System.out.println("Time Taken: "+ end);
}
public void testStudentServiceUsingThreads()
{
long StartTime= System.currentTimeMillis();
StudentService svc = new StudentService();
List<StudentServiceThread> studentServiceThreadList = new ArrayList<>();
for (int i= 0; i<10;i++) {
Student student = new Student("test "+i);
StudentServiceThread studentServiceThread = new StudentServiceThread(student, svc);
studentServiceThread.setName("MyTestThread"+i);
studentServiceThread.start();
studentServiceThreadList.add(studentServiceThread);
}
// studentServiceThread.run();
for(StudentServiceThread studentServiceThread: studentServiceThreadList){
try {
studentServiceThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long end = System.currentTimeMillis()- StartTime;
System.out.println("Time Taken: "+ end);
}
public void testStudentServiceUsingRunnable()
{
StudentService studentService = new StudentService();
Student student = new Student("test");
StudentServiceRunnable studentServiceRunnable = new StudentServiceRunnable(student, studentService);
Thread th = new Thread(studentServiceRunnable);
th.start();
try {
th.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void testStudentServiceUsingAnonymousRunnable()
{
StudentService studentService = new StudentService();
Student student = new Student("test");
// StudentServiceRunnable studentServiceRunnable = new StudentServiceRunnable(studentService,student);
Thread th = new Thread(new Runnable() {
@Override
public void run() {
studentService.saveStudent(student);
}
});
th.start();
try {
th.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void testStudentServiceUsingLambdaRunnable()
{
StudentService studentService = new StudentService();
Student student = new Student("test");
// StudentServiceRunnable studentServiceRunnable = new StudentServiceRunnable(studentService,student);
Thread th = new Thread(() -> studentService.saveStudent(student));
th.start();
try {
th.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
### Multithreading in Java
*topics:*
* Multithreading Lab
* Homework Question 2 Code
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