Make the SlackAPI thread-safe in its current implementation

This commit is contained in:
Alexander Meech 2018-05-31 22:06:12 -04:00
parent e47df41225
commit d7db64da84
1 changed files with 6 additions and 9 deletions

View File

@ -23,13 +23,14 @@ public class SlackAPI
public static final String DEFAULT_ICON = ":mineplex:"; public static final String DEFAULT_ICON = ":mineplex:";
// Singular instance. // Singular instance.
private static SlackAPI _instance; private static final SlackAPI _instance = new SlackAPI();
// Don't allow instantiation elsewhere. // Don't allow instantiation elsewhere.
private SlackAPI() {} private SlackAPI() {}
/** /**
* Sends a message asynchronously to a Slack channel. * <p>Sends a message to a Slack channel</p>
* <p>Will be run asynchronously if called from the main thread, else it will execute immediately on the current thread</p>
* *
* @param team The team which contains the target channel. * @param team The team which contains the target channel.
* @param channel The target channel for the message. * @param channel The target channel for the message.
@ -59,7 +60,8 @@ public class SlackAPI
if (Bukkit.isPrimaryThread()) if (Bukkit.isPrimaryThread())
{ {
ThreadPool.ASYNC.execute(send); ThreadPool.ASYNC.execute(send);
} else }
else
{ {
send.run(); send.run();
} }
@ -132,11 +134,6 @@ public class SlackAPI
*/ */
public static SlackAPI getInstance() public static SlackAPI getInstance()
{ {
if (_instance == null)
{
_instance = new SlackAPI();
}
return _instance; return _instance;
} }
} }