范例1:
package com.contoso;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
/**
*
* @author zhengzizhi@126.com
*/
public class JFrameMain extends JFrame {
private JPanel contentPane;
private JTextField txtInput;
private JButton btnStartChildThread;
private JButton btnExitChildThread;
private JTextArea txaInfoOutput;
private ChildThread childThread;
private JScrollPane outputScrollPane;
public JFrameMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(0, 0, 1000, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setTitle("如何正确的退出子线程");
contentPane.setLayout(null);
txtInput = new JTextField();
txtInput.setBounds(75, 50, 75, 23);
txtInput.setText("500000");
contentPane.add(txtInput);
btnStartChildThread = new JButton("启动子线程");
btnStartChildThread.setBounds(160, 50, 150, 23);
contentPane.add(btnStartChildThread);
btnExitChildThread = new JButton("退出子线程");
btnExitChildThread.setBounds(320, 50, 150, 23);
contentPane.add(btnExitChildThread);
JPanel outputPane = new JPanel();
outputPane.setBorder(new TitledBorder(null, " 打印输出信息: ", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(59, 59, 59)));
outputPane.setBounds(20, 90, 950, 500);//前两个参数是横纵坐标,后两个参数是宽度和高度
contentPane.add(outputPane);
outputPane.setLayout(new BorderLayout(0, 5));//布局管理器
outputScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
outputPane.add(outputScrollPane);
txaInfoOutput = new JTextArea();
outputScrollPane.setViewportView(txaInfoOutput);
btnStartChildThread.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
childThread = new ChildThread(txtInput, txaInfoOutput, outputScrollPane);
childThread.start();
}
});
btnExitChildThread.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (childThread != null) {
childThread.interrupt();
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrameMain frmMain = new JFrameMain();
frmMain.setVisible(true);
}
});
}
}
package com.contoso;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChildThread extends Thread {
private JTextField txtInput;
private JTextArea txaInfoOutput;
private JScrollPane outputScrollPane;
public ChildThread(JTextField txtInput, JTextArea txaInfoOutput, JScrollPane outputScrollPane) {
this.txtInput = txtInput;
this.txaInfoOutput = txaInfoOutput;
this.outputScrollPane = outputScrollPane;
}
@Override
public void run() {
txaInfoOutput.removeAll();
int count = Integer.parseInt(txtInput.getText().trim());
JScrollBar outputScrollBar = outputScrollPane.getVerticalScrollBar();
for (int i = 1; i <= count; i++) {
outputScrollBar.setValue(outputScrollBar.getMaximum());
if (Thread.currentThread().interrupted()) {
txaInfoOutput.append("\n" + Thread.currentThread().getName() + "当前子线程已经进入"+ Thread.currentThread().getState() +"状态,子线正在准备退出\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
break;
}
txaInfoOutput.append(i + "+");
if (i % 18 == 0) {
txaInfoOutput.append("\n");
}
}
txaInfoOutput.append("\n" + Thread.currentThread().getName() + " 当前子线程状态为: " + Thread.currentThread().getState() + "\n");
for (int i = 1; i <= 3; i++) {
outputScrollBar.setValue(outputScrollBar.getMaximum());
}
}
}
范例2:
package com.myth;
public class Example1 extends Thread {
public static volatile boolean exit = false;
public static Object obj = new Object();
public void stopMe() {
exit = true;
}
@Override
public void run() {
int i = 0;
while (true) {
if (exit) {
System.out.println("exit by stop me");
break;
}
synchronized (obj) {
i++;
System.out.println("i = " + i);
}
Thread.yield();
}
}
public static void main(String[] args) throws InterruptedException {
Example1 t = new Example1();
t.start();
Thread.sleep(50);
t.stopMe();
}
}
范例3:
package com.myth;
public class Example2 extends Thread {
@Override
public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Interrupted!");
break;
}
try{
Thread.sleep(2000);
} catch(InterruptedException e){
System.out.println("Interrupted When Sleep");
Thread.currentThread().interrupt();
}
Thread.yield();
}
}
public static void main(String[] args) throws InterruptedException {
Example2 t = new Example2();
t.start();
Thread.sleep(2000);
t.interrupt();
}
}
范例4:如何正确的退出线程组
package com.myth;
public class ThreadGroupExample {
public static void main(String[] args) {
// 创建一个名称为PrintGroup的线程组
ThreadGroup tg = new ThreadGroup("PrintGroup");
// 线程构造器初始化时将线程和线程组关联起来
Thread t1 = new Thread(tg, new Printer(), "T1");
Thread t2 = new Thread(tg, new Printer(), "T2");
Thread t3 = new Thread(tg, new Printer(), "T3");
t1.start();
t2.start();
t3.start();
System.out.println(tg.activeCount());// 打印活动线程的总数,这是个估计值,非精确值
tg.list(); // 打印这个线程组中所有的线程信息,对调试有一定的帮助
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 调用中断方法立即会抛出InterruptedException异常,
// 它会在Printer对象Thread.sleep()方法执行时捕获直接抛出的中断异常
tg.interrupt();
}
}
package com.myth;
public class Printer implements Runnable {
@Override
public void run() {
String groupinfo = Thread.currentThread().getThreadGroup().getName() + "-" + Thread.currentThread().getName();
while (true) {
System.out.println("this is " + groupinfo);
if(Thread.currentThread().isInterrupted()){
break;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
run:
3
this is PrintGroup-T1
this is PrintGroup-T3
this is PrintGroup-T2
java.lang.ThreadGroup[name=PrintGroup,maxpri=10]
Thread[T1,5,PrintGroup]
Thread[T2,5,PrintGroup]
Thread[T3,5,PrintGroup]
this is PrintGroup-T1
this is PrintGroup-T3
this is PrintGroup-T2
this is PrintGroup-T1
this is PrintGroup-T2
this is PrintGroup-T3
this is PrintGroup-T2
this is PrintGroup-T1
this is PrintGroup-T3
this is PrintGroup-T2
this is PrintGroup-T3
this is PrintGroup-T1
this is PrintGroup-T2
this is PrintGroup-T1
this is PrintGroup-T3
this is PrintGroup-T3
this is PrintGroup-T1
this is PrintGroup-T2
this is PrintGroup-T1
this is PrintGroup-T2
this is PrintGroup-T3
BUILD SUCCESSFUL (total time: 20 seconds)