Last active
August 29, 2015 14:17
-
-
Save deanveloper/9b5b4a1fc693abfbebd2 to your computer and use it in GitHub Desktop.
A problem that I will give to my AP computer science class to solve
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
import java.text.NumberFormat; | |
import java.util.Locale; | |
public class Problem{ | |
static int test = 0; | |
static boolean complete = false; | |
public static void main(String[] args){ | |
new Thread(){ | |
@Override | |
public void run(){ | |
for(int i = 0; i < 1000000000; i++){ //1 billion | |
test = test + 1; // add 1 to our variable | |
} | |
done(); | |
} | |
}.start(); | |
new Thread(){ | |
@Override | |
public void run(){ | |
for(int i = 0; i < 1000000000; i++){ //1 billion | |
test = test + 1; // add 1 to our variable | |
} | |
done(); | |
} | |
}.start(); | |
} | |
static synchronized void done(){ | |
if(complete){ | |
// If the other thread is finished, print the result | |
// adding 1 billion to 1 billion makes 2 billion, right? | |
System.out.println( formatNumber(test) ); | |
} | |
complete = true; // Mark a thread as complete | |
} | |
static String formatNumber(int i){ | |
return NumberFormat.getNumberInstance(Locale.US).format(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment