Java并发编程:如何建立线程?

在前面一篇文章中已经讲述了在进程和线程的由来,今天就来说一下在Java中如何建立线程,让线程去执行一个子任务。下面先讲述一下Java中的应用程序和进程相关的概念知识,而后再阐述如何建立线程以及如何建立进程。下面是本文的目录大纲:html

一.Java中关于应用程序和进程相关的概念java

二.Java中如何建立线程程序员

三.Java中如何建立进程web

一.Java中关于应用程序和进程相关的概念编程

在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),通常来讲名字默认为java.exe或者javaw.exe(windows下能够经过任务管理器查看)。Java采用的是单线程编程模型,即在咱们本身的程序中若是没有主动建立线程的话,只会建立一个线程,一般称为主线程。可是要注意,虽然只有一个线程来执行任务,不表明JVM中只有一个线程,JVM实例在建立的时候,同时会建立不少其余的线程(好比垃圾收集器线程)。windows

因为Java采用的是单线程编程模型,所以在进行UI编程时要注意将耗时的操做放在子线程中进行,以免阻塞主线程(在UI编程时,主线程即UI线程,用来处理用户的交互事件)。架构

二.Java中如何建立线程app

在java中若是要建立线程的话,通常有两种方式:1)继承Thread类;2)实现Runnable接口。ide

  1.继承Thread类网站

继承Thread类的话,必须重写run方法,在run方法中定义须要执行的任务。

1
2
3
4
5
6
7
8
9
10
11
12
class MyThread extends Thread{
  private static int num = 0 ;
 
  public MyThread(){
  num++;
  }
 
  @Override
  public void run() {
  System.out.println( "主动建立的第" +num+ "个线程" );
  }
}

建立好了本身的线程类以后,就能够建立线程对象了,而后经过start()方法去启动线程。注意,不是调用run()方法启动线程,run方法中只是定义须要执行的任务,若是调用run方法,即至关于在主线程中执行run方法,跟普通的方法调用没有任何区别,此时并不会建立一个新的线程来执行定义的任务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test {
  public static void main(String[] args) {
  MyThread thread = new MyThread();
  thread.start();
  }
}
 
class MyThread extends Thread{
  private static int num = 0 ;
 
  public MyThread(){
  num++;
  }
 
  @Override
  public void run() {
  System.out.println( "主动建立的第" +num+ "个线程" );
  }
}

在上面代码中,经过调用start()方法,就会建立一个新的线程了。为了分清start()方法调用和run()方法调用的区别,请看下面一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test {
  public static void main(String[] args) {
  System.out.println( "主线程ID:" +Thread.currentThread().getId());
  MyThread thread1 = new MyThread( "thread1" );
  thread1.start();
  MyThread thread2 = new MyThread( "thread2" );
  thread2.run();
  }
}
 
class MyThread extends Thread{
  private String name;
 
  public MyThread(String name){
  this .name = name;
  }
 
  @Override
  public void run() {
  System.out.println( "name:" +name+ " 子线程ID:" +Thread.currentThread().getId());
  }
}

运行结果:

从输出结果能够得出如下结论:

1)thread1和thread2的线程ID不一样,thread2和主线程ID相同,说明经过run方法调用并不会建立新的线程,而是在主线程中直接运行run方法,跟普通的方法调用没有任何区别;

2)虽然thread1的start方法调用在thread2的run方法前面调用,可是先输出的是thread2的run方法调用的相关信息,说明新线程建立的过程不会阻塞主线程的后续执行。

2.实现Runnable接口

在Java中建立线程除了继承Thread类以外,还能够经过实现Runnable接口来实现相似的功能。实现Runnable接口必须重写其run方法。

下面是一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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路径下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public abstract class Process
{
 
  abstract public OutputStream getOutputStream(); //获取进程的输出流
 
  abstract public InputStream getInputStream(); //获取进程的输入流
 
  abstract public InputStream getErrorStream(); //获取进程的错误流
 
  abstract public int waitFor() throws InterruptedException; //让进程等待
 
  abstract public int exitValue(); //获取进程的退出标志
 
  abstract public void destroy(); //摧毁进程
}

 1)经过ProcessBuilder建立进程

ProcessBuilder是一个final类,它有两个构造器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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方法的具体实现源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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语句块里面的一句:

1
2
3
4
return ProcessImpl.start(cmdarray,
  environment,
  dir,
  redirectErrorStream);

说明真正建立进程的是这一句,注意调用的是ProcessImpl类的start方法,此处能够知道start必然是一个静态方法。那么ProcessImpl又是什么类呢?该类一样位于java.lang.ProcessImpl路径下,看一下该类的具体实现:

ProcessImpl也是一个final类,它继承了Process类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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对象的:

1
return new ProcessImpl(cmdarray, envblock, dir, redirectErrorStream);

而在ProcessImpl中对Process类中的几个抽象方法进行了具体实现。

说明事实上经过ProcessBuilder的start方法建立的是一个ProcessImpl对象。

下面看一下具体使用ProcessBuilder建立进程的例子,好比我要经过ProcessBuilder来启动一个进程打开cmd,并获取ip地址信息,那么能够这么写:

1
2
3
4
5
6
7
8
9
10
11
12
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)设置进程的环境变量以及工做目录等,感兴趣的朋友能够查看相关API文档。

  2)经过Runtime的exec方法来建立进程

首先仍是来看一下Runtime类和exec方法的具体实现,Runtime,顾名思义,即运行时,表示当前进程所在的虚拟机实例。

因为任何进程只会运行于一个虚拟机实例当中,因此在Runtime中采用了单例模式,即只会产生一个虚拟机实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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方法:

1
2
3
4
5
6
7
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地址信息:

1
2
3
4
5
6
7
8
9
10
11
12
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中如何建立线程和进程的话,暂时就讲这么多了,感兴趣的朋友能够参考相关资料、

参考资料:

http://luckykapok918.blog.163.com/blog/static/205865043201210272168556/

http://www.cnblogs.com/ChrisWang/archive/2009/12/02/use-java-lang-process-and-processbuilder-to-create-native-application-process.html

http://lavasoft.blog.51cto.com/62575/15662/

问啊-一键呼叫程序员答题神器,牛人一对一服务,开发者编程必备官方网站:www.wenaaa.com

QQ群290551701 汇集不少互联网精英,技术总监,架构师,项目经理!开源技术研究,欢迎业内人士,大牛及新手有志于从事IT行业人员进入!

相关文章
相关标签/搜索