Location>code7788 >text

Java implements task manager performance network monitoring data

Popularity:791 ℃/2025-01-13 15:53:11

In modern operating systems, the task manager is a very important tool for monitoring and managing the running status of the computer, including CPU usage, memory usage, disk I/O, network traffic, etc. For developers and system administrators, understanding this performance data can help optimize application and system performance. This article will introduce how to use Java to write a simple program to monitor network performance data, and show how to obtain and display this information.

1. Background knowledge

In Java, monitoring network performance data usually requires relying on the operating system's native API or third-party libraries. The Java standard library itself does not directly provide tools for obtaining network interface statistics. However, it can be done by executing system commands (such as under Linuxifconfigorip -s link, under Windowsnetstat) to parse network data, or use cross-platform third-party libraries such asOshi

Oshi is an open source Java library for obtaining operating system and hardware information, supporting Windows, Linux and macOS. It provides a simple API to obtain the usage of hardware resources such as CPU, memory, disk and network.

2. Preparation work

Before you start writing code, you need to make sure that the Oshi library is included in your development environment. Dependencies can be managed through Maven or Gradle.

1. Maven dependencies

in yourAdd the following dependencies to the file:

<dependency>
    <groupId></groupId>
    <artifactId>oshi-core</artifactId>
    <version>6.2.3</version>
</dependency>
2. Gradle dependencies

in yourAdd the following dependencies to the file:

groovyCopy code

 implementation ':oshi-core:6.2.3'

3. Code implementation

Below is a complete Java program example that shows how to use the Oshi library to obtain and display traffic data for a network interface.

 networkIFs = ();
 
         //Print initial network interface information
         printNetworkInterfaces(networkIFs);
 
         // Sleep for a period of time to calculate traffic changes
         (5);
 
         // Get network interface information again to calculate traffic
         List networkIFsAfterSleep = ();
 
         //Print traffic changes
         printNetworkTraffic(networkIFs, networkIFsAfterSleep);
     }
 
     private static void printNetworkInterfaces(List networkIFs) {
         ("Network Interfaces:");
         for (NetworkIF networkIF : networkIFs) {
             ("Name: " + ());
             ("Description: " + ());
             ("MAC Address: " + ());
             ("MTU: " + ());
             ("Up: " + ());
             ("------------------------");
         }
         ();
     }
 
     private static void printNetworkTraffic(List networkIFsBefore, List networkIFsAfter) {
         ("Network Traffic (bytes) over 5 seconds:");
         for (NetworkIF networkIFBefore : networkIFsBefore) {
             String ifName = ();
             for (NetworkIF networkIFAfter : networkIFsAfter) {
                 if ((())) {
                     long rxBytesBefore = ();
                     long txBytesBefore = ();
                     long rxBytesAfter = ();
                     long txBytesAfter = ();
 
                     long rxRate = rxBytesAfter - rxBytesBefore;
                     long txRate = txBytesAfter - txBytesBefore;
 
                     ("Interface: " + ifName);
                     ("Received Rate: " + rxRate + " bytes/sec");
                     ("Transmitted Rate: " + txRate + " bytes/sec");
                     ("------------------------");
                 }
             }
         }
     }
 }

4. Detailed explanation of code

  1. Get system information

    SystemInfo systemInfo = new SystemInfo();
    HardwareAbstractionLayer hal = ();
    

    SystemInfoClass is used to obtain information about the entire system,HardwareAbstractionLayerClasses provide interfaces for accessing hardware resources.

  2. Get list of network interfaces

     networkIFs = ();

    getNetworkIFsMethod returns a list containing all network interfaces.

  3. Print initial network interface information

    java copy code
    
     printNetworkInterfaces(networkIFs);

    printNetworkInterfacesMethod iterates through the list of network interfaces and prints each interface's name, description, MAC address, MTU, and status.

  4. Calculate traffic changes

    (5);
    List<NetworkIF> networkIFsAfterSleep = ();
    

    The program sleeps for 5 seconds and then obtains network interface information again to calculate traffic changes.

  5. Print flow changes

    java copy code
    
     printNetworkTraffic(networkIFs, networkIFsAfterSleep);

    printNetworkTrafficMethods calculate the receive and send rates for each network interface and print the results.

5. Operation results

After running the program, you will see output similar to the following:

Network Interfaces:
 Name: eth0
 Description: Ethernet interface
 MAC Address: 00:1a:2b:3c:4d:5e
 MTU: 1500
 Up: true
 --------------------------
 ...
 (Other network interface information)
 ...
 
 Network Traffic (bytes) over 5 seconds:
 Interface: eth0
 Received Rate: 1234567 bytes/sec
 Transmitted Rate: 7654321 bytes/sec
 --------------------------
 ...
 (Traffic information for other network interfaces)
 ...

6. Summary

This article describes how to use Java and the Oshi library to implement a simple network performance monitoring tool. With this program we can get the name, description, MAC address, MTU and status of a network interface and calculate the receive and send rates within a specified time interval. This is a very useful tool for developers and system administrators to help monitor and optimize network performance.

The Oshi library provides a cross-platform solution that makes obtaining system hardware resource information in Java easier and more efficient. By extending the program, you can also add more monitoring functions, such as CPU usage, memory usage, disk I/O, etc., to build a complete system performance monitoring tool.