您的位置:首页 > 编程语言 > Java开发

java中的并发:进程和线程

2015-07-29 00:00 639 查看
摘要: ----好好使用进程,可以解决不少问题。

1.简介
1)进程:同一个系统中执行的一个子程序,包含三部分:虚拟CPU,代码,数据.
2)线程:同一个进程中执行的子程序流.
3)进程让操作系统的并发性成为可能,而线程让进程的内部并发成为可能.一个进程虽然包括多个线程,但是这些线程是共同享有进程占有的资源和地址空间的.进程是操作系统进行资源分配的基本单位,而线程是操作系统进行调度的基本单位.

2.创建一个进程
2.1首先了解三个类 1)Process(抽象类)
进程类,提供了执行从进程输入、执行输出到进程、等待进程完成、检查进程的退出状态以及销毁进程的方法,可以通过ProcessBuilder.start()和 Runtime.exec()方法创建一个本机进程,并返回Process子类的一个实例.
2)ProcessBuilder(final类)
此类用于创建操作系统进程,常用方法:
ProcessBuilder(String... command) 利用指定的操作系统程序和参数构造一个进程生成器
directory(File directory) 设置此进程生成器的工作目录
start() 使用此进程生成器的属性启动一个新进程,返回一个Process对象.
3)RunTime
每个java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接,可以通过getRuntime方法获取当前运行时,应用程序不能创建自己的Runtime类实例.通过RunTime.exec()创建的进程,最终还是通过ProcessBuilder类的start方法来创建的.
2.2通过ProcessBuilder创建进程

Java代码



public class ProcessDemo {

// 启动进程打开cmd,并获取IP地址

public static void main(String[] args) throws IOException {

// 设置执行命令

List<String> command = new ArrayList<>();

Collections.addAll(command, "cmd", "/c", "ipconfig -all");

ProcessBuilder pb = new ProcessBuilder(command);

// 设置命令路径

pb.directory(new File("c:\\windows\\system32"));

// 开启进程

Process process = pb.start();

Scanner scanner = new Scanner(process.getInputStream());

while (scanner.hasNext()) {

System.out.println(scanner.nextLine());

}

scanner.close();

}

}

2.3通过Runtime创建进程

Java代码



public class ProcessDemo {

public static void main(String[] args) throws IOException {

Runtime run = Runtime.getRuntime();

String[] cmd = { "cmd", "/c", "ipconfig -all" };

Process process = run.exec(cmd, null, new File("c:\\windows\\system32"));

Scanner scanner = new Scanner(process.getInputStream());

while (scanner.hasNextLine()) {

System.out.println(scanner.nextLine());

}

scanner.close();

}

}

3.创建一个线程
3.1首先了解两个类
1)Thread线程类,通过继承Thread类并重写run()方法,调用start()方法启动一个线程.
2)Runnable接口,通过实现此接口并实现run()方法,将自身作为构造Thread的参数,然后通过Thread的start方法来启动一个线程.
3)事实上,Thread类是实现了Runnable接口的.直接继承Thread类的话,可能比实现Runnable接口看起来更加简洁,但是由于java只允许单继承,一般建议选择实现Runnable接口.
3.2通过继承Thread创建线程

Java代码



public class ThreadDemo {

public static void main(String[] args) {

new Thread() {

public void run() {

int i = 0;

while (i++ < 100) {

System.out.println("thread...i:" + i);

}

}

}.start();

new Thread() {

public void run() {

int j = 0;

while (j++ < 100) {

System.out.println("thread...j:" + j);

}

}

}.start();

}

}

运行结果为两个线程交替打印直到结束.
3.3通过实现Runnable创建线程

Java代码



public class ThreadDemo {

public static void main(String[] args) {

new Thread(new Runnable() {

public void run() {

int i = 0;

while (i++ < 100) {

System.out.println("thread...i:" + i);

}

}

}).start();

new Thread(new Runnable() {

public void run() {

int j = 0;

while (j++ < 100) {

System.out.println("thread...j:" + j);

}

}

}).start();

}

}

运行结果为两个线程交替打印直到结束.
3.4一个小问题

Java代码



public class ThreadDemo {

public static void main(String[] args) {

new Thread(new Runnable() {

public void run() {

int i = 0;

while (i++ < 100) {

System.out.println("runnable...:" + i);

}

}

}){

public void run() {

int i = 0;

while (i++ < 100) {

System.out.println("thread...:" + i);

}

}

}.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: