Created
December 3, 2014 02:06
-
-
Save sksullivan/8e458c54e8ffc73fab90 to your computer and use it in GitHub Desktop.
Improved Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------MonteCarloThread.java------------------------- | |
public class MonteCarloThread implements Runnable { | |
private Connection connection; | |
private static int count = 0; | |
private final int STEPS_IN_BLOCK; | |
private long totalHits; | |
private long totalRuns; | |
public MonteCarloThread(int steps, Connection connection) { | |
STEPS_IN_BLOCK = steps; | |
this.connection = connection; | |
System.out.println(this.connection); | |
} | |
public double getRuns() { | |
return totalRuns; | |
} | |
public double getHits() { | |
return totalHits; | |
} | |
@Override | |
public void run() { | |
MonteCarlo accumulator = new MonteCarlo(); | |
while (!Thread.interrupted()) { | |
accumulator.run(STEPS_IN_BLOCK); | |
count++; | |
System.out.println(count); | |
} | |
totalHits = accumulator.getHits(); | |
totalRuns = accumulator.getRuns(); | |
System.out.println(); | |
System.out.println(totalHits); | |
System.out.println(totalRuns); | |
connection.post(totalHits, totalRuns); | |
} | |
} | |
-------------------------DCApp.java------------------------- | |
/** | |
* Created by Stephen S. and Sophia L. on 10/11/14. | |
*/ | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
public class DCApp implements ActionListener { | |
static Connection connection; | |
JTextField runsBox = new JTextField(40); | |
JTextArea log = new JTextArea(10, 10); | |
JButton button = new JButton("Click Me!"); | |
JFrame frame = new JFrame("Calculating Pi"); | |
static DCApp app; | |
String str; | |
int val_str; | |
public static void main(String[] args) { | |
app = new DCApp(); | |
app.init(); | |
} | |
public void init() { | |
DCApp gui = new DCApp(); | |
frame.setVisible(true); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
button.addActionListener(gui); | |
runsBox.addKeyListener(new KeyAdapter() { | |
public void keyTyped (KeyEvent e) { | |
char c = e.getKeyChar(); | |
if (!(Character.isDigit(c)) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)) | |
{ | |
e.consume(); | |
} | |
} | |
}); | |
frame.getContentPane().setLayout(new BorderLayout()); | |
frame.getContentPane().add(button, BorderLayout.NORTH); | |
JPanel panel = new JPanel(); | |
frame.getContentPane().add(panel); | |
panel.add(new JLabel("Runs: ")); | |
panel.add(runsBox); | |
log.setEditable(true); | |
log.setRows(99); | |
frame.getContentPane().add(log, BorderLayout.CENTER); | |
frame.getContentPane().add(runsBox, BorderLayout.SOUTH); | |
frame.setSize(600, 600); | |
connection = new Connection("http://tensile-tenure-727.appspot.com/totals"); | |
threadGroup = makeThreadGroup(Thread.MIN_PRIORITY); | |
runAll(); | |
} | |
public void actionPerformed(ActionEvent evt) { | |
interruptAll(); | |
str = app.runsBox.getText(); | |
if (str.length() != 0) | |
{ | |
val_str = Integer.parseInt(str); | |
} | |
try { | |
if (!(str.equals("") || val_str == 0)) { | |
System.out.println(str); | |
MonteCarloSim sim = new MonteCarloSim(Integer.parseInt(str.trim())); | |
sim.runAll(); | |
long hits = sim.getHits(); | |
System.out.println(hits); | |
connection.post(hits, Integer.parseInt(str.trim())); | |
} else { | |
JOptionPane.showMessageDialog(null, "Please only insert a positive integer for the number of runs."); | |
System.out.println("User did not insert a valid value for the number of runs."); | |
} | |
} catch (Exception e) { | |
JOptionPane.showMessageDialog(null, "Exception Occurred."); | |
} | |
} | |
private static final int STEPS = 1000; | |
private static Thread[] threadGroup; | |
private static Thread[] makeThreadGroup(int priority) { | |
Runtime runtime = Runtime.getRuntime(); | |
Thread[] threadGroup = new Thread[runtime.availableProcessors()]; | |
for (int i = 0; i < runtime.availableProcessors(); i++) { | |
Thread thread = new Thread(new MonteCarloThread(STEPS,connection)); | |
thread.setPriority(priority); | |
threadGroup[i] = thread; | |
} | |
return threadGroup; | |
} | |
public static void runAll() { | |
for (Thread thread : threadGroup) { | |
System.out.println(thread); | |
thread.start(); | |
} | |
} | |
public static void interruptAll() { | |
for (Thread thread : threadGroup) { | |
thread.interrupt(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment