Java中实现函数的阻塞

使用Object.wait()即可实现阻塞,使用Object.notify()解除阻塞,代码示例如下

MainFrame.java

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainFrame extends JFrame {

    private JButton blockButton, unblockButton;
    private final BlockTest blockTest = new BlockTest();
    private static final Logger LOGGER = Logger.getLogger(MainFrame.class.getName());

    private MainFrame() {
        initComponents();
        initFrame();
    }

    private void initComponents() {
        blockButton = new JButton("阻塞测试");
        unblockButton = new JButton("解除阻塞");
        unblockButton.setEnabled(false);
        blockButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent ev) {
                if (blockButton.isEnabled()) {
                    //开启一个线程来完成阻塞测试,以避免阻塞应用界面而造成无法响应鼠标键盘的输入
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                blockButton.setEnabled(false);
                                unblockButton.setEnabled(true);
                                blockTest.block();  //处于阻塞状态,以下finally块中的代码在阻塞被解除后才会执行
                            } catch (InterruptedException ex) {
                                LOGGER.log(Level.SEVERE, null, ex);
                            } finally {
                                out.println("mouseClicked(MouseEvent) called");
                            }
                        }
                    }.start();
                }
            }
        });
        unblockButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent ev) {
                if (unblockButton.isEnabled()) {
                    blockTest.unblock();
                    unblockButton.setEnabled(false);
                    blockButton.setEnabled(true);
                }
            }
        });
        setLayout(new GridLayout(2, 1));
        add(blockButton);
        add(unblockButton);
    }

    private void initFrame() {
        setTitle("block test");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
    }
}

BlockTest.java

import static java.lang.System.out;

public class BlockTest {

    public void block() throws InterruptedException {
        synchronized (this) {
            out.println("阻塞测试");
            wait();
            out.println("阻塞状态已解除");
        }
    }

    public void unblock() {
        synchronized (this) {
            notify();
            out.println("解除阻塞");
        }
    }
}

使用while循环结合sleep也能实现阻塞,不过对cpu的占用略高

原文地址:https://www.cnblogs.com/buyishi/p/8394842.html