13

I have a java program that does some calculations and then uploads the results to a MYSQL database (hosted in another computer in the same network). I sometimes face the problem that the program does calculations faster than it uploads the result. Therefore it is not able to upload all the results. The program currently is not threaded.

Is there a way I can make the program sleep for a few milliseconds after it has done the calculations so that the upload takes place properly. (Like in other languages sleep or wait function)

I can thread the program but that will be too much rewriting. Is there an easier way?

Thanks

Gray
  • 115,027
  • 24
  • 293
  • 354
Ank
  • 6,040
  • 22
  • 67
  • 100
  • 4
    I am curious as to how a single threaded program can do one thing faster than the other and somehow have it fail due to timing issues. If it is single threaded than these operations are sequential. Are you using a library or something to upload that works in a different thread or something? If it is truly single threaded this scenario doesn't actually make sense. – Robin Feb 24 '12 at 23:11
  • @Robin: I think the only way it's possible is if Ankur's app is registering with or performing some sort of async process. A single-threaded app could be in a single thread of logic that is interrupted by some async event. Still, Sleep should not be first idea to try and fix the issue. – Paul Sasik Feb 24 '12 at 23:18
  • I am using 3rd party API's in my program. They are probably creating threads in the program causing the async. – Ank Feb 24 '12 at 23:59
  • If you just pasted your code we could likely see what the real problem is. I somehow doubt that you're doing any asynchronous stuff related to your usage of jdbc. What other third party apis are you using? – Blake Pettersson Feb 25 '12 at 00:15

8 Answers8

11

Is there a way I can make the program sleep for a few milliseconds after it has done the calculations so that the upload takes place properly. (Like in other languages sleep or wait function)

Thread.sleep(milliseconds) is a public static method that will work with single threaded programs as well. Something like the following is a typical pattern:

try {
    // to sleep 10 seconds
    Thread.sleep(10000);
} catch (InterruptedException e) {
    // recommended because catching InterruptedException clears interrupt flag
    Thread.currentThread().interrupt();
    // you probably want to quit if the thread is interrupted
    return;
}

There is no need to implement Runnable or do anything else with thread calls. You can just call it anytime to put a pause in some code.

Gray
  • 115,027
  • 24
  • 293
  • 354
9

You don't have to re-thread or any such thing. All you need to do is call:

Thread.Sleep(5000); // pause the app for 5 seconds

Every application is also a thread, in your case also called a single-threaded app. You can use a threading API like Sleep w/o any other code or refactoring.

Big note of caution though: If you need to use Thread.Sleep to manage your control flow then there's probably something going wrong architecturally. From your OP I am concerned than in what you describe as a single-threaded app you seem to have one operation "outpacing" another. This should not be possible, unless you're receiving asynch events from elsewhere.

Another caveat: Sleep takes a millisecond parameter that's usually arbitrary and just means "wait a little while." The problem is that a "little while" may be OK today but tomorrow your machine will be under greater load and "a little while" will no longer be good enough, your Sleep will lapse and the same error will appear. Sure, you can set the time out to "a long while" but then you'll be waiting "a long while" for each and every transaction... Catch 22.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • What do u mean by there's probably something going wrong architecturally? Sorry I didn't get that!! – Ank Feb 24 '12 at 22:52
  • 3
    @Ankur: See my recent edit. Thread.Sleep is usually a hack to fix a timing issue. If the code is all your own, then the timing issue will have been of your own creation. I.e. Sleep is OK to hack around 3rd party APIs and services you have no control over, but if you own the code, don't rush to using it too quickly. – Paul Sasik Feb 24 '12 at 22:54
  • I do own the code but I'm using MySQL API and another third party API in it. Do you recommend Thread.Sleep()? – Ank Feb 24 '12 at 22:57
  • 1
    So you don't own (didn't create) ALL the code. You're dependent on other's code. Still, I can't recommend Sleep w/o seeing more of the issue. I suggest you try to figure out why you're going out of synch before using Sleep. – Paul Sasik Feb 24 '12 at 22:59
  • Hard to say without seeing your code, but you're probably doing something very wrong. – Blake Pettersson Feb 24 '12 at 22:59
  • Most of the times the program writes everything to the database but lately I've noticed that the program randomly omits some of the enteries while writing to the database. I've checked my Database for issues and its all okay. I'm kinda sure that this is exactly whats happening. – Ank Feb 24 '12 at 23:00
  • 2
    If I were you, I would do this: Take a base line sample of issues. e.g. How many missed entries are there today, tomorrow? Then throw in Sleep(5000) and record the # of incidents. Evaluate the difference. If it's significant, then try and resolve the issue with logging and other troubleshooting, don't use Sleep as a crutch. If the difference is small, there are other problems besides timing. – Paul Sasik Feb 24 '12 at 23:04
  • `randomly omits` is not a good sign. are you *sure* it is random? like others, i find it difficult to believe that a single threaded program has a race condition! – John Gardner Feb 24 '12 at 23:12
3

Use Thread.sleep():

try
{
    Thread.sleep(1000); // Sleep for one second
}
catch (InterruptedException e)
{
    Thread.currentThread().interrupt();
}

This does not introduce a new Thread into the program or require any other Thread related machinery.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • FYI: It is recommended to call Thread.currentThread().interrupt(); if you catch `InterruptedException`. See: http://stackoverflow.com/questions/4906799/why-invoke-thread-currentthread-interrupt-when-catch-any-interruptexception – Gray Feb 24 '12 at 22:51
1

Just use:-

try
{
Thread.sleep(<time in ms>);
}
catch(InterruptedException ex}
{
}

This will make the current thread (e.g. main thread) sleep.

Martin
  • 7,089
  • 3
  • 28
  • 43
  • Do I implement Runnable in my main class? – Ank Feb 24 '12 at 22:49
  • 1
    No there is no need to implement Runnable. The above code is calling a static method to sleep for a while, which happens to be in the Thread class. This is not actually threading your code. – Martin Feb 24 '12 at 22:50
  • 1
    @Ankur No, Thread.sleep() is just a regular static method, it's rather unfortunately named since it has nothing to do with threading. – Joachim Isaksson Feb 24 '12 at 22:51
  • 2
    FYI: It is recommended to call Thread.currentThread().interrupt(); if you catch `InterruptedException`. See: http://stackoverflow.com/questions/4906799/why-invoke-thread-currentthread-interrupt-when-catch-any-interruptexception – Gray Feb 24 '12 at 22:52
  • @Gray: Thanks for that. I hadn't actually realised that. – Martin Feb 24 '12 at 22:53
0

The static method sleep() in the java.lang.Thread class pauses the current thread -- which could just be the main thread -- when called. You don't need to do anything special to use it.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

Every Java application executes in a thread in the JVM. Calling the static method Thread.sleep will cause your application's thread (the one that is running) to stop

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
0

Your program is not Multi-threaded...but it is using a thread. Thread.Sleep will still work.

Bradley Mountford
  • 8,195
  • 4
  • 41
  • 41
0

You can use Thread.sleep(); after the calculation without the need to rewrite your whole program with threads!

foch
  • 1,039
  • 10
  • 21