# threads - part 2 - CNT 4714 Enterprise Computing Spring...

This preview shows pages 1–6. Sign up to view the full content.

CNT 4714: Threading Part 2 Page 1 Mark Llewellyn © CNT 4714: Enterprise Computing Spring 2011 Programming Multithreaded Applications in Java Part 2 Department of Electrical Engineering and Computer Science University of Central Florida Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cnt4714/spr2011

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CNT 4714: Threading Part 2 Page 3 Mark Llewellyn © //class for threaded prime number testing //no inheritance issues so using the simple form of thread creation class testRange extends Thread { static long possPrime; long from, to; //test range for a thread //constructor //record the number to be tested and the range to be tried testRange(int argFrom, long argpossPrime) { possPrime = argpossPrime; if (argFrom ==0) from = 2; else from = argFrom; to=argFrom+99; } //implementation of run public void run() { for (long i=from; i <= to & iif (possPrime % i == 0) { //i divides possPrime exactly System.out.println("factor " + i + " found by thread " + getName() ); break; //exit for loop immediately } yield(); //suspend thread } } } Prime Number Tester Class

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CNT 4714: Threading Part 2 Page 4 Mark Llewellyn © //driver class to demonstrate threaded prime number tester public class testPrime { public static void main (String s[]) { //number to be tested for primality is entered as a command line argument //examples: 5557 is prime, 6841 is prime, 6842 is not prime long possPrime = Long.parseLong(s[0]); int centuries = (int) (possPrime/100) + 1; for (int i=0; i<centuries;i++) { new testRange(i*100, possPrime).start(); } } } Driver Class for Prime Number Tester This is an example of related but unsynchronized threads . In this case the threads are related since they are each working on a piece of the same data, but approach it from a slightly different perspective. However, they are unsynchronized since they do not share information.
Page 5 Mark Llewellyn © 2048 and 6842 are not prime – their factors are shown by the thread which discovered the factor. 5557 is prime so no

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 07/14/2011 for the course CNT 4714 taught by Professor Llewellyn during the Spring '10 term at University of Central Florida.

### Page1 / 52

threads - part 2 - CNT 4714 Enterprise Computing Spring...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online