1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 import java.lang.management.*;
  26 import com.sun.management.OperatingSystemMXBean;
  27 import java.util.ArrayList;
  28 import java.util.Iterator;
  29 import java.util.List;
  30 import java.util.Random;
  31 import java.util.concurrent.ExecutorService;
  32 import java.util.concurrent.Executors;
  33 import java.util.concurrent.Future;
  34 
  35 import javax.management.Notification;
  36 import javax.management.NotificationEmitter;
  37 import javax.management.NotificationListener;
  38 
  39 public class ServiceThread {
  40     public static void main(String[] args) throws InterruptedException {
  41 
  42         ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(new NotificationListener() {
  43 
  44             @Override
  45             public void handleNotification(Notification pNotification, Object pHandback) {
  46                 System.out.println("notification...");
  47             }
  48         }, null, null);
  49 
  50         for (MemoryPoolMXBean m : ManagementFactory.getMemoryPoolMXBeans()) {
  51             if (m.getType() == MemoryType.HEAP) {
  52                 MemoryUsage localMemoryUsage = m.getUsage();
  53                 long l = (long) (localMemoryUsage.getMax() == -1L ? 
  54                          Runtime.getRuntime().maxMemory() * 0.9D : localMemoryUsage.getMax() * 0.9D);
  55                 m.setCollectionUsageThreshold(l);
  56             }
  57         }
  58 
  59         ExecutorService pool = Executors.newFixedThreadPool(5);
  60         List<Future<?>> futures = new ArrayList<>();
  61         for (int i = 0; i < 10; i++) {
  62             futures.add(pool.submit(new Runnable() {
  63                 @Override
  64                 public void run() {
  65                     try {
  66                         Random random = new Random();
  67                         List<String> interned = new ArrayList<>();
  68                         while (true) {
  69                             int length = random.nextInt(100);
  70                             StringBuilder builder = new StringBuilder();
  71                             String sample = "whoisyourdaddy";
  72                             for (int i = 0; i < length; i++) {
  73                                 builder.append(sample.charAt(random.nextInt(sample.length())));
  74                             }
  75                             interned.add(builder.toString().intern());
  76                         }
  77 
  78                     } catch (Throwable t) {
  79                         System.out.println(Thread.currentThread().getName() + ": " + t);
  80                         throw t;
  81                     }
  82                 }
  83             }));
  84         }
  85         
  86         int m = 0;
  87         while (m < 2) {
  88             Thread.sleep(60000);
  89             m++;
  90             final Iterator<Future<?>> iter = futures.iterator();
  91             while (iter.hasNext()) {
  92                 Future<?> future = iter.next();
  93                 System.out.println("\tfuture task DONE: " + future.isDone());
  94                 if (future.isDone()) {
  95                     iter.remove();
  96                 }
  97             }
  98             if (futures.isEmpty()) {
  99                 if (!pool.isShutdown()) {
 100                     System.out.println("\tshutting down thread pool...");
 101                     pool.shutdown();
 102                 } 
 103             }
 104         }
 105 
 106         OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
 107         double load = os.getProcessCpuLoad()*100;
 108         System.out.println("Process CPU Load: "+ load);
 109         if (load > 5){
 110             System.out.println("Test Failed...");
 111             System.exit(1);
 112         } else {
 113             System.out.println("Test Passed...");
 114             System.exit(0);
 115         }
 116     }
 117 }
 118