How can I add a sleep(1000) to this code?
I've tried but I can't get it to work.
(This program is supposed to be an infinite loop)

public class ExtendedThread extends Thread {
 
 
public ExtendedThread(String name) throws InterruptedException {
super(name);
}
 
 
 
public void run() { 
 
for (int i = 1; i >= 1; i++) {
System.out.println("Thread " + getName() + i);
}
} 
 
public static void main (String[] args)throws InterruptedException {
int numThreads = Integer.parseInt(args[0]);
ExtendedThread[] threads = new ExtendedThread[numThreads];
 
for (int i = 1; i <= (numThreads + 1); i++) {
threads[i] = new ExtendedThread (i + " - Count ");
threads[i].start();
 
} 
}
}

Recommended Answers

All 2 Replies

public void run() { 
 
for (int i = 1; i >= 1; i++) {
System.out.println("Thread " + getName() + i);
}

What is the for loop supposed to do? For all I can see, it gets executed only once. Is this what you wanted? Maybe an infinite while loop would be more like it:

while(true)
{
    System.out.println("Thread: " + getName());
    Thread.sleep(1000);
}

yes ,if infinite loop ,that code
while(true){
}

commented: Pointles on comenting somebody else corect solution. Please use full sentences next time. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.