Java模拟音乐播放器 暂停与重新播放——线程如何控制另外一个线程的状态

package com.example.Thread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 达达队长 on 2018/4/28.
 */


class Count extends Thread {

    private volatile int stop = 1;
    public volatile int i = 0;
    private JLabel count;

    public Count(JLabel count) {
        this.count = count;
    }

    @Override
    public void run() {
        try {
            synchronized (this) {
                while (true) {
                    if (stop == 0) {
                        wait();
                    } else if (stop == 1) {
                        count.setText(i++ + "");
                    } else if (stop == 2) {
                        return;
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void setStop() {
        stop = 0;
    }

    public synchronized void setStart() {
        stop = 1;
        notify();
    }

    public void setKill() {
        stop = 2;
    }
}

public class MyTimer {

    public static int lableSize = 1;
    public static Map<JButton, Count> threadMap = new HashMap<JButton, Count>(16);

    public static void main(String[] args) {


        // 创建 JFrame 实例
        JFrame frame = new JFrame("多线条启停");
        // Setting the width and height of frame
        frame.setSize(1080, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* 创建面板,这个类似于 HTML 的 div 标签
         * 我们可以创建多个面板并在 JFrame 中指定位置
         * 面板中我们可以添加文本字段,按钮及其他组件。
         */


        /*
         * 调用用户定义的方法并添加组件到面板
         */
        placeComponents(frame);

        // 设置界面可见
        frame.setVisible(true);
    }

    private static void placeComponents(final JFrame frame) {

        final JPanel panel = new JPanel();

        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(panel);

        // 创建登录按钮
        JButton loginButton = new JButton("添加计数器");
        loginButton.setBounds(10, 10, 80, 25);
        panel.add(loginButton);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 创建 JLabel
                JLabel userLabel = new JLabel("计数器" + lableSize + ":");
                userLabel.setBounds(10, 30 * lableSize, 40, 25);
                panel.add(userLabel);

                final JLabel count = new JLabel("0");
                count.setBounds(100, 30 * lableSize, 40, 25);
                panel.add(count);


                final JButton button = new JButton("启动");
                button.setBounds(200, 30 * lableSize, 80, 25);
                panel.add(button);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        if (button.getText().equals("启动")) {
                            Count thread = threadMap.get(button);
                            if (null == thread) {
                                thread = new Count(count);
                                threadMap.put(button, thread);
                                thread.start();
                            } else {
                                thread.setStart();
                            }
                            button.setText("暂停");

                        } else if (button.getText().equals("暂停")) {
                            Count thread = threadMap.get(button);
                            thread.setStop();
                            button.setText("启动");

                        }
                    }
                });


                final JButton kill = new JButton("停止");
                kill.setBounds(300, 30 * lableSize, 80, 25);
                panel.add(kill);
                kill.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Count thread = threadMap.get(button);
                        if (null == thread) {

                        } else {
                            thread.setKill();
                            System.out.println(thread.isAlive());
                            count.setText("0");
                            threadMap.remove(button);
                            button.setText("启动");
                        }
                    }
                });


                lableSize++;
                frame.repaint();
            }
        });
    }

}
原文地址:https://www.cnblogs.com/Yiran583/p/8961497.html