You don't have to re-thread or any such thing. All you need to do is call.
Up vote 2 down vote favorite share g+ share fb share tw.
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 java multithreading link|improve this question asked Feb 24 at 22:45Ankur39411 100% accept rate.
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 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 at 23:18 I am using 3rd party API's in my program. They are probably creating threads in the program causing the async.
– Ankur Feb 24 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 240 at 0:15.
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.
Sorry I didn't get that! – Ankur Feb 24 at 22:52 2 @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 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()?
– Ankur Feb 24 at 22:57 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 at 22:59 1 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 at 23:04.
Thread. Sleep(milliseconds) is a public static method that will work with single threaded programs as well. Some like the following is a typical pattern: try { // to sleep 10 seconds Thread.
Sleep(10000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } 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.
– Ankur Feb 24 at 22:48 No, you just call sleep whenever you want. No need to use another of the thread primitives. – Gray Feb 24 at 22:50.
Just use:- try { Thread.sleep(); } catch(InterruptedException ex} { } This will make the current thread (e.g. Main thread) sleep.
– Ankur Feb 24 at 22:49 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.
– Strawberry Feb 24 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 at 22:51 2 FYI: It is recommended to call Thread.currentThread().interrupt(); if you catch InterruptedException. See: stackoverflow.com/questions/4906799/… – Gray Feb 24 at 22:52 @Gray: Thanks for that.
I hadn't actually realised that. – Strawberry Feb 24 at 22:53.
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.
FYI: It is recommended to call Thread.currentThread().interrupt(); if you catch InterruptedException. See: stackoverflow.com/questions/4906799/… – Gray Feb 24 at 22:51 @Gray, thanks. Was unaware of that.
Updated answer. – hmjd Feb 24 at 22:53.
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.
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.
Your program is not Multi-threaded...but it is using a thread. Thread. Sleep will still work.
You can use Thread.sleep(); after the calculation without the need to rewrite your whole program with threads!
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.