java简述线程生命周期,Java线程之线程的生命周期

线程的生命周期

和人有生老病死一样,线程也有它完整的生命周期:

1.         新生(New):代表线程的对象已经被初始化,但尚未运行run方法。

2.         可执行(Runnable):线程正在运行run方法,但这只说明线程目前处于的状态,如果系统没有能力拨出CPU执行时间给线程,线程就“不执行”,这里的“不执行”不代表“停滞”或“死亡”。

3.         停滞(Blcked):线程是可以执行的,但由于某些因素的阻碍处于停滞状态,系统排程器略过了应给的CPU执行时间。

4.         死亡(Dead):线程的正式结束方式,run方法执行完毕并返回。

如果用我们上学来解释线程的生命周期则更容易理解:

1.         我们在进行了一系列考察后选择了这个学校,填写入学登记表、交纳学费、打印听课证、获取课程表,这时我们已经成功的“初始化”了求学的线程;

2.         当我们第一天上课的时候,求学线程就处于“可执行”状态,法定节假日只是学校没有能力来安排课程,并不是求学线程的“停滞”或“死亡”;

3.         求学的过程是很艰辛且漫长的,如果哪天晚上我一高兴,和几个哥们对酒当歌,直喝的月朦胧、鸟朦胧,宿舍的床铺也朦胧,第二天早晨只觉得头晕脑胀、四肢无力,这时才想起求学线程还在进行中,可又偏偏心有余而力不足,不得已打电话给班主任老师,由于某某原因,需要请假一天,无奈求学线程被停滞;

4.         有起点就会有终点、有开始就会有结束,在我们顺利的通过了毕业考试后,求学线程便理所应当的画上一个圆满的句号。

在Thread中应该如何更好的控制整个生命周期,我们先来看看Thread类代码,下面的方法会影响到线程的生命周期,为了方便省略了实现细节:

package java.lang;

public class Thread{

public void start(); // 线程的启动

public void run(); // 线程体

public void stop(); // 已废弃

public void resume(); // 已废弃

public void suspend(); // 已废弃

public static void sleep(long millis); // 在指定的毫秒数内让当前正在执行的线程休眠

public static void sleep(long millis, int nanos); // 同上,增加了纳秒参数

public boolean isAlive(); // 测试线程是否处于活动状态

public void interrupt(); // 中断线程

public boolean isInterrupted(); // 测试线程是否已经中断

public static boolean interrupted(); // 测试当前线程是否已经中断

public void join() throws InterruptedException; // 等待该线程终止

public void join(long millis) throws InterruptedException; // 等待该线程终止的时间最长为 millis 毫秒

public void join(long millis, int nanos) throws InterruptedException; // 等待该线程终止的时间最长为 millis 毫秒 + nanos 纳秒

}

重点:停滞状态

1.         通过sleep(millis)使线程进入休眠,该方法在指定的时间内无法被唤醒。

2.         通过suspend()暂停线程,除非线程收到resume()消息,否则不会变回可执行状态。(该方法已经废弃)

3.         通过wait() 暂停线程,除非收到notify()或notifyAll()消息,否则不会变回可执行状态。wait()和notify()两个函数都是Object的一部分,不像sleep()那样属于Thread,这是因为两个函数会取用对象的机锁,而机锁正是每个继承自Object对象都拥有的。如此一来我们可以把wait()置于任何同步函数内,也只能在同步函数中调用wait()。sleep()、suspend()、resume()可以在所有非同步函数中使用,因为它们不会取用机锁。

4.         线程正在等待某个IO动作完成。

5.         线程正尝试调用一个同步对象,但尚未取得该对象机锁。

下面的例子表现了一个完整的线程生命周期:

1.[代码]TestMain6.java

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**

* 线程的生命周期

* 一个线程的生命周期分为四种状态:新生、可执行、停滞、死亡

* 我们在本例中对一个线程进行上述操作

* @author 五斗米

* @blog http://blog.csdn.net/mq612

*/

public class TestMain6 extends JFrame {

private MyThread thread = null; // 要操作的线程

private JTextField text = null; // 运行计数器

private JLabel label = null; // 显示线程运行状态

private JButton newButton = null, startButton = null,

waitButton = null, stopButton = null; // 新生、启动、停滞、死亡 四个按钮

private boolean isWait = false; // 是否为暂停状态

/**

* 构造一个银行存取款界面

*/

public TestMain6(){

super("线程生命周期");

text = new JTextField(25);

label = new JLabel(" ");

newButton = new JButton("新生");

newButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

thread = new MyThread();

label.setText("新生");

}

});

startButton = new JButton("执行");

startButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

thread.start();

label.setText("执行");

}

});

waitButton = new JButton("停滞");

waitButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(!isWait){ // 如果不是暂停状态

isWait = true;

waitButton.setText("继续");

}else{

isWait = false;

synchronized (thread){

thread.notify(); // 继续

}

waitButton.setText("停滞");

}

}

});

stopButton = new JButton("死亡");

stopButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(isWait){

isWait = false;

synchronized (thread){

thread.notify();

}

}

thread.quit();

label.setText("死亡");

}

});

JPanel pane = new JPanel();

pane.add(label);

pane.add(text);

pane.add(newButton);

pane.add(startButton);

pane.add(waitButton);

pane.add(stopButton);

this.getContentPane().add(pane);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(300, 200);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

class MyThread extends Thread{

private int i = 0; // 计数器数值

private boolean b = true; // 控制循环,也就是控制线程结束的boolean变量

public MyThread(){

i = 0;

text.setText(Integer.toString(i));

}

public void quit(){

this.b = false;

}

public synchronized void run(){

while(b){

if(isWait){ // 这里决定了线程何时停滞

try {

wait(); // 只能在同步函数中调用wait()

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

text.setText(Integer.toString(i++));

try {

this.sleep(100);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

}

public static void main(String [] args){

new TestMain6();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值