how can i make the system (execution process )wait for some periode of time

Dani AI

Generated

Short expert note and what to fix

The code shared by mixes a few distinct problems. Calling new A() from inside A()'s constructor causes immediate, unbounded recursion and a StackOverflowError. Thread.sleep(...) only pauses the current thread, throws InterruptedException and must be handled — and using it inside a constructor is usually a design smell because it blocks whatever thread is creating the object (it will freeze a UI thread). was right to worry about recursion, and was right to ask where the constructor actually is. ’s mention of wait/notify refers to thread coordination, not simple delayed scheduling.

A minimal, safer constructor pattern

public class A {
    private static int a = 0;

    public A() {
        a++; // increment the counter
        try {
            Thread.sleep(1000); // pause 1 second on the current thread
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // preserve interruption status
        }
        // do NOT call new A() here — that would recurse endlessly
    }
}

Better: schedule work outside the constructor

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(() -> new A(), 0, 1, TimeUnit.SECONDS);
// when finished: scheduler.shutdown();

Use a scheduler or a dedicated background thread for periodic tasks instead of sleeping in constructors.

Quick troubleshooting checklist

  • Replace i++ with the actual variable name (your snippet used an undefined i).
  • Put Thread.sleep inside try/catch and avoid swallowing the exception.
  • Don’t create instances from inside their own constructor — move that loop or scheduling logic elsewhere.
  • wait()/notify() require synchronization and are for coordination; they are not a simpler alternative to scheduling.
  • If on a UI thread, never sleep there — use a timer or background executor.

Fix those points and the code will compile and behave predictably.

Recommended Answers

All 7 Replies

am I missing something? I don't see any constructor in here ...

Sorry - my fault. It was originally posted as a code snippet, I changed it to a Discussion, and the code vanished. But basically it went

class A {
   A() {
      ...
      new A();
   }

ps Sorry akhilchandranms, if you would like to post the code again that would be great.
J


Thanku sir i got it

class A{
static int a=0;
i++;
Thread.sleep(1000);
A(){
new A();

}
}

I think you should try to compile and execute that code before assuming you have finished!

commented: Yes. And we know why. ;-> +6

there are other methods also wait notify notify all etc use that too....

nira: Are you suggesting that OP should use wait notify notify all etc to meet his requirement of "make the system wait for some period of time".
Please make sure your answers really do relate to the OP's actual need.

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.