forked from dan200/ComputerCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Put all ComputerCraft threads into a thread group - Be a little more aggressive in when we cleanup/abandon threads.
- Loading branch information
Showing
4 changed files
with
98 additions
and
70 deletions.
There are no files selected for viewing
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
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
68 changes: 68 additions & 0 deletions
68
src/main/java/dan200/computercraft/shared/util/ThreadUtils.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,68 @@ | ||
/* | ||
* This file is part of ComputerCraft - http://www.computercraft.info | ||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission. | ||
* Send enquiries to [email protected] | ||
*/ | ||
|
||
package dan200.computercraft.shared.util; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* Provides some utilities to create thread groups | ||
*/ | ||
public final class ThreadUtils | ||
{ | ||
private static final ThreadGroup baseGroup = new ThreadGroup( "ComputerCraft" ); | ||
|
||
private ThreadUtils() | ||
{ | ||
} | ||
|
||
/** | ||
* Construct a group under ComputerCraft's shared group | ||
* | ||
* @param name The group's name. This will be prefixed with "ComputerCraft-". | ||
* @return The constructed thread group. | ||
*/ | ||
public static ThreadGroup group( String name ) | ||
{ | ||
return new ThreadGroup( baseGroup, baseGroup.getName() + "-" + name ); | ||
} | ||
|
||
/** | ||
* Create a new {@link ThreadFactoryBuilder}, which constructs threads under a group of the given {@code name}. | ||
* | ||
* Each thread will be of the format {@code ComputerCraft-<name>-<number>}, and belong to a group | ||
* called {@code ComputerCraft-<name>} (which in turn will be a child group of the main {@code ComputerCraft} group. | ||
* | ||
* @param name The name for the thread group and child threads. | ||
* @return The constructed thread factory builder, which may be extended with other properties. | ||
* @see #factory(String) | ||
*/ | ||
public static ThreadFactoryBuilder builder( String name ) | ||
{ | ||
ThreadGroup group = group( name ); | ||
return new ThreadFactoryBuilder() | ||
.setDaemon( true ) | ||
.setNameFormat( group.getName().replace( "%", "%%" ) + "-%d" ) | ||
.setThreadFactory( x -> new Thread( group, x ) ); | ||
} | ||
|
||
/** | ||
* Create a new {@link ThreadFactory}, which constructs threads under a group of the given {@code name}. | ||
* | ||
* Each thread will be of the format {@code ComputerCraft-<name>-<number>}, and belong to a group | ||
* called {@code ComputerCraft-<name>} (which in turn will be a child group of the main {@code ComputerCraft} group. | ||
* | ||
* @param name The name for the thread group and child threads. | ||
* @return The constructed thread factory. | ||
* @see #builder(String) | ||
*/ | ||
public static ThreadFactory factory( String name ) | ||
{ | ||
return builder( name ).build(); | ||
} | ||
} |