SingltonJAVA

This commit is contained in:
2024-09-25 16:44:47 -04:00
parent 9d5ed95bf7
commit d6d3ccd4df
16 changed files with 142 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/SingletonClasses.iml" filepath="$PROJECT_DIR$/SingletonClasses.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
+28
View File
@@ -0,0 +1,28 @@
package SingletonClasses;
public class LogManagement {
// First we create an instance of the class
private static LogManagement logM;
// Second, we create a PRIVATE constructor (PRIVATE TO AVOID INSTANTIATION)
private LogManagement() {
}
// We create public method to access the one and ONLY instance
public static LogManagement FuncLog() {
if (logM == null) {
synchronized (LogManagement.class) {
if (logM == null) {
logM = new LogManagement();
}
}
}
return logM;
}
public void Logs() {
System.out.println("Detailed logs on the way");
}
}
+11
View File
@@ -0,0 +1,11 @@
package SingletonClasses;
/**
* LogReader
*/
public class LogReading {
public static void main(String[] args) {
LogManagement.FuncLog().Logs();
}
}
+35
View File
@@ -0,0 +1,35 @@
package SingletonClasses;
public class MultiThreads {
public static void main(String[] args) {
Runnable tast = () -> {
LogManagement.FuncLog().Logs();
};
Thread thread1 = new Thread(tast);
Thread thread2 = new Thread(tast);
Thread thread3 = new Thread(tast);
Thread thread4 = new Thread(tast);
Thread thread5 = new Thread(tast);
Thread thread6 = new Thread(tast);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
try{
thread1.join();
thread2.join();
thread3.join();
thread4.join();
thread5.join();
thread6.join();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<?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$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="SingletonClasses" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/SingletonClasses.iml" filepath="$PROJECT_DIR$/SingletonClasses.iml" />
</modules>
</component>
</project>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
@@ -0,0 +1,11 @@
<?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$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="SingletonClasses" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>