java多线程编程
进程和线程
Java给多线程编程提供了内置的支持。一个多线程程序包含两个或多个能并发运行的部分。程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径。
多线程是多任务的一种特别的形式,但多线程使用了更小的资源开销。
这里定义和线程相关的另一个术语 - 进程:一个进程包括由操作系统分配的内存空间,包含一个或多个线程。一个线程不能独立的存在,它必须是进程的一部分。一个进程一直运行,直到所有的非守候线程都结束运行后才能结束。
多线程能满足程序员编写高效率的程序来达到充分利用CPU的目的。
一个线程的生命周期
- 新建状态:
使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态。它保持这个状态直到程序start() 这个线程。
- 就绪状态:
当线程对象调用了start()方法之后,该线程就进入就绪状态。就绪状态的线程处于就绪队列中,要等待JVM里线程调度器的调度。
- 运行状态:
如果就绪状态的线程获取 CPU 资源,就可以执行 run(),此时线程便处于运行状态。处于运行状态的线程最为复杂,它可以变为阻塞状态、就绪状态和死亡状态。
- 阻塞状态:
如果一个线程执行了sleep(睡眠)、suspend(挂起)等方法,失去所占用资源之后,该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。
- 死亡状态:
一个运行状态的线程完成任务或者其他终止条件发生时,该线程就切换到终止状态。
线程的实现
创建线程有两种方式:
1.实现Runable接口;
2.继承Thread类本身。
通过实现Runnable接口创建线程
创建一个线程,最简单的方法是创建一个实现Runnable接口的类。
为了实现Runnable,一个类只需要执行一个方法调用run(),声明如下:
public void run()
你可以重写该方法,重要的是理解的run()可以调用其他方法,使用其他类,并声明变量,就像主线程一样。
在创建一个实现Runnable接口的类之后,你可以在类中实例化一个线程对象。
Thread定义了几个构造方法,下面的这个是我们经常使用的:
Thread(Runnable threadOb,String threadName);
这里,threadOb 是一个实现Runnable 接口的类的实例,并且 threadName指定新线程的名字。
新线程创建之后,你调用它的start()方法它才会运行。
void start();
实例
package thread;
//创建一个新线程
class NewThread implements Runnable{
Thread t;
NewThread(){
//创建第二个新线程
t = new Thread(this,"Demo Thread");
System.out.println("Child thread: " + t);
t.start();//开始线程
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
for(int i = 5; i > 0; i--){
System.out.println("Child thread " + i);
//暂停线程
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread");
}
}
public class ThreadDemo {
/**
* @param args
*/
public static void main(String[] args) {
new NewThread();//创建一个新线程
try {
for(int i = 5; i > 0; i--){
System.out.println("Main thread interrupted.");
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting");
}
}
编译以上程序运行结果如下:
Child thread: Thread[Demo Thread,5,main]
Main thread interrupted.
Child thread 5
Child thread 4
Main thread interrupted.
Child thread 3
Child thread 2
Main thread interrupted.
Child thread 1
Exiting child thread
Main thread interrupted.
Main thread interrupted.
Main thread exiting
通过继承Thread来创建线程
创建一个线程的第二种方法是创建一个新的类,该类继承Thread类,然后创建一个该类的实例。
继承类必须重写run()方法,该方法是新线程的入口点。它也必须调用start()方法才能执行。
实例
package thread;
class NewThread1 extends Thread{
NewThread1(){
super("Demo Thread");
System.out.println("Child thread: "+ this);
start();
}
@Override
public void run() {
try {
for(int i = 5; i > 0; i--){
System.out.println("Child Thread: " + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread");
}
}
public class ExtendThread {
/**
* @param args
*/
public static void main(String[] args) {
new NewThread();
try {
for(int i = 5; i > 0; i--){
System.out.println("Main Thread: " + i);
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
编译以上程序运行结果如下:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child thread 5
Child thread 4
Child thread 3
Main Thread: 4
Child thread 2
Child thread 1
Main Thread: 3
Exiting child thread
Main Thread: 2
Main Thread: 1
Main thread exiting.
线程的几个主要概念:
在多线程编程时,你需要了解以下几个概念:
- 线程同步
- 线程间通信
- 线程死锁
- 线程控制:挂起、停止和恢复
多线程的使用
有效利用多线程的关键是理解程序是并发执行而不是串行执行的。例如:程序中有两个子系统需要并发执行,这时候就需要利用多线程编程。
通过对多线程的使用,可以编写出非常高效的程序。不过请注意,如果你创建太多的线程,程序执行的效率实际上是降低了,而不是提升了。
请记住,上下文的切换开销也很重要,如果你创建了太多的线程,CPU花费在上下文的切换的时间将多于执行程序的时间!