Process and Thread (i) =====> process

Source: Internet
Author: User

Java provides two ways to start a process or other program:
(1) using the runtime's Exec () method
(2) using the Processbuilder start () method

2.1.1 Processbuilder
The Processbuilder class is a new class that is newly added to J2SE 1.5 in Java.lang, which is used to create an operating system process that provides a way to start and manage processes (that is, applications). Before J2SE 1.5, control management of processes was implemented by the process class.
Each Processbuilder instance manages a set of process properties. The start () method uses these properties to create a new Process instance. The start () method can be called repeatedly from the same instance to create a new child process with the same or related properties.
Each process builder manages these process properties:
A command is a list of strings that represents the external program file to invoke and its parameters, if any. In this case, a list of strings that represent valid operating system commands is dependent on the system. For example, each population variable is usually an element in this list, but there are some operating systems that want the program to tag its own command-line string-in which case the Java implementation might need the command to contain exactly these two elements.
An environment is a system-dependent mapping from a variable to a value. The initial value is a copy of the current process environment (see SYSTEM.GETENV ()).
Working directory. The default value is the current working directory of the current process and is typically named according to the system property User.dir.
The Redirecterrorstream property. Initially, this property is false, meaning that the child process's standard output and error output are sent to two separate streams that can be accessed through the Process.getinputstream () and Process.geterrorstream () methods. If the value is set to True, standard errors are merged with standard output. This makes it easier to correlate error messages and corresponding outputs. In this case, the merged data can be read from the stream returned by Process.getinputstream (), and the stream read returned from Process.geterrorstream () will reach the end of the file directly.
Modifying the properties of the process builder will affect subsequent processes started by the object's start () method, but will never affect processes that were previously started or Java itself. Most error checking is performed by the start () method. You can modify the state of an object, but the start () will fail. For example, setting the command property to an empty list will not throw an exception unless start () is included.
Note that this class is not synchronous. If more than one thread accesses a processbuilder at the same time, and at least one of the threads modifies one of the properties from the structure, it must remain externally synchronized.

Example: Two processes open two text boxes

Package Xiancheng;public class Prossdemo {/* create process */public static void main (string[] args) throws exception{// The Exec method of the runtime class runtime  = Runtime.getruntime (); Runtime.exec ("notepad");//procssbuild class Processbuilder Processbuilder = new Processbuilder ("Notepad"); Processbuilder.start (); }}

  

Process and Thread (i) =====> process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.