groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: GROOVY-9145: Add DGSM `dumpAll` to `Thread`
Date Mon, 27 May 2019 14:01:08 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new af69a7b  GROOVY-9145: Add DGSM `dumpAll` to `Thread`
af69a7b is described below

commit af69a7b36fb92d06b96b2cbbb24d8dff03e6d13c
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sun May 26 23:51:43 2019 +0800

    GROOVY-9145: Add DGSM `dumpAll` to `Thread`
---
 .../groovy/runtime/DefaultGroovyStaticMethods.java   | 20 +++++++++++++++++++-
 .../runtime/DefaultGroovyStaticMethodsTest.groovy    |  5 ++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
index 7d94771..39cf13c 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
@@ -24,13 +24,18 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.Locale;
 import java.util.ResourceBundle;
 import java.util.TimeZone;
 import java.util.regex.Matcher;
+import java.util.stream.Collectors;
 
 /**
  * This class defines all the new static groovy methods which appear on normal
@@ -99,6 +104,20 @@ public class DefaultGroovyStaticMethods {
     }
 
     /**
+     * Dump the thread dump of all threads
+     *
+     * @param self    placeholder variable used by Groovy categories; ignored for default
static methods
+     * @return the thread dump of all threads
+     * @since 3.0.0
+     */
+    public static String dumpAll(Thread self){
+        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
+        return Arrays.stream(threadMxBean.dumpAllThreads(true, true))
+                .map(ThreadInfo::toString)
+                .collect(Collectors.joining(""));
+    }
+
+    /**
      * Get the last hidden matcher that the system used to do a match.
      *
      * @param self placeholder variable used by Groovy categories; ignored for default static
methods
@@ -262,5 +281,4 @@ public class DefaultGroovyStaticMethods {
     public static long currentTimeSeconds(System self){
     return System.currentTimeMillis() / 1000;
   }
-
 }
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
index f19b8c3..ba9d3e8 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
@@ -21,7 +21,6 @@ package org.codehaus.groovy.runtime
  * Tests for DefaultGroovyStaticMethods
  */
 class DefaultGroovyStaticMethodsTest extends GroovyTestCase {
-
     void testCurrentTimeSeconds() {
 	    long timeMillis = System.currentTimeMillis()
         long timeSeconds = System.currentTimeSeconds()
@@ -29,4 +28,8 @@ class DefaultGroovyStaticMethodsTest extends GroovyTestCase {
         assert timeMillis/1000 as int <= timeSeconds
         assert timeMillis2/1000 as int >= timeSeconds
     }
+
+    void testDumpAll() {
+        assert Thread.dumpAll().contains("dumpAll")
+    }
 }


Mime
View raw message