温馨提示:本范例中的随机数据文件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();
}
});
}
}