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的占用略高


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM