5 Replies - 89300 Views - Last Post: 10 July 2008 - 07:41 AM Rate Topic: -----

#1 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

In Java how to delay something?

Posted 08 July 2008 - 09:11 PM

In Java how to delay something?

In C++, we have PAUSE function

Is it in Java we got SLEEP function?

Thank
Is This A Good Question/Topic? 0
  • +

Replies To: In Java how to delay something?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: In Java how to delay something?

Posted 08 July 2008 - 09:15 PM

View Postfsloke, on 8 Jul, 2008 - 09:11 PM, said:

In Java how to delay something?

In C++, we have PAUSE function

Is it in Java we got SLEEP function?

Thank

Yeah...

try {
  Thread.sleep(1000L);	  // one second
}
catch (Exception e) {}	   // this never happen... nobody check for it


Was This Post Helpful? 0
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: In Java how to delay something?

Posted 09 July 2008 - 06:51 AM

View Postpbl, on 8 Jul, 2008 - 09:15 PM, said:

View Postfsloke, on 8 Jul, 2008 - 09:11 PM, said:

In Java how to delay something?

In C++, we have PAUSE function

Is it in Java we got SLEEP function?

Thank

Yeah...

try {
  Thread.sleep(1000L);	  // one second
}
catch (Exception e) {}	   // this never happen... nobody check for it


I think he could also use timers.
Was This Post Helpful? 0
  • +
  • -

#4 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: In Java how to delay something?

Posted 09 July 2008 - 07:40 AM

I plan to use schedule is better.

So my application not for looping and delay continuously...

Schedule will call the function according to the time that we set.

How to set a scheduler?
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: In Java how to delay something?

Posted 09 July 2008 - 08:52 AM

View Postpbl, on 9 Jul, 2008 - 06:15 AM, said:

try {
  Thread.sleep(1000L);	  // one second
}
catch (Exception e) {}	   // this never happen... nobody check for it



WTH? Never say never!
Yes, you can usually get away with swallowing that exception, especially in the beginning. However it is usually bad practice. I don't like to quote whole articles, so here you are, and just a short abstract to catch your attention:

Quote

Many Java™ language methods, such as Thread.sleep() and Object.wait(), throw InterruptedException. You can't ignore it because it's a checked exception, but what should you do with it? In this month's Java theory and practice, concurrency expert Brian Goetz explains what InterruptedException means, why it is thrown, and what you should do when you catch one.

This story is probably familiar: You're writing a test program and you need to pause for some amount of time, so you call Thread.sleep(). But then the compiler or IDE balks that you haven't dealt with the checked InterruptedException. What is InterruptedException, and why do you have to deal with it?

The most common response to InterruptedException is to swallow it -- catch it and do nothing (or perhaps log it, which isn't any better) -- as we'll see later in Listing 4. Unfortunately, this approach throws away important information about the fact that an interrupt occurred, which could compromise the application's ability to cancel activities or shut down in a timely manner.


Any similarities?
Was This Post Helpful? 0
  • +
  • -

#6 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: In Java how to delay something?

Posted 10 July 2008 - 07:41 AM

Thank a lot guys...

Happy to talk to your all...

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1