首页 技术 正文
技术 2022年11月14日
0 收藏 370 点赞 5,087 浏览 3516 个字

Java并发编程:进程的创建

<!–*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: ‘sh’; }
pre.src-bash:before { content: ‘sh’; }
pre.src-emacs-lisp:before { content: ‘Emacs Lisp’; }
pre.src-R:before { content: ‘R’; }
pre.src-perl:before { content: ‘Perl’; }
pre.src-java:before { content: ‘Java’; }
pre.src-sql:before { content: ‘SQL’; }

table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>–>*/–>

code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}

Java并发编程:进程的创建

Table of Contents

上次聊了进程和线程的相关概念,那么,怎么在Java中创建一个进程呢?
当我们创建一个应用程序,然后执行这个程序,系统肯定会创建一个进程。我们这里讲的不是这种,而是在已经运行的程序内部再去调用运行其他程序来创建进程。
Java中调用其他程序有两种方式:Runtime.getRuntime().exec() 和 ProcessBuilder.start()。

1 Runtime.getRuntime().exec()

先看一个例子:

public class ProcessDemo {
public static void main(String[] args) {
Runtime proc = Runtime.getRuntime();
try {
proc.exec("open /Applications/Launchpad.app");
// proc.exec("open notepad.exe");
} catch (Exception e) {
e.printStackTrace();
}
}
}

在MacOsX中打开一个Launchpad.app程序,或者在Windows中打开一个记事本,就是这么简单。那么,我们观察一下,它是不是和我们有些语言中的exec一样,是阻塞运行的呢?

import javax.swing.JOptionPane;public class ProcessDemo {
public static void main(String[] args) {
Runtime proc = Runtime.getRuntime();
try {
proc.exec("open /Applications/Launchpad.app");
// proc.exec("open notepad.exe");
JOptionPane.showMessageDialog(null,
"Finished!",
"TITLE",
JOptionPane.WARNING_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}
}

可以看出,并不是阻塞运行的。

2 ProcessBuilder.start()

我们看一下用ProcessBuilder怎么实现?

public class ProcessDemo {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("open", "/Applications/Launchpad.app");
try {
pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

ProcessBuilder 的command需要分开来写,第一个是command名称,后面是它的参数。我们发现和Rumtime的exec()其实差不多。如果,我们去看源码的话,发现Runtime的exec也是用的ProcessBuilder来实现的。

Date: 2017-07-03 22:39

Author: WEN YANG

Created: 2017-07-04 Tue 21:36

Emacs 25.2.1 (Org mode 8.2.10)

Validate

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294