????????????????ó?????????
????1??thread1??thread2?????ID?????thread2???????ID???????????run???????ò????????μ?????????????????????????run????????????????????????κ?????
????2?????thread1??start??????????thread2??run????????????????????????thread2??run???????????????????????????????????????????????????С?
????2.???Runnable???
??????Java?д???????????Thread?????????????????Runnable????????????????????Runnable????????д??run??????
??????????????????
public class Test {
public static void main(String[] args) {
System.out.println("?????ID??"+Thread.currentThread().getId());
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
class MyRunnable implements Runnable{
public MyRunnable() {
}
@Override
public void run() {
System.out.println("?????ID??"+Thread.currentThread().getId());
}
}
????Runnable???????????“????”????????壬??????Runnable?????????????????????????????????Thread???С??????????????Runnable???Thread??????????????Thread??start??????????????????????и??????????????Runnable??run??????????????????????????????????????????κ?????
????????????Thread??????????????Thread?????????Runnable?????
??????Java?У???2??????????????????????????????????????????????????????????????Thread??????????????Runnable??????????????????????Java????????У??????????????????????????????????????Runnable????
??????.Java????δ???????
??????Java?У?????????????????????????????漰??5?????????
????????????????Runtime.exec()???????????????????????????????ProcessBuilder??start????????????????????????????2????????????????
???????????????Process??Process?????????????????????????м?????????????????????????Process????????????
????λ??java.lang.Process·???£?
????public abstract class Process
????{
????abstract public OutputStream getOutputStream(); //?????????????
????abstract public InputStream getInputStream(); //??????????????
????abstract public InputStream getErrorStream(); //?????????????
????abstract public int waitFor() throws InterruptedException; //?y?????
????abstract public int exitValue(); //??????????????
????abstract public void destroy(); //??????
????}
????1?????ProcessBuilder????????
????ProcessBuilder?????final??????????????????
public final class ProcessBuilder
{
private List<String> command;
private File directory;
private Map<String??String> environment;
private boolean redirectErrorStream;
public ProcessBuilder(List<String> command) {
if (command == null)
throw new NullPointerException();
this.command = command;
}
public ProcessBuilder(String... command) {
this.command = new ArrayList<String>(command.length);
for (String arg : command)
this.command.add(arg);
}
....
}
???????????д????????????????????????????????????????????????????List???д?????????????????????????????????????????
???????????????????????????????ProcessBuilder??start????????????????????????????start?????о?????????Щ???顣??????start?????????????????
public Process start() throws IOException {
// Must convert to array first -- a malicious user-supplied
// list might try to circumvent the security check.
String[] cmdarray = command.toArray(new String[command.size()]);
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();
try {
return ProcessImpl.start(cmdarray??
environment??
dir??
redirectErrorStream);
} catch (IOException e) {
// It's much easier for us to create a high-quality error
// message than the low-level C code which found the problem.
throw new IOException(
"Cannot run program "" + prog + """
+ (dir == null ? "" : " (in directory "" + dir + "")")
+ ": " + e.getMessage()??
e);
}
}
?????÷??????????Process?????÷???????沿????????????????????????????????????Щ?????趨?????????try????????????
????return ProcessImpl.start(cmdarray??
????environment??
????dir??
????redirectErrorStream);
????????????????????????????????????ProcessImpl???start????????????????start????????????????????ProcessImpl??????????????????λ??java.lang.ProcessImpl·???£?????????????????
????ProcessImpl??????final?????????Process??
????final class ProcessImpl extends Process {
????// System-dependent portion of ProcessBuilder.start()
????static Process start(String cmdarray[]??
????java.util.Map<String??String> environment??
????String dir??
????boolean redirectErrorStream)
????throws IOException
????{
????String envblock = ProcessEnvironment.toEnvironmentBlock(environment);
????return new ProcessImpl(cmdarray?? envblock?? dir?? redirectErrorStream);
????}
????....
????}
????????ProcessImpl???start????????????????????start???????????????????????ProcessImpl??????
????return new ProcessImpl(cmdarray?? envblock?? dir?? redirectErrorStream);
????????ProcessImpl?ж?Process???е???????????????????????
???????????????ProcessBuilder??start???????????????ProcessImpl????
???????濴?????????ProcessBuilder????????????????????????ProcessBuilder?????????????cmd???????ip??????????????????д??
????public class Test {
????public static void main(String[] args) throws IOException {
????ProcessBuilder pb = new ProcessBuilder("cmd"??"/c"??"ipconfig/all");
????Process process = pb.start();
????Scanner scanner = new Scanner(process.getInputStream());
????while(scanner.hasNextLine()){
????System.out.println(scanner.nextLine());
????}
????scanner.close();
????}
????}
????????????????????????????????ProcessBuilder??????????????????????????е?????????????????????????????????????????????????List?д??????
????????????????????÷?????????????????????ProcessBuilder??environment??????directory(File directory)???y?????????????????????????????????????????API?????
????2?????Runtime??exec??????????????
??????????????????Runtime???exec?????????????Runtime????????壬??????????????????????????????????
?????????κν??????????????????????????У???????Runtime?в?????????????????????????????????
????public class Runtime {
????private static Runtime currentRuntime = new Runtime();
????/**
????* Returns the runtime object associated with the current Java application.
????* Most of the methods of class <code>Runtime</code> are instance
????* methods and must be invoked with respect to the current runtime object.
????*
????* @return the <code>Runtime</code> object associated with the current
????* Java application.
????*/
????public static Runtime getRuntime() {
????return currentRuntime;
????}
????/** Don't let anyone else instantiate this class */
????private Runtime() {}
????...
????}
??????????????????????Runtime??????????private?????????????getRuntime????Runtime?????????????????????exec???? ??????Runtime???ж??exec??????????????????????е???????汾??exec??????
????public Process exec(String[] cmdarray?? String[] envp?? File dir)
????throws IOException {
????return new ProcessBuilder(cmdarray)
????.environment(envp)
????.directory(dir)
????.start();
????}
????????????????????Runtime???exec????????????????????ProcessBuilder???start?????????????
???????濴????????????????Runtime??exec??δ?????????????????????????cmd?????ip????????
????public class Test {
????public static void main(String[] args) throws IOException {
????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();
????}
????}
????????????exec????????????????????ProcessBuilder????????????????????????????????????????????????
??????????Java????δ?????????????????????????????????????????ο????????