I need timer that is going to make pause in running of main thread.

Like Thread.sleep() does,just I don't want to create Exceptions like this does.

So I want to use Timer class. "java.util.Timer;" this one.


I want to make class and to use her constructor where I need pause,like this:

some code;

//I need pause of 10 secunds hier

MakePauseClass x= new MakePauseClass(10);

some code;

I guess I can tweak this code to achieve this:

I guess I should make different void run() method...

import java.util.Timer;
import java.util.TimerTask;

public class MakePauseClass{
    Timer timer;

    public MakePauseClass(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask1(), seconds*1000);
	}

    class RemindTask1 extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");
            
            timer.cancel(); //Terminate the timer thread
        }
    }
          
}

Is the whole idea to avoid having a try/catch? In which case you can just put those around a wait() in a MakePause class and handle the exceptions there. Timers will start another task after a certain time, but not do the pause you want.

Is the whole idea to avoid having a try/catch?

The whole idea is to have simple one line code that make pause of int seconds without Exception so it could be used anywhere in code where I want.

Timers will start another task after a certain time, but not do the pause you want.

So,how to accomplish then what I want?

Like I said. In your MakePause class just do a wait, surround it in try/catch clauses, and ignore any exceptions.

Can you write that down in code,please?

I dont know that wait() method???

public class MakePauseClass 
MakePauseClass(int pauseLongSeconds)
{

}

You try. That's how you learn.

here it is

public class MakePause {



public MakePause(int pauseLong) throws Exception {

	try 

	{

		wait(pauseLong);
	}

	catch (Exception) {}
	}
}

That's almost right. You don't need or want the "throws Exception" because you have trapped the exception within the method, so it won't be seen by the caller, which is what you wanted.

how to implement that wait() method?

OP:

The idea of the try catch is that if an Exception occurs, you are prepared to deal with it. The catch part deals with the Exception. The try catch construct does not throw an Exception, nor do you want to intentionally throw an Exception here.

how to implement that wait() method?

You don't have to, it's part of the Thread class

Did you mean on Thread.sleep(1000*seconds); ?

Is there another one?

sleep is fine, wait is inherited from Object and can also be used.

wait(1000*seconds); just doesn't work in my eclipse

:) :(

Doesn't work as in: runs but no delay is apparent; throws and exception; or won't compile? Specifics please!

... but if it's an illegal monitor state exception, you can enclose the wait in a synchronized(this) { ... } block, or use Thread.sleep(1000*seconds); instead.

Doesn't work as in: runs but no delay is apparent;

this one is the case !!

Just use Thread.sleep(). As a person who has gotten upset when I couldn't figure something out in the past, I'll tell you the truth: that there are some situations when that frustration and effort to learn is warranted & pays off, and there are some situations, like this one, where you should probably just use the obvious workaround. Also, be aware that Thread.sleep takes it's arguments in milliseconds, and wait probably does too, so if you gave it a low value, you probably wouldn't notice anything - 1000 milliseconds = 1 second. Most people would not notice any delay below 300-500ish milliseconds.

:)

If you still have code that looks like

try {
   ... sleep/wait or whatever
} catch(Exception e) {
}

then you are flying blind. If you get an exception you need to do an e.printStackTrace(); in the catch so you know what's happened.
Assuming there are really no exceptions, put a System.out.println(new Date()); before and after the pause so you can be certain what's happening

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.