您可以注册一个javax.management.NotificationListener,当达到某个阈值时调用该方法。
就像是
final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();final NotificationEmitter ne = (NotificationEmitter) memBean;ne.addNotificationListener(listener, null, null);final List<MemoryPoolMXBean> memPools = ManagementFactory .getMemoryPoolMXBeans();for (final MemoryPoolMXBean mp : memPools) { if (mp.isUsageThresholdSupported()) { final MemoryUsage mu = mp.getUsage(); final long max = mu.getMax(); final long alert = (max * threshold) / 100; mp.setUsageThreshold(alert); }}侦听器是您自己的NotificationListener实现。



