JAVA-2111: Split or move core-java-jvm module (#9688)
* JAVA-2111: Moved boolean Memory Layout article * JAVA-2111: Moved Shutdown Hooks article * JAVA-2111: Updated README files * JAVA-2111: Fixed prev link
This commit is contained in:
@@ -2,4 +2,10 @@
|
||||
|
||||
This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
|
||||
### Relevant Articles:
|
||||
### Relevant Articles:
|
||||
|
||||
- [Memory Layout of Objects in Java](https://www.baeldung.com/java-memory-layout)
|
||||
- [Measuring Object Sizes in the JVM](https://www.baeldung.com/jvm-measuring-object-sizes)
|
||||
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
||||
- [boolean and boolean[] Memory Layout in the JVM](https://www.baeldung.com/jvm-boolean-memory-layout)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.boolsize;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.vm.VM;
|
||||
|
||||
public class BooleanSizeUnitTest {
|
||||
|
||||
@Test
|
||||
public void printingTheVMDetails() {
|
||||
System.out.println(VM.current().details());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printingTheBoolWrapper() {
|
||||
System.out.println(ClassLayout.parseClass(BooleanWrapper.class).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printingTheBoolArray() {
|
||||
boolean[] value = new boolean[3];
|
||||
|
||||
System.out.println(ClassLayout.parseInstance(value).toPrintable());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.boolsize;
|
||||
|
||||
class BooleanWrapper {
|
||||
private boolean value;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.shutdownhook;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class ShutdownHookUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAHook_WhenShutsDown_ThenHookShouldBeExecuted() {
|
||||
Thread printingHook = new Thread(() -> System.out.println("In the middle of a shutdown"));
|
||||
Runtime.getRuntime().addShutdownHook(printingHook);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingAHook_WhenThreadAlreadyStarted_ThenThrowsAnException() {
|
||||
Thread longRunningHook = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException ignored) {}
|
||||
});
|
||||
longRunningHook.start();
|
||||
|
||||
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(longRunningHook))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Hook already running");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addingAHook_WhenAlreadyExists_ThenAnExceptionWouldBeThrown() {
|
||||
Thread unfortunateHook = new Thread(() -> {});
|
||||
Runtime.getRuntime().addShutdownHook(unfortunateHook);
|
||||
|
||||
assertThatThrownBy(() -> Runtime.getRuntime().addShutdownHook(unfortunateHook))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Hook previously registered");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAHook_WhenItIsAlreadyRegistered_ThenWouldDeRegisterTheHook() {
|
||||
Thread willNotRun = new Thread(() -> System.out.println("Won't run!"));
|
||||
Runtime.getRuntime().addShutdownHook(willNotRun);
|
||||
|
||||
assertThat(Runtime.getRuntime().removeShutdownHook(willNotRun)).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user