---
title: "线程的启动，暂停和终止"
author: "xiaomanetjava"
date: "2010-10-05T19:04:45Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-10-05T19:04:45Z"
id: 777522
url: "https://xiaomanetjava.iteye.com/blog/777522"
---

线程的启动，暂停和终止

     线程的启动暂停和终止，听起来很简单，不过经常有初学者在这个问题上卡住，经常是启动了，能暂停，但再要启动就不行了，其实这个问题比较容易解决，思路也很简单，就是在启动线程的时候用一个判断语句锁定整个run()方法就可以了。

     并且如何停止java的线程一直是一个困恼我们开发多线程程序的一个问题。这个问题最终在Java5的[java.util.concurrent](http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html)中得到了回答：使用[interrupt()](http://xiaomanetjava.iteye.com/admin/blogs/#interrupt%28%29)，让线程在run方法中停止。

#### 简介

在Java的多线程编程中，[java.lang.Thread](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html)类型包含了一些列的方法[start()](http://xiaomanetjava.iteye.com/admin/blogs/#start%28%29), [stop()](http://xiaomanetjava.iteye.com/admin/blogs/#stop%28%29), [stop(Throwable)](http://xiaomanetjava.iteye.com/admin/blogs/#stop%28java.lang.Throwable%29) and [suspend()](http://xiaomanetjava.iteye.com/admin/blogs/#suspend%28%29), [destroy()](http://xiaomanetjava.iteye.com/admin/blogs/#destroy%28%29) and [resume()](http://xiaomanetjava.iteye.com/admin/blogs/#resume%28%29)。通过这些方法，我们可以对线程进行方便的操作，但是这些方法中，只有[start()](http://xiaomanetjava.iteye.com/admin/blogs/#start%28%29)方法得到了保留。

在Sun公司的一篇文章《[Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?](http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html) 》中详细讲解了舍弃这些方法的原因。那么，我们究竟应该如何停止线程呢？

      下面是我在单线程中的启动，暂停，继续并重新开始运行线程的代码实现。

代码片段1

package Threadtest;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

/\**

 * 测试线程的中断，继续，中断，继续

 * 

 * @author Administrator

 * 

 \*/

public class User extends JFrame {

public void init() {

this.setTitle("测试线程");

this.setLayout(new FlowLayout());

this.setSize(300, 500);

// 创建一个进度条对象

javax.swing.JProgressBar jpa = new javax.swing.JProgressBar();

jpa.setMaximum(600);// 设置最大度

jpa.setValue(0);

this.add(jpa);

final javax.swing.JLabel la = new javax.swing.JLabel("显示区");

this.add(la);

javax.swing.JButton bu1 = new javax.swing.JButton("开始");

this.add(bu1);

javax.swing.JButton bu2 = new javax.swing.JButton("停止");

this.add(bu2);

this.setVisible(true);

// 创建并启动后台线程:

final Worker worker = new Worker(la, jpa, bu1, bu2);

worker.start();

bu1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 按钮上的标签应改为停止 OK

worker.changeState();

}

});

bu2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 按钮上的标签应改为停止 OK

worker.stopState();

}

});

}

/\**

 * @param args

 \*/

public static void main(String\[] args) {

User u = new User();

u.init();

}

}

代码片段2

package Threadtest;

import javax.swing.JButton;

import javax.swing.JProgressBar;

//1.继承Thread类

//2.重写run方法

public class Worker extends Thread {

private javax.swing.JLabel la;

private boolean isSleep = true;// 暂停标志

private javax.swing.JProgressBar jpa;

private javax.swing.JButton bu1;

private javax.swing.JButton bu2;

private boolean stop = true;

public Worker(javax.swing.JLabel la, JProgressBar jpa, JButton bu1,

JButton bu2) {

this.jpa = jpa;

this.la = la;

this.bu1 = bu1;

this.bu2 = bu2;

}

// 指示线程是否修改标签上的内容

public void changeState() {

isSleep = !isSleep;

bu1.setText("暂停");

}

public void stopState() {

stop = !stop;

}

// 线程的运行，是从run方法开始执行

// 当run方法执行完毕时,一个线程退出!

public void run() {

int count = 0;

while (true) {

if (stop) {

if (!isSleep) {

bu1.setText("开始");

count += 10;

jpa.setValue(count);

String s = "" + count;

la.setText(s);

}

try {

Thread.sleep(100);

if (count == 600) {

break;

}

} catch (Exception ef) {

ef.printStackTrace();

}

}

}

}

}

&lt;!--EndFragment--&gt;