f0873034b4e44be30398a490021607fbf1385fdf
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / FinalizableScheduledExecutorService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import java.util.concurrent.ScheduledThreadPoolExecutor;
11 import java.util.concurrent.TimeUnit;
12
13 /**
14  * A simple ScheduledExecutorService, which shuts down its threads after a period of inactivity. It is safe to not
15  * shutdown this
16  *
17  * @author Robert Varga
18  */
19 final class FinalizableScheduledExecutorService extends ScheduledThreadPoolExecutor {
20
21     private FinalizableScheduledExecutorService(final int maxThreads, final long time, final TimeUnit unit) {
22         super(maxThreads);
23         setKeepAliveTime(time, unit);
24         allowCoreThreadTimeOut(true);
25     }
26
27     static ScheduledThreadPoolExecutor newSingleThread() {
28         return new FinalizableScheduledExecutorService(1, 15, TimeUnit.SECONDS);
29     }
30
31     // This is a bit ugly, but allows
32     @Override
33     @SuppressWarnings("checkstyle:NoFinalizer")
34     protected void finalize() {
35         super.finalize();
36         super.shutdownNow();
37     }
38 }