如何实现线程的开启、暂停和停止

来源:互联网 发布:linux mkdir创建目录 编辑:程序博客网 时间:2024/05/22 02:08
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class ThreadDemo extends JFrame implements ActionListener {
  4. private JProgressBar progress;// 声明进度条
  5. private JButton btnStart, btnPause, btnStop;// 声明按钮
  6. private JToolBar toolBar;// 声明状态栏
  7. private MyThread myThread;
  8. // 创建构造函数
  9. public ThreadDemo() {
  10. this.setSize(400, 150);// 设置窗体的大小
  11. this.setLocationRelativeTo(null);// 设置窗体的初始位置,此为绝对据中
  12. this.setResizable(false);// 设置窗体是否能被更改大小
  13. this.setLayout(null);// 窗体不使用任何的布局方式
  14. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体的关闭方式
  15. this.setVisible(true);// 显示窗体
  16. this.init();// 加载其他组建
  17. this.initListener();// 加载监听事件
  18. }

  19. // 初始化其他组建
  20. public void init() {
  21. progress = new JProgressBar();
  22. toolBar = new JToolBar();
  23. toolBar.add(new JLabel("state"));
  24. toolBar.add(progress);
  25. toolBar.setFloatable(false);
  26. toolBar.setBounds(15, 80, 350, 21);
  27. btnStart = new JButton("start");
  28. btnStart.setBounds(12, 10, 91, 21);
  29. btnPause = new JButton("Pause");
  30. btnPause.setBounds(131, 10, 91, 21);
  31. btnStop = new JButton("stop");
  32. btnStop.setBounds(241, 10, 91, 21);
  33. this.getContentPane().add(btnStart);
  34. this.getContentPane().add(btnPause);
  35. this.getContentPane().add(btnStop);
  36. this.getContentPane().add(toolBar);
  37. }


  38. // 初始化监听事件
  39. public void initListener() {
  40. this.btnStart.addActionListener(this);
  41. this.btnPause.addActionListener(this);
  42. this.btnStop.addActionListener(this);
  43. }

  44. public static void main(String[] args) {
  45. new ThreadDemo();
  46. }

  47. class MyThread extends Thread {
  48. private boolean flag = false;// 设置线程的标志位
  49. private boolean pause = false;// 设置线程的暂停位

  50. // 重写线程的启动方法
  51. public void setStart() {
  52. this.flag = true;
  53. super.start();
  54. }

  55. // 重写线程的运行方法
  56. public void run() {
  57. int value = 0;
  58. while (flag) {
  59. try {
  60. if (pause) {
  61. Thread.sleep(50);
  62. continue;
  63. }
  64. progress.setValue(value++);
  65. if (progress.getValue() == progress.getMaximum()) {
  66. break;
  67. }
  68. Thread.sleep(50);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }


  74. // 暂停线程
  75. public void setPause() {
  76. this.pause = !pause;
  77. }

  78. // 关闭线程
  79. public void setStop() {
  80. progress.setValue(0);
  81. this.flag = false;
  82. }
  83. }
  84. // 重写监听事件
  85. public void actionPerformed(ActionEvent e) {
  86. // 开始事件
  87. if (e.getSource() == this.btnStart) {
  88. System.out.println("启动线程");
  89. myThread = new MyThread();
  90. myThread.setStart();
  91. }
  92. // 暂停事件
  93. if (e.getSource() == this.btnPause) {
  94. System.out.println("暂停线程");
  95. myThread.setPause();
  96. }
  97. // 停止事件
  98. if (e.getSource() == this.btnStop) {
  99. System.out.println("终止线程");
  100. myThread.setStop();
  101. }
  102. }
  103. }
  104. // 创建构造函数
public ThreadDemo() {
this.setSize(400, 150);// 设置窗体的大小
this.setLocationRelativeTo(null);// 设置窗体的初始位置,此为绝对据中
this.setResizable(false);// 设置窗体是否能被更改大小
this.setLayout(null);// 窗体不使用任何的布局方式
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体的关闭方式
this.setVisible(true);// 显示窗体
this.init();// 加载其他组建
this.initListener();// 加载监听事件
}
// 初始化其他组建
public void init() {
progress = new JProgressBar();
toolBar = new JToolBar();
toolBar.add(new JLabel("state"));
toolBar.add(progress);
toolBar.setFloatable(false);
toolBar.setBounds(15, 80, 350, 21);
btnStart = new JButton("start");
btnStart.setBounds(12, 10, 91, 21);
btnPause = new JButton("Pause");
btnPause.setBounds(131, 10, 91, 21);
btnStop = new JButton("stop");
btnStop.setBounds(241, 10, 91, 21);
this.getContentPane().add(btnStart);
this.getContentPane().add(btnPause);
this.getContentPane().add(btnStop);
this.getContentPane().add(toolBar);
}


// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}

public static void main(String[] args) {
new ThreadDemo();
}

class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位

// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化其他组建
public void init() {
progress = new JProgressBar();
toolBar = new JToolBar();
toolBar.add(new JLabel("state"));
toolBar.add(progress);
toolBar.setFloatable(false);
toolBar.setBounds(15, 80, 350, 21);
btnStart = new JButton("start");
btnStart.setBounds(12, 10, 91, 21);
btnPause = new JButton("Pause");
btnPause.setBounds(131, 10, 91, 21);
btnStop = new JButton("stop");
btnStop.setBounds(241, 10, 91, 21);
this.getContentPane().add(btnStart);
this.getContentPane().add(btnPause);
this.getContentPane().add(btnStop);
this.getContentPane().add(toolBar);
}

// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}

public static void main(String[] args) {
new ThreadDemo();
}

class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位

// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}

public static void main(String[] args) {
new ThreadDemo();
}

class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位

// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}
public static void main(String[] args) {
new ThreadDemo();
}

class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位

// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位

// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}

// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}


// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}

// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}

// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
原创粉丝点击