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

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

View Full Document Right Arrow Icon
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 markl@cs.ucf.edu HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cnt4714/spr2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
CNT 4714: Threading Part 2 Page 2 Mark Llewellyn © Threads In the previous section of notes the thread examples all involved threads which were unsynchronized. None of the threads actually needed to communicate with one another and they did not require access to a shared object. The threads we’ve seen so far fall into the category of unrelated threads . These are threads which do different tasks and do not interact with one another. A slightly more complex form of threading involves threads which are related but unsynchronized . In this case, multiple threads operate on different pieces of the same data structure. An example of this type of threading is illustrated on the next page with a threaded program to determine if a number is prime.
Background image of page 2
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 & i if (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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online