--- /dev/null Wed Jun 7 14:51:59 2017 +++ new/test/runtime/ServiceThread/ServiceThread.java Wed Jun 7 14:51:59 2017 @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +import java.lang.management.*; +import com.sun.management.OperatingSystemMXBean; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; + +public class ServiceThread { + public static void main(String[] args) throws InterruptedException { + + ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(new NotificationListener() { + + @Override + public void handleNotification(Notification pNotification, Object pHandback) { + System.out.println("notification..."); + } + }, null, null); + + for (MemoryPoolMXBean m : ManagementFactory.getMemoryPoolMXBeans()) { + if (m.getType() == MemoryType.HEAP) { + MemoryUsage localMemoryUsage = m.getUsage(); + long l = (long) (localMemoryUsage.getMax() == -1L ? + Runtime.getRuntime().maxMemory() * 0.9D : localMemoryUsage.getMax() * 0.9D); + m.setCollectionUsageThreshold(l); + } + } + + ExecutorService pool = Executors.newFixedThreadPool(5); + List> futures = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + futures.add(pool.submit(new Runnable() { + @Override + public void run() { + try { + Random random = new Random(); + List interned = new ArrayList<>(); + while (true) { + int length = random.nextInt(100); + StringBuilder builder = new StringBuilder(); + String sample = "whoisyourdaddy"; + for (int i = 0; i < length; i++) { + builder.append(sample.charAt(random.nextInt(sample.length()))); + } + interned.add(builder.toString().intern()); + } + + } catch (Throwable t) { + System.out.println(Thread.currentThread().getName() + ": " + t); + throw t; + } + } + })); + } + + int m = 0; + while (m < 2) { + Thread.sleep(60000); + m++; + final Iterator> iter = futures.iterator(); + while (iter.hasNext()) { + Future future = iter.next(); + System.out.println("\tfuture task DONE: " + future.isDone()); + if (future.isDone()) { + iter.remove(); + } + } + if (futures.isEmpty()) { + if (!pool.isShutdown()) { + System.out.println("\tshutting down thread pool..."); + pool.shutdown(); + } + } + } + + OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(); + double load = os.getProcessCpuLoad()*100; + System.out.println("Process CPU Load: "+ load); + if (load > 5){ + System.out.println("Test Failed..."); + System.exit(1); + } else { + System.out.println("Test Passed..."); + System.exit(0); + } + } +} +