如何使用子线程读文件随时暂停子线程和恢复子线程的执行以及控制主线程界面上控件实时输出还有多个button状态来回切换

温馨提示:本范例中的随机数据文件HumanBigData1.csv 一共有5亿行大小56GB ;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.contoso;
/*
  Author: zhengzizhi@126.com
  CreateDate: 2019-04-04
*/

import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChildThread extends Thread {

    private JTextField txtInputPath;
    private JTextArea txaPrintContent;
    private JButton btnStart;
    private JButton btnPause;
    private JButton btnResume;

    private static final String runningStatus = "1";
    private static final String pauseStatus = "2";
    private String runStatus = runningStatus;

    public ChildThread() {

    }

    public ChildThread(JTextField txtInputPath, JTextArea txaPrintContent, JButton btnStart, JButton btnPause, JButton btnResume) {
        this.txtInputPath = txtInputPath;
        this.txaPrintContent = txaPrintContent;
        this.btnStart = btnStart;
        this.btnPause = btnPause;
        this.btnResume = btnResume;
    }

    public String getRunStatus() {
        return runStatus;
    }

    public void setRunStatus(String runStatus) {
        this.runStatus = runStatus;
    }

    public String getPauseStatus() {
        return pauseStatus;
    }

    @Override
    public void run() {
        btnStart.setVisible(false);
        btnPause.setEnabled(true);
        btnResume.setEnabled(false);
        txaPrintContent.setText("");
        long startTime = System.currentTimeMillis();
        String rline = "";
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(txtInputPath.getText()));
            while ((rline = bufferedReader.readLine()) != null) {
                synchronized (pauseStatus) {
                    txaPrintContent.append(rline + "\n");
                    if (txaPrintContent.getLineCount() > 5000) {
                        txaPrintContent.setText("");
                    }
                    if (runStatus.equals(pauseStatus)) {
                        btnPause.setEnabled(false);
                        btnResume.setEnabled(true);
                        pauseStatus.wait();
                        btnPause.setEnabled(true);
                        btnResume.setEnabled(false);
                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception e) {
                }
            }
        }
        long endTime = System.currentTimeMillis();
        txaPrintContent.setText("读大数据文件共耗时(秒): " + (endTime - startTime) / 1000);
        btnStart.setVisible(true);
        btnPause.setEnabled(true);
        btnResume.setEnabled(false);
    }

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.contoso;
/*
  Author: zhengzizhi@126.com
  CreateDate: 2019-04-04
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class MainForm extends JFrame {

    private JPanel contentPane;
    private JTextField txtInputPath;
    private JTextArea txaPrintContent;
    private JButton btnStart;
    private JButton btnPause;
    private JButton btnResume;
    private JScrollPane scrollPane;
    
    ChildThread childThread;

    private static final String runningStatus = "1";
    private static final String pauseStatus = "2";

    protected void onPause() {
        if (childThread != null) {
            childThread.setRunStatus(pauseStatus);
        }
    }

    protected void onResume() {
        if (childThread != null) 
            synchronized (pauseStatus) {
                childThread.setRunStatus(runningStatus);
                childThread.getPauseStatus().notifyAll();
           }
    }
    
    public JTextArea getPrintContent() {
        return txaPrintContent;
    }

    public void setPrintContent(JTextArea txaPrintContent) {
        this.txaPrintContent = txaPrintContent;
    }
    
    public JScrollPane getScrollPane() {
        return scrollPane;
   }

    public static void main(String[] args) {
         new MainForm();
    }

    public MainForm() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        setTitle("如何使用子线程读文件");
        setSize(1280, 985);
        contentPane.setLayout(null);

        JLabel lblName = new JLabel("实时打印文件的内容:");
        lblName.setBounds(25, 6, 200, 23);
        contentPane.add(lblName);
        
        scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setBounds(10, 32, 1250, 800);
        contentPane.add(scrollPane);

        txaPrintContent = new JTextArea();
        txaPrintContent.setBounds(10, 32, 1250, 800);
        scrollPane.setViewportView(txaPrintContent);
            
        JLabel lblInputPath = new JLabel("输入文件路径");
        lblInputPath.setBounds(10, 865, 120, 23);
        contentPane.add(lblInputPath);

        txtInputPath = new JTextField();
        txtInputPath.setText("D:\\resultBigDataFile\\HumanBigData1.csv");
        txtInputPath.setBounds(100, 865, 320, 23);
        contentPane.add(txtInputPath);

        btnStart = new JButton("启动线程");
        btnStart.setBounds(440, 865, 100, 23);
        contentPane.add(btnStart);
        
        btnPause = new JButton("暂停线程");
        btnPause.setBounds(545, 865, 100, 23);
        contentPane.add(btnPause);
        
        btnResume = new JButton("恢复线程");
        btnResume.setBounds(650, 865, 100, 23);
        contentPane.add(btnResume);

        setVisible(true);
        btnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {              
              childThread = new ChildThread(txtInputPath,txaPrintContent,btnStart,btnPause,btnResume);
              childThread.start();  
            }
        });
        
        btnPause.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onPause();
            }
        });
        
        btnResume.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onResume();
            }
        });

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值