View Full Version : Confused over Thread.sleep()

04-06-2012, 02:01 PM
I've just written a couple of classes from what I'm reading and I'm not entirely sure what the purpose of Thread.sleep() is in this program.
I know it pauses the program and throws an exception if disturbed, but I can't get my head around what it's actually achieving.

Could someone enlighten me please. Thank you.

Main class with sleep()

public class PrimeThreads {
public static void main(String[] args) {
PrimeThreads pt = new PrimeThreads(args);

public PrimeThreads(String[] args) {
PrimeFinder[] finder = new PrimeFinder[args.length];
for (int i = 0; i < args.length; i++) {
try {
long count = Long.parseLong(args[i]);
finder[i] = new PrimeFinder(count);
System.out.println("Looking for prime " + count);
} catch (NumberFormatException nfe) {
System.out.println("Error: " + nfe.getMessage());
boolean complete = false;
while (!complete) {
complete = true;
for (int j = 0; j < finder.length; j++) {
if (finder[j] == null) continue;
if (!finder[j].finished) {
complete = false;
} else {
finder[j] = null;
try {
} catch (InterruptedException ie) {
// Do nothing
private void displayResult(PrimeFinder finder) {
System.out.println("Prime " + finder.target
+ " is " + finder.prime);

Thread class performing calculation:

public class PrimeFinder implements Runnable {
public long target;
public long prime;
public boolean finished = false;
private Thread runner;

PrimeFinder(long inTarget) {
target = inTarget;
if (runner == null) {
runner = new Thread(this);

public void run() {
long numPrimes = 0;
long candidate = 2;
while (numPrimes < target) {
if (isPrime(candidate)) {
prime = candidate;
finished = true;

boolean isPrime(long checkNumber) {
double root = Math.sqrt(checkNumber);
for (int i = 2; i <= root; i++) {
if (checkNumber % i == 0)
return false;
return true;

04-06-2012, 04:10 PM
First of all the Thread.sleep(1000) command forces the main process which is the one generated from the execution of PrimeThreads to wait (sleep) for 1 second (1000 milliseconds). If another thread tries to wake him up before the sleep time is over it will throw an InterruptedException.

However in your case, there does not seem to be another thread (at least visible in the code you posted) so this exception won't be thrown.

That is the main purpose of the sleep in general.

In your case I cannot see any usage for it. In fact we use the sleep method whenever we need the main thread to wait for another operation to be completed or to give a small wait time between two sequential operations. In the code you posted, the sleep is last command so I think it is useless.

I got other comments for your code:

the while loop "while(!complete) only runs once because you are setting the complete to true in the first line of the while block. So it is a one iteration loop. An if statement would have been enought

if(!complete) {

complete = true;

etc.. etc.. etc..


Hope this was clear.

04-06-2012, 05:32 PM
Thank you for your response.

This is actually straight out the book I'm reading 'Sams Teach Yourself Java 6'. And it does only consist of the two classes above.

I 'sort of' got the point of Thread.sleep(), I just didn't get what it was doing here! I too thought being the last command that it's doing nothing, but I'm only starting out so thought I'd check.

Thanks again.

04-06-2012, 05:34 PM
I'll have to have a look into that while loop too. Cheers :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum