threads - part 2 - COP 4610L: Applications in the...

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

View Full Document Right Arrow Icon
COP 4610L: Threading Part 2 Page 1 Mark Llewellyn © COP 4610L: Applications in the Enterprise Fall 2006 Programming Multithreaded Applications in Java Part 2 COP 4610L: Applications in the Enterprise Fall 2006 Programming Multithreaded Applications in Java Part 2 School of Electrical Engineering and Computer Science University of Central Florida Instructor : Mark Llewellyn markl@cs.ucf.edu CSB 242, 823-2790 http://www.cs.ucf.edu/courses/cop4610/fall2006
Background image of page 1

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

View Full DocumentRight Arrow Icon
COP 4610L: 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
COP 4610L: 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<possPrime; 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
COP 4610L: 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
COP 4610L: Threading Part 2 Page 5 Mark Llewellyn © 6841 is prime 2048 and 6842 are not prime – their factors are shown by the thread which discovered the factor.
Background image of page 5

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

View Full DocumentRight Arrow Icon
COP 4610L: Threading Part 2 Page 6 Mark Llewellyn © Related and Synchronized Threads The most complicated type of threaded application involves threads which interact with each other. These are related synchronized threads .
Background image of page 6
Image of page 7
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 COP 4610L taught by Professor Staff during the Spring '09 term at University of Central Florida.

Page1 / 50

threads - part 2 - COP 4610L: Applications in the...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online