欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java线程之线程的生命周期,java线程生命周期,线程的生命周期和人有生老

来源: javaer 分享于  点击 46859 次 点评:193

Java线程之线程的生命周期,java线程生命周期,线程的生命周期和人有生老


线程的生命周期

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

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

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

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

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

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

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

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

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

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

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

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 <a href="http://blog.csdn.net/mq612">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();    }}//该片段来自于http://byrx.net
相关栏目:

用户点评