File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes displaying static int Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "displaying static int" Watch "displaying static int" New topic
Author

displaying static int

Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
Hi, i am runnning a few threads, each one counts from 1-100,101-200,201-300 etc. each thread looks for prime numbers in their respective range. When one is found, it updates a static int countPrime. I then want to display prime. If I put it in main, it displays before any threads have run and is there for 0. where and how should I display it?
Thanks Ian


- Make it idiot proof and they'll make a better idiot!
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
Do not use a Static int variable. Use a vector, in your thread constructor, pass the vector to your thread, when the thread found a prime number, add it to the Vector (as String) and make the thread print that number to the System.out To be more nice, make your thread constructor method taking a OutputStream and pass the System.out as argument.
In that way, your prime number will be display where ever your want and you will have the prime number in a vector for further use


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
I'm not sure what the real purpose is here - seems like this is a fairly inefficient way to look for primes, so I'll assume this is for educational purposes, and searching for primes is just a token example of "work" for a thread to be occupied with. So, ignoring issues like "why would you use threads for this"...
If you want to display results after all the prime-seeking threads have completed, you need to write a loop that explicitly waits for them all. Let's say you've got all the Threads you're waiting for in an array named "threads":

Here we wait for each thread in turn, using join(). In many cases, the thread may have already completed while we were waiting for another thread - in which case join() will return immediately, and the loop will go on to the next thread. When the loop has completed, we will be certain that all threads in the array have completed.


"I'm not back." - Bill Harding, Twister
 
 
subject: displaying static int
 
Threads others viewed
Help - boolean
problem with java code
Finding a prime number
problem displaying prime no program.
Prime Factor Program
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture