BAEL-6428 Make a Method Execute Only Once in Java
This commit is contained in:
parent
f9da3f6b9c
commit
d065736bf4
|
@ -0,0 +1,6 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Methods
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Execute a Method Only Once in Java](https://www.baeldung.com/execute-a-method-only-once-in-java)
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?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>
|
||||||
|
<artifactId>core-java-methods</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-methods</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-methods</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<assertj.version>3.22.0</assertj.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
final class AtomicBooleanInitializer {
|
||||||
|
|
||||||
|
private final AtomicBoolean isInitialized = new AtomicBoolean(false);
|
||||||
|
int callCount = 0;
|
||||||
|
|
||||||
|
void initialize() {
|
||||||
|
if (isInitialized.compareAndSet(false, true)) {
|
||||||
|
initializationLogic();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializationLogic() {
|
||||||
|
callCount++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
final class StaticInitializer {
|
||||||
|
|
||||||
|
static int CALL_COUNT = 0;
|
||||||
|
|
||||||
|
static {
|
||||||
|
initializationLogic();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initializationLogic() {
|
||||||
|
CALL_COUNT++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
final class SynchronizedInitializer {
|
||||||
|
|
||||||
|
private static volatile boolean isInitialized = false;
|
||||||
|
int callCount = 0;
|
||||||
|
|
||||||
|
synchronized void initialize() {
|
||||||
|
if (!isInitialized) {
|
||||||
|
initializationLogic();
|
||||||
|
isInitialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializationLogic() {
|
||||||
|
callCount++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
final class AtomicBooleanInitializerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAtomicBooleanInitializer_whenRepeatedlyCallingInitialize_thenCallCountIsOne() {
|
||||||
|
AtomicBooleanInitializer atomicBooleanInitializer = new AtomicBooleanInitializer();
|
||||||
|
assertEquals(0, atomicBooleanInitializer.callCount);
|
||||||
|
|
||||||
|
atomicBooleanInitializer.initialize();
|
||||||
|
atomicBooleanInitializer.initialize();
|
||||||
|
atomicBooleanInitializer.initialize();
|
||||||
|
|
||||||
|
assertEquals(1, atomicBooleanInitializer.callCount);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
final class StaticInitializerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenLoadingStaticInitializer_thenCallCountIsOne() {
|
||||||
|
assertEquals(1, StaticInitializer.CALL_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenInitializingStaticInitializer_thenCallCountStaysOne() {
|
||||||
|
StaticInitializer staticInitializer = new StaticInitializer();
|
||||||
|
assertEquals(1, StaticInitializer.CALL_COUNT);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.executeonce;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
final class SynchronizedInitializerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenSynchronizedInitializer_whenRepeatedlyCallingInitialize_thenCallCountIsOne() {
|
||||||
|
SynchronizedInitializer synchronizedInitializer = new SynchronizedInitializer();
|
||||||
|
assertEquals(0, synchronizedInitializer.callCount);
|
||||||
|
|
||||||
|
synchronizedInitializer.initialize();
|
||||||
|
synchronizedInitializer.initialize();
|
||||||
|
synchronizedInitializer.initialize();
|
||||||
|
|
||||||
|
assertEquals(1, synchronizedInitializer.callCount);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue