The implementation of processes and threads in Java

Source: Internet
Author: User

Overview

Process and thread, in essence, is the operating system of the dispatch unit, can be regarded as an operating system "resources." Java, as a platform-independent programming language, is bound to further encapsulate the functions provided by the underlying (operating system), with platform-independent programming interfaces for programmers to use, and processes and threads as part of the core concept of the operating system. In the Java language, the encapsulation of processes and threads provides some classes related to process and thread, respectively. This article begins by simply describing how to use these classes to create processes and threads. Then it focuses on how these classes correspond to the operating system local process line threads relative, gives the Java virtual machine A summary of these encapsulated classes, and because Java encapsulation also hides some of the underlying concepts and operability, this article Java process threads and local process threads make some simple comparisons that list some of the limitations of using Java processes, threads, and issues that need attention.

How Java processes are built

In JDK, a class that is directly related to a process is Java.lang.Process, and it is an abstract class. A Processimpl class that implements the abstract class is also provided in the JDK, and if the user creates a process, it is certainly accompanied by a new Processimpl instance. Also closely related to process creation is Processbuilder, which began appearing in JDK1.5, and provides a convenient way to configure the environment for creating new processes, directories, and merging error and output streams, relative to the process class.

Both the Java.lang.Runtime.exec method and the Java.lang.ProcessBuilder.start method can create a local process, and then return a Java.lang.Process reference that represents the process.

Runtime.exec method to establish a local process

In JDK1.5, the method can accept 6 different forms of parameter passing in.

Process exec (String command)
Process exec (String [] cmdarray)
Process exec (string [] Cmdarrag, String [] envp)
Process exec (string [] Cmdarrag, String [] envp, File dir)
Process exec (string cmd, string [] envp)
Process exec (String command, string [] envp, File dir)

Their main differences are in the form of incoming command arguments, the provided environment variables, and the definition execution directory.

Processbuilder.start method to create a local process

If you want to use the current directory and environment variables in a newly created process, you do not need any configuration, pass the command line and parameters directly into Processbuilder, and then call the Start method to obtain a reference to the process.

Process p = new Processbuilder ("command", "Param"). Start ();

You can also configure environment variables and working directories first, and then create processes.

Processbuilder PB = new Processbuilder ("command", "param1", "param2");
map<string, string> env = Pb.environment ();
Env.put ("VAR", "Value");
Pb.directory ("Dir");
Process p = Pb.start ();

The Processbuilder properties that can be preconfigured are the greatest advantages of creating a process through Processbuilder. And you can change the properties of the PB variables in the code as needed in later use. If subsequent code modifies its properties, it affects the process created with the Start method and has no effect on the process instance created before the modification.

The JVM's implementation of the process

In the JDK code, only the Processimpl class is provided to implement the Process abstraction class. This refers to native's create, close, waitfor, Destory, and Exitvalue methods. In Java, the native method is a local method that relies on the operating system platform, and its implementation is implemented in a similar low-level language as C + +. We can find the corresponding local method in the source code of the JVM and analyze it. The JVM's implementation of the process is relatively simple, taking the JVM under Windows as an example. In the JVM, pass the parameters passed in Java to the operating system to the corresponding method to implement the function. As shown in table 1

native method names called in the JDK the Windows API corresponding to the call
Create Createprocess,createpipe
Close CloseHandle
waitfor WaitForMultipleObjects
Destroy TerminateProcess
Exitvalue GetExitCodeProcess

Take the Create method for example, let's look at how it connects to the system API.

In the Processimple class, there is a native create method with the following parameters:

Private native long Create (string cmdstr, String Envblock,
String dir, Boolean redirecterrorstream, FileDescriptor in_fd,
FileDescriptor out_fd, FileDescriptor err_fd) throws IOException;

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.