Java编写多线程计算1!+…+30!阶乘之和实现界面显示过程和结果
说下思路:
我们直接使用WindowsBuider完成对可视化界面的设计,然后添加监听事件,实现两个线程分别的任务,(线程一负责计算阶乘,线程二负责将每次的进度和结果添加到可视化界面)
具体代码:
package windows;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShowThread {
private JFrame frame;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowThread window = new ShowThread();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ShowThread() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 414, 242);
frame.getContentPane().add(panel);
panel.setLayout(null);
textArea = new JTextArea();
textArea.setBounds(101, 10, 255, 113);
panel.add(textArea);
JLabel label = new JLabel("\u8BA1\u7B97\u8FC7\u7A0B");//标签:"计算过程"
label.setBounds(23, 58, 54, 15);
panel.add(label);
textField = new JTextField();
textField.setBounds(101, 150, 260, 34);
panel.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");标签:"计算结果"
label_1.setBounds(23, 159, 54, 15);
panel.add(label_1);
JButton button = new JButton("\u5F00\u59CB\u8BA1\u7B97");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//调用计算线程
ComputeThread ct=new ComputeThread();
Thread tc=new Thread(ct);
//调用读取线程
ReadThread rt=new ReadThread();
Thread tr=new Thread(rt);
//开始线程
tc.start();
tr.start();
}
});
button.setBounds(136, 209, 93, 23);
panel.add(button);
}
class ReadThread extends Thread{//读取阶乘的进度和结果
ShowThread sThread = new ShowThread();
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
textArea.setText(ComputeThread.stringSum);//将过程显示到文本域中
textArea.setLineWrap(true);
textField.setText(ComputeThread.stringResult);//将结果显示到文本框中
try{
Thread.sleep(100);//0.1秒读取一次线程
}catch (Exception e) {
// TODO: handle exception
}
}
}
}
}
class ComputeThread extends Thread{//计算阶乘
double sum=0;//存储阶乘和
static int i=0;//存储阶乘和进度
static String stringSum="";//存储阶乘和的字符串
static String stringResult="";//存储阶乘结果的字符串
private double method(int n){//计算阶乘
double result=1;//阶乘结果
for(int i=1;i<=n;i++){
result*=i;
}
return result;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(i<30){//计算阶乘和
i++;
sum += method(i);
stringResult = String.valueOf(sum);//将阶乘和存储到字符串中
if(i!=1){//显示阶乘和的过程:1!+2!+...+30!
stringSum=stringSum+"+"+i+"!";
}else{
stringSum=i+"!";
}
try{//0.5-1秒读取一次线程
Thread.sleep((int)(Math.random()*500+500));
}catch(InterruptedException ex){
}
}
}
}
运行结果: