Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java 11 Cookbook

You're reading from   Java 11 Cookbook A definitive guide to learning the key concepts of modern application development

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher
ISBN-13 9781789132359
Length 802 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Mohamed Sanaulla Mohamed Sanaulla
Author Profile Icon Mohamed Sanaulla
Mohamed Sanaulla
Nick Samoylov Nick Samoylov
Author Profile Icon Nick Samoylov
Nick Samoylov
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Installation and a Sneak Peek into Java 11 FREE CHAPTER 2. Fast Track to OOP - Classes and Interfaces 3. Modular Programming 4. Going Functional 5. Streams and Pipelines 6. Database Programming 7. Concurrent and Multithreaded Programming 8. Better Management of the OS Process 9. RESTful Web Services Using Spring Boot 10. Networking 11. Memory Management and Debugging 12. The Read-Evaluate-Print Loop (REPL) Using JShell 13. Working with New Date and Time APIs 14. Testing 15. The New Way of Coding with Java 10 and Java 11 16. GUI Programming Using JavaFX 17. Other Books You May Enjoy

Managing the spawned process


There are a few methods, such as destroy(), destroyForcibly() (added in Java 8), isAlive() (added in Java 8), and supportsNormalTermination() (added in Java 9), which can be used to control the process spawned. These methods are available on the Process object as well as on the ProcessHandle object. Here, controlling would be just to check whether the process is alive, and if it is, then destroy the process. 

In this recipe, we will spawn a long-running process and do the following:

  • Check for its liveliness
  • Check whether it can be stopped normally; that is, depending on the platform, the process has to be stopped by just using destroy or by using force destroy
  • Stop the process

How to do it...

  1. Spawn a new process from the Java code, which runs the sleep command for, say, one minute, or 60 seconds:
        ProcessBuilder pBuilder = new ProcessBuilder("sleep", "60");
        Process p = pBuilder.inheritIO().start();
  1. Wait for, say, 10 seconds:
p.waitFor(10, TimeUnit.SECONDS...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image