Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //code without thread use
- public static int getNextPrime(int from) {
- int nextPrime = from+1;
- boolean superPrime = false;
- while(!superPrime) {
- boolean prime = true;
- for(int i = 2;i < nextPrime;i++) {
- if(nextPrime % i == 0) {
- prime = false;
- }
- }
- if(prime) {
- superPrime = true;
- } else {
- nextPrime++;
- }
- }
- return nextPrime;
- }
- public static void main(String[] args) {
- int primeStart = 5;
- ArrayList list = new ArrayList();
- for(int i = 0;i < 10000;i++) {
- list.add(primeStart);
- primeStart = getNextPrime(primeStart);
- }
- }
- public class PrimeRunnable implements Runnable {
- private int from;
- private int lastPrime;
- public PrimeRunnable(int from) {
- this.from = from;
- }
- public boolean isPrime(int number) {
- for(int i = 2;i < from;i++) {
- if((number % i) == 0) {
- return false;
- }
- }
- lastPrime = number;
- return true;
- }
- public int getLastPrime() {
- return lastPrime;
- }
- public void run() {
- while(!isPrime(++from))
- ;
- }
- }
- public static void main(String[] args) {
- int primeStart = 5;
- ArrayList list = new ArrayList();
- for(int i = 0;i < 10000;i++) {
- PrimeRunnable pr = new PrimeRunnable(primeStart);
- Thread t = new Thread(pr);
- t.start();
- t.join();
- primeStart = pr.getLastPrime();
- list.add(primeStart);
- }
- }
- public static void main(String[] args) {
- int primeStart = 5;
- // Make thread-safe list for adding results to
- List list = Collections.synchronizedList(new ArrayList());
- // Pull thread pool count out into a value so you can easily change it
- int threadCount = 10000;
- Thread[] threads = new Thread[threadCount];
- // Start all threads
- for(int i = 0;i < threadCount;i++) {
- // Pass list to each Runnable here
- // Also, I added +i here as I think the intention is
- // to test 10000 possible numbers>5 for primeness -
- // was testing 5 in all loops
- PrimeRunnable pr = new PrimeRunnable(primeStart+i, list);
- Thread[i] threads = new Thread(pr);
- threads[i].start(); // thread is now running in parallel
- }
- // All threads now running in parallel
- // Then wait for all threads to complete
- for(int i=0; i<threadCount; i++) {
- threads[i].join();
- }
- }
- public class PrimeRunnable implements Runnable {
- private int from;
- private List results; // shared but thread-safe
- public PrimeRunnable(int from, List results) {
- this.from = from;
- this.results = results;
- }
- public void isPrime(int number) {
- for(int i = 2;i < from;i++) {
- if((number % i) == 0) {
- return;
- }
- }
- // found prime, add to shared results
- this.results.add(number);
- }
- public void run() {
- isPrime(from); // don't increment, just check one number
- }
- }
- public static void main(String[] args) {
- int primeStart = 5;
- // Make thread-safe list for adding results to
- List list = Collections.synchronizedList(new ArrayList());
- int threadCount = 16; // Experiment with this to find best on your machine
- ExecutorService exec = Executors.newFixedThreadPool(threadCount);
- int workCount = 10000; // See how # of work is now separate from # of threads?
- for(int i = 0;i < workCount;i++) {
- // submit work to the svc for execution across the thread pool
- exec.execute(new PrimeRunnable(primeStart+i, list));
- }
- // Wait for all tasks to be done or timeout to go off
- exec.awaitTermination(1, TimeUnit.DAYS);
- }
- private static int currentPrime;
- public static void main(String[] args) throws InterruptedException {
- for (currentPrime = 0; currentPrime < 10000; currentPrime++) {
- Thread t = new Thread(new Runnable() {
- public void run() {
- getNextPrime(currentPrime);
- }});
- t.run();
- t.join();
- }
- }
- for(int i = 2;i < nextPrime;i++) {
- if(nextPrime % i == 0) {
- prime = false;
- }
- }
- for(int i = 2;i < from;i++) {
- if((number % i) == 0) {
- return false;
- }
- }
- for(int i = 2;i < nextPrime;i++) {
- if(nextPrime % i == 0) {
- prime = false;
- break;
- }
- }
Add Comment
Please, Sign In to add comment