• Thread suspend()挂起resume()恢复


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ThreadVSR extends JPanel implements Runnable{
    private static final String[] symbolList={"|","/","--","\","/","--","\"};
    private Thread runThread;
    private JTextField symbolTF;

    public ThreadVSR() {
    symbolTF=new JTextField();
    symbolTF.setEditable(false);
    symbolTF.setFont(new Font("Monospaced",Font.BOLD,26));
    symbolTF.setHorizontalAlignment(JTextField.CENTER);
    final JButton suspendB=new JButton("suspend");
    final JButton resumeB=new JButton("resume");
    suspendB.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    suspendNow();
    }
    });
    resumeB.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    resumeNow();
    }
    });
    JPanel in=new JPanel();
    in.setLayout(new GridLayout(0,1,3,3));
    in.add(symbolTF);
    in.add(suspendB);
    in.add(resumeB);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.add(in);
    }
    private void suspendNow(){
    if (this.runThread!=null){
    runThread.suspend();
    }
    }
    private void resumeNow(){
    if (this.runThread!=null){
    this.runThread.resume();
    }
    }

    @Override
    public void run() {
    try {
    runThread=Thread.currentThread();
    int count=0;
    while (true){
    symbolTF.setText(symbolList[count%symbolList.length]);
    Thread.sleep(200);
    count++;
    System.out.println(count);
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    runThread=null;
    }
    public static void main(String[] args){
    ThreadVSR vrs=new ThreadVSR();
    Thread t=new Thread(vrs);
    t.start();
    JFrame f=new JFrame("suspend-resume-");
    f.setContentPane(vrs);
    f.setSize(320,200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
  • 相关阅读:
    理想中的分布式架构
    nginx+tomcat动静分离的核心配置
    Liunx下Tomcat+MYSQL+Nginx配置
    Nginx+Tomcat动静态资源分离
    Nginx+Keepalived+Tomcat之动静分离的web集群
    linux tomca几个配置文件及点
    redis实现spring-redis-data的入门实例
    Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存
    Oracle VM Virtual Box 4.3 小巧精悍的虚拟机软件
    Ubuntu下配置 keepalived+nginx+tomcat 负载均衡
  • 原文地址:https://www.cnblogs.com/feipengting/p/7553991.html
Copyright © 2020-2023  润新知