Java creation Process __java

Source: Internet
Author: User
Java creation Process


1 concept of the process 1
1.1 Concept of the process 1
1.2 Characteristics of the process 1
1.3 Process and Thread differences 1
2 The creation of the process 1
2.1 Java Process creation 1
2.1.1 Processbuilder 2
2.1.2 Runtime 3
2.1.3 Process 4
2.2 Instance 5
2.2.1 Create Child Process 5
2.2.2 Process blocking Problem 7
2.2.3 executes Java program in Java 11



1 concept of the process
1.1 Concept of the process
A process is the basis of an operating system structure, an executing program, an instance of a program that is running on a computer, an entity that can be assigned to a processor and executed by a processor, a single sequential display, a current state and a set of active cells described by a group of related system resources.
First, the process is an entity. Each process has its own address space, typically including the text area (region), the data region (region), and the stack region. The text area stores the code that the processor executes, the data region stores variables and the dynamically allocated memory used during process execution, and the stack area stores the instructions and local variables that are invoked by the active procedure.
Second, the process is a "program in progress". A program is a lifeless entity, and only the processor gives the program life, it becomes an active entity, which we call the process.
1.2 Characteristics of the process
Dynamic: The essence of the process is the process of a program in a multi-channel program system, the process is dynamic generation, dynamic extinction.
Concurrency: Any process can be executed concurrently with other processes
Independence: The process is a basic unit that can operate independently, and it is also an independent unit for allocating resources and dispatching.
Asynchrony: Due to the mutual constraints between processes, the process has the discontinuity of execution, that is, the process in accordance with their independent, unpredictable speed forward
Structural characteristics: The process consists of three parts, the program, the data and the process Control block.
Several different processes can contain the same program: A program can have different results in different sets of data, but the program cannot change during execution.
1.3 Process and Thread differences
The main difference between processes and threads is that they are different ways of managing operating system resources. Processes have separate address spaces, and when a process crashes, it does not affect other processes in protected mode, and threads are just different execution paths in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, one thread dead is equal to the entire process dead, so the process of multiple processes than multithreaded program is robust, but in the process of switching, the resource consumption is greater, the efficiency is some worse. However, for concurrent operations that require simultaneous and share certain variables, only threads can be used, and processes cannot be used.

2 Creation of processes
2.1 Java Process creation
Java provides two ways to start a process or other program:
(1) the Exec () method using runtime
(2) using the Processbuilder start () method
2.1.1 Processbuilder
The Processbuilder class is a new class added to J2SE 1.5 in Java.lang that creates an operating system process that provides a way to start and manage a process (that is, an application). Prior to J2SE 1.5, the process class was implemented to control management of processes.
Each Processbuilder instance manages a process attribute set. 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 represent the external program files and their parameters (if any) to invoke. Here, a list of strings representing valid operating system commands is dependent on the system. For example, each population variable is typically an element in this list, but there are some operating systems that want the program to tag the command-line string itself--a system in which the Java implementation may require 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, usually named according to the system Properties User.dir.
Redirecterrorstream property. Initially, this property is false, meaning that the standard output and error output of the child process is sent to two separate streams that can be accessed through the Process.getinputstream () and Process.geterrorstream () methods. If the value is set to True, the standard error is merged with the standard output. This makes it easier to associate error messages with corresponding output. In this case, the merged data can be read from the stream returned by Process.getinputstream (), and the stream read from Process.geterrorstream () will reach the end of the file directly.
Modifying the properties of the process builder affects processes that are subsequently started by the start () method of the object, but 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 then start () will fail. For example, setting a command property to an empty list will not throw an exception unless you include start ().
Note that this class is not synchronized. If more than one thread accesses a processbuilder at the same time, and at least one of them is structurally modifying one of the properties, it must maintain an external synchronization.

Java code    Construction method Summary    processbuilder (list<string> command)                 constructs a process builder using the specified operating system programs and parameters.      Processbuilder (string... command)                 constructs a process builder using the specified operating system programs and parameters.         Method Summary     list<string> command ()                returns the operating system program and parameters for this process builder.     processbuilder command (list<string> command)                 set the operating system program and parameters for this process builder.     processbuilder command (string... command)                 set the operating system program and parameters for this process builder.     file directory ()                returns the working directory for this process builder.     processbuilder directory (file directory)                 set the working directory for this process builder.     map<string,string> environment ()                 returns a string-mapped view of this process builder environment.     boolean redirecterrorstream ()                 notifies the process builder whether to merge standard errors and standard output.     processbuilder redirecterrorstream (Boolean redirecterrorstream)                set the  redirectErrorStream  property of this process builder.     process start ()                 Start a new process using the properties of this process builder.   

2.1.2 Runtime
Each Java application has a Runtime class instance that enables the application to connect to the environment in which it is running. You can get the current runtime by using the GetRuntime method.
The application cannot create its own instance of the Runtime class. However, you can obtain a reference to the current runtime Run-time object by using the GetRuntime method. Once you get a reference to the current runtime object, you can call the runtime object's method to control the state and behavior of the Java Virtual machine.
Java Code    void addshutdownhook (thread hook)                 register a new virtual machine to close the hook.     int availableprocessors ()                 returns the number of available processors to  Java  virtual machines.     process exec (string command)                 executes the specified string command in a separate process.     process exec (string[] cmdarray)                 executes the specified commands and variables in a separate process.     process exec (STRING[]&NBSP;CMDARRAY,&NBSP;STRING[]&NBSP;ENVP)                 executes the specified command and variable in a separate process in the specified environment.     process exec (String[] cmdarray, string[] envp, file dir)                 in designated environment and industryExecutes the specified command and variable in a separate process for the directory.     process exec (STRING&NBSP;COMMAND,&NBSP;STRING[]&NBSP;ENVP)                 executes the specified string command in a separate process in the specified environment.     process exec (String command, string[] envp, file dir)                 executes the specified string command in a separate process with the specified environment and working directory.     void exit (int status)                 terminates the currently running  Java  virtual machine by starting the shutdown sequence of the virtual machine.     long freememory ()                 returns the amount of idle memory in the  Java  virtual machine.    &NBSP;VOID&NBSP;GC ()                Run the garbage collector.     inputstream getlocalizedinputstream (inputstream in)          
Related Article

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.