-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cody Littley <cody@swirldslabs.com>
- Loading branch information
1 parent
12414d9
commit b1b815d
Showing
3 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/JvmAnchor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.swirlds.platform; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
import com.swirlds.base.state.Startable; | ||
import com.swirlds.base.state.Stoppable; | ||
import com.swirlds.common.threading.framework.config.ThreadConfiguration; | ||
import com.swirlds.common.threading.manager.ThreadManager; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* Creates a non-daemon JVM thread that does nothing. Keeps the JVM alive if all other threads are daemon threads. | ||
*/ | ||
public class JvmAnchor implements Startable, Stoppable { | ||
|
||
private final Thread thread; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param threadManager the thread manager | ||
*/ | ||
public JvmAnchor(@NonNull final ThreadManager threadManager) { | ||
thread = new ThreadConfiguration(threadManager) | ||
.setComponent("platform") | ||
.setThreadName("jvm-anchor") | ||
.setRunnable(this::run) | ||
.setDaemon(false) | ||
.build(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void start() { | ||
thread.start(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void stop() { | ||
thread.interrupt(); | ||
} | ||
|
||
/** | ||
* Runs and does nothing until interrupted. | ||
*/ | ||
private void run() { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
try { | ||
SECONDS.sleep(Integer.MAX_VALUE); | ||
} catch (final InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters