ThreadManager.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright 2017 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. * 2019.12.15-changed method getHuaweiExecutors
  15. * 2019.12.15-changed method releaseHuaweiExecutors
  16. * 2019.12.15-changed method getExecutor
  17. * 2019.12.15-changed method releaseExecutor
  18. * Huawei Technologies Co., Ltd.
  19. *
  20. */
  21. package cn.seecoder.courselearning.messaging;
  22. import cn.seecoder.courselearning.util.ValidatorUtils;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.ThreadFactory;
  25. /**
  26. * An interface that controls the thread pools and thread factories used by the HCM SDK. Each
  27. * instance of HuaweiApp uses an implementation of this interface to create and manage
  28. * threads.
  29. */
  30. public abstract class ThreadManager {
  31. final HuaweiExecutors getHuaweiExecutors(HuaweiApp app) {
  32. return new HuaweiExecutors(getExecutor(app));
  33. }
  34. final void releaseHuaweiExecutors(HuaweiApp app, HuaweiExecutors executor) {
  35. releaseExecutor(app, executor.userExecutor);
  36. }
  37. /**
  38. * Returns the main thread pool for an app.
  39. *
  40. * @param app A {@link HuaweiApp} instance.
  41. * @return A non-null <code>ExecutorService</code> instance.
  42. */
  43. protected abstract ExecutorService getExecutor(HuaweiApp app);
  44. /**
  45. * Cleans up the thread pool associated with an app.
  46. * This method is invoked when an app is deleted.
  47. *
  48. * @param app A {@link HuaweiApp} instance.
  49. * @return A non-null <code>ExecutorService</code> instance.
  50. */
  51. protected abstract void releaseExecutor(HuaweiApp app, ExecutorService executor);
  52. /**
  53. * Returns the <code>ThreadFactory</code> to be used for creating long-lived threads. This is
  54. * used mainly to create the long-lived worker threads for the scheduled (periodic) tasks started by the SDK.
  55. * The SDK guarantees clean termination of all threads started via this <code>ThreadFactory</code>, when the user
  56. * calls {@link HuaweiApp#delete()}.
  57. *
  58. * <p>If long-lived threads cannot be supported in the current runtime, this method may
  59. * throw a {@code RuntimeException}.
  60. *
  61. * @return A non-null <code>ThreadFactory</code>.
  62. */
  63. protected abstract ThreadFactory getThreadFactory();
  64. static final class HuaweiExecutors {
  65. private ExecutorService userExecutor;
  66. private HuaweiExecutors(ExecutorService userExecutor) {
  67. ValidatorUtils.checkArgument(userExecutor != null, "ExecutorService must not be null");
  68. this.userExecutor = userExecutor;
  69. }
  70. }
  71. }