The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

a threaded solution (thanks)

Posted by David Fashena on January 30, 2002 at 6:29 PM

Thanks very much for pointing me in the right direction. Or at least in _a_ right direction. When I put the time-consuming number crunching in its own thread, it worked. I had been putting off learning about Threads, so this turned out to be a valuable exercise. I've appended an improved version of my little Pi applet below. Thanks again.

import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Pi_applet extends JApplet implements ActionListener {

public JTextField its_jtf = new JTextField(null, 20);
public JTextField pi_jtf = new JTextField(null, 20);

JButton start_button = new JButton("Start Calculation");
JLabel result_label = new JLabel("Estimate of Pi = ", SwingConstants.RIGHT);

JTextArea info_jta = new JTextArea(20,40);
JTextArea its_jta = new JTextArea(20,40);

JPanel pane = new JPanel();

double pi_est=0.0; // estimate of Pi
int its; // desired number of iterations

public void init() {

start_button.addActionListener(this);

its_jtf.addActionListener(this);

pane.setLayout(new GridLayout(3,2));

info_jta.append("This applet computes an estimate of Pi,\n using the formula: \nPi ~ 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - ...");
its_jta.append( "Enter number of iterations in the box \nbelow. The more iterations, the more \naccurate the estimate will be. ");

pane.add(new JScrollPane(info_jta));
pane.add(new JScrollPane(its_jta));

pane.add(start_button);
pane.add(its_jtf);
pane.add(result_label);
pane.add(pi_jtf);

setContentPane(pane);
this.setVisible(true);
}

private Pi_task pt = null;

public void actionPerformed(ActionEvent e) {
try {
String num_str = its_jtf.getText();
its = Integer.parseInt(num_str);
if (its<0) throw new NoNegException();

pi_jtf.setText("Working...");

pt = new Pi_task();
// start the computation in its own thread

result_label.setText("Pi estimate is: " );
}
catch(NumberFormatException nf_err) {
result_label.setText("Please enter an integer. ");
pi_jtf.setText(" ooops! ");
}
catch(NoNegException no_neg) {
result_label.setText("Non-negatives only ! ");
pi_jtf.setText(" oopsie! ");
}

}

class NoNegException extends Exception {}

class Pi_task extends Thread {
Pi_task() { start(); }
public void run() {
pi_est = crunch(its);
pi_jtf.setText(Double.toString(pi_est));
return;
}
double crunch(int max) { // compute estimate of Pi
double b = 1.0, s= 0.0, FOUR = 4.0,fm;
int n, flip = 1;
for (n=0; n< max; n++) {
s += flip*FOUR/b;
b = b+2.0;
flip *= -1;
}
return(s);
}
}


}






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us