- Java 9 Programming Blueprints
- Jason Lee
- 388字
- 2021-07-02 18:56:29
Managing Processes in Java
With a very quick tour through some of the big new features of Java 9, as well as those from a couple of previous releases, let's turn our attention to applying some of these new APIs in a practical manner. We'll start with a simple process manager.
While having your application or utility handle all of your user's concerns internally is usually ideal, occasionally you need to run (or shell out to) an external program for a variety of reasons. From the very first days of Java, this was supported by the JDK via the Runtime class via a variety of APIs. Here is the simplest example:
Process p = Runtime.getRuntime().exec("/path/to/program");
Once the process has been created, you can track its execution via the Process class, which has methods such as getInputStream(), getOutputStream(), and getErrorStream(). We have also had rudimentary control over the process via destroy() and waitFor(). Java 8 moved things forward by adding destroyForcibly() and waitFor(long, TimeUnit). Starting with Java 9, these capabilities will be expanded. Quoting from the Java Enhancement Proposal (JEP), we see the following reasons for this new functionality:
Many enterprise applications and containers involve several Java virtual machines and processes and have long-standing needs that include the following:
- The ability to get the pid (or equivalent) of the current Java virtual machine and the pid of processes created with the existing API.
- The ability to enumerate processes on the system. Information on each process may include its pid, name, state, and perhaps resource usage.
- The ability to deal with process trees, in particular, some means to destroy a process tree.
- The ability to deal with hundreds of sub-processes, perhaps multiplexing the output or error streams to avoid creating a thread per sub-process.
In this chapter, we'll build a simple process manager application, akin to Windows Task Manager or *nix's top. There is, of course, little need for a process manager written in Java, but this will be an excellent avenue for us to explore these new process handling APIs. Additionally, we'll spend some time with other language features and APIs, namely, JavaFX and Optional.
The following topics are covered in this chapter:
- Creating the project
- Bootstrapping the application
- Defining the user interface
- Initializing the user interface
- Adding menus
- Updating the process list
With that said, let's get started.
- PySpark Cookbook
- Webpack實(shí)戰(zhàn):入門、進(jìn)階與調(diào)優(yōu)
- Microsoft Dynamics AX 2012 R3 Financial Management
- Instant PHP Web Scraping
- Visual Basic 程序設(shè)計(jì)實(shí)踐教程
- Visual C++程序設(shè)計(jì)全程指南
- Moodle 3.x Developer's Guide
- HTML5程序開發(fā)范例寶典
- Unity虛擬現(xiàn)實(shí)開發(fā)圣典
- 前端程序員面試筆試真題與解析
- TensorFlow 2.0深度學(xué)習(xí)應(yīng)用實(shí)踐
- Neo4j High Performance
- R Data Visualization Cookbook
- PHP程序開發(fā)參考手冊(cè)
- Java程序設(shè)計(jì)教程(慕課版)