Java thread creation has two forms, one is to inherit the thread and one is to implement the Runnable interface.
Private class Newthread extends Thread { @Override public void Run () { //do Something } } Private class Newrunnable implements Runnable { @Override public void Run () { //do Something } }
used in code:
New Newthread (). Start (); New Thread (New Newrunnable ()). Start ();
in fact, the two are essentially the same, Thread, runnable source code as follows:
Class Thread implements Runnable public interface Runnable { /** * When an object implementing interface <c Ode>runnable</code> is used * To create a thread, starting the thread causes the object ' s * <code>r Un </code> method to is called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> are that it could take any action wha Tsoever. * * @see java.lang.thread#run () */public abstract void run ();
you can see that thread actually implements the Runnable interface in essence;
and how the process in Java is created, there are two ways to do it: one is to use Processbuilder.start to create, one is to use the runntime.exec implementation;
first, create with Processbuilder.start1. Use:
Processbuilder PB = new Processbuilder ("cmd", "/C", "Ipconfig/all"); Process process = NULL; try { process = Pb.start (); } catch (IOException e) { e.printstacktrace (); } System.out.println (Process.isalive ()); Scanner Scanner = new Scanner (Process.getinputstream ()); while (Scanner.hasnextline ()) { System.out.println (Scanner.nextline ()); } Scanner.close ();
2, first look at the process of the main source code:
Public abstract class Process { abstract public outputstream getoutputstream (); Gets the output stream of the process abstract public inputstream getinputstream (); Gets the input stream of the process abstract public inputstream geterrorstream (); Gets the error stream for the process abstract public int waitFor () throws interruptedexception; Let the process wait for the abstract public int exitvalue (); Gets the exit flag for the process abstract public void Destroy (); Destroy process public processes destroyforcibly () { destroy (); return this; } public Boolean isAlive () { try { exitvalue (); return false; } catch (Illegalthreadstateexception e) { return true;}} }
3, the Processbuilder constructor:
Public final class processbuilder{ private list<string> command; Private File directory; Private map<string,string> environment; Private Boolean redirecterrorstream; Private redirect[] redirects; Public Processbuilder (list<string > command) { if (command = = null) throw new NullPointerException (); This.command = command; } Public Processbuilder (String ... command) { This.command = new ArrayList <> (command.length); for (String Arg:command) this.command. Add (arg);} }
support two kinds of constructors, one is List form, one is indefinite long parameter form;
4. Processbuilder starts the process with the Start method:
Public Process Start () throws IOException {//must convert to array first--a malicious user-supplied//list Migh T try to circumvent the security check. string[] Cmdarray = Command.toarray (new string[command. Size ()]); Cmdarray = Cmdarray.clone (); for (String Arg:cmdarray) if (arg = = null) throw new NullPointerException (); Throws indexoutofboundsexception if command is empty String prog = cmdarray[0]; SecurityManager security = System.getsecuritymanager (); if (Security! = null) security.checkexec (prog); String dir = Directory = = null? Null:directory.toString (); for (int i = 1; i < cmdarray.length; i++) {if (Cmdarray[i].indexof (' \u0000 ') >= 0) {throw new IOException ("Invalid null character in command"); }} try {return Processimpl.start (Cmdarray, Environment, Dir, redirects, Redirecterrorstream); } catch (IOException | IllegalArgumentException e) {...}}
The front is a series of treatments for commands, which ultimately pass
Processimpl.start (Cmdarray,environment,dir,redirects,redirecterrorstream);
to start the process.
5. Take a look at Processimpl (and Android Context,contextimpl)
Final class Processimpl extends process { static process start (String cmdarray[], java.util.map<string, string> environment, String dir, processbuilder.redirect[] redirects, boolean redirecterrorstream) throws ioexception{ String envblock = processenvironment.toenvironmentblock (environment); try { ... return new Processimpl (Cmdarray, Envblock, dir, Stdhandles, Redirecterrorstream); } Finally {...}}}
you can see that the actual type of the process that is ultimately returned is the Processimpl object; Processimpl is the concrete implementation class of the abstract class process.
second, use runtime.exec to create1. Use
String cmd = "cmd" + "/C" + "Ipconfig/all"; Process process = Runtime.getruntime (). exec (cmd); Scanner Scanner = new Scanner (Process.getinputstream ()); while (Scanner.hasnextline ()) { System.out.println (Scanner.nextline ()); } Scanner.close ();
2. Runtime class:
public class Runtime { private static runtime currentruntime = new Runtime (); public static Runtime GetRuntime () { return currentruntime; } Private Runtime () {}}
It is obvious that the singleton pattern.
3, Runtime#exec:
Public Process exec (String command) throws IOException { return exec (command, NULL, NULL); } Public Process exec (String command, string[] envp, File dir) throws IOException { if (command.length () = = 0) t Hrow New IllegalArgumentException ("Empty command"); StringTokenizer st = new StringTokenizer (command); string[] Cmdarray = new String[st.counttokens ()]; for (int i = 0; St.hasmoretokens (); i++) cmdarray[i] = St.nexttoken (); return exec (Cmdarray, envp, dir); } Public Process exec (string[] cmdarray, string[] envp, File dir) throws IOException { return new Processbuilder ( Cmdarray) . Environment (ENVP). directory (dir). start ();
You can see that the end is still called Processbuilder.start to create, the two principles are essentially the same, but because of the different constructors, the Runtime.exec method only supports the method of assembling all commands into a single string.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Creation of the Java process