如何在IntelliJ中实现终端time命令的Java程序计时功能?
Absolutely! You can totally replicate the time command's exact output (including real, user, and sys times) right in IntelliJ IDEA. Here are a few straightforward ways to do it:
Option 1: Use IntelliJ's Built-in Terminal (Easiest)
- Just open the Terminal tab at the bottom of IntelliJ (hit
Alt+F12if it's hidden, or go to View > Tool Windows > Terminal). - Paste your exact command and run it:
time java -Djava.util.concurrent.ForkJoinPool.common.parallelism=40 TestingNumberOfThreads - You’ll get the exact same output as your terminal:
real 0m4.356s user 0m46.273s sys 0m0.702s
Option 2: Create a Custom Run Configuration
If you prefer using IntelliJ’s run buttons instead of typing commands every time:
- Go to Run > Edit Configurations.
- Either select your existing Java app config or click the
+to create a new one. - Under the Before launch section, click
+and choose Run External Tool. - Create a new External Tool with these settings (for Linux/macOS):
- Name: Time Wrapper
- Program:
/bin/bash - Arguments:
-c "time java -Djava.util.concurrent.ForkJoinPool.common.parallelism=40 TestingNumberOfThreads" - Working directory: Set this to your project’s output directory (e.g.,
$MODULE_DIR$/targetor$PROJECT_DIR$/out/production/classesto ensure it finds your compiledTestingNumberOfThreadsclass).
- Save the configuration, then run it—you’ll see the
timecommand’s output in the Run tool window.
Option 3: Embed Timing Directly in Your Code
If you want the timing to be part of your program’s output, you can use Java’s management APIs to capture user and system CPU times:
import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; public class TestingNumberOfThreads { public static void main(String[] args) { // Capture start times long realStart = System.currentTimeMillis(); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long totalUserStart = threadBean.getCurrentThreadUserTime(); long totalSysStart = threadBean.getCurrentThreadCpuTime() - totalUserStart; // --- Your program logic goes here --- // Capture end times and calculate durations long realEnd = System.currentTimeMillis(); long totalUserEnd = threadBean.getCurrentThreadUserTime(); long totalSysEnd = threadBean.getCurrentThreadCpuTime() - totalUserEnd; long realMs = realEnd - realStart; long userMs = (totalUserEnd - totalUserStart) / 1_000_000; // Convert nanos to ms long sysMs = (totalSysEnd - totalSysStart) / 1_000_000; // Format output to match the `time` command System.out.printf("real %dm%03ds user %dm%03ds sys %dm%03ds%n", realMs / 60000, (realMs % 60000) / 1000, userMs / 60000, (userMs % 60000) / 1000, sysMs / 60000, (sysMs % 60000) / 1000); } }
Note: This example tracks just the main thread’s CPU time. If your ForkJoinPool uses multiple threads, you can modify it to sum up CPU times across all active threads using threadBean.getAllThreadIds() for a more accurate match to the time command’s total user/sys values.
内容的提问来源于stack exchange,提问作者Neeraj Jain

