Results 1 to 13 of 13
- 10-28-2011, 04:09 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Making sure there is a delay between 2 threads
Hi,
i need to make sure that a thread is only running after a certain time of another.
I have a list of class instances like MyTheadList(ThreadcClass).
In the class there is a method called startThread looking like this:
there is another static synchronized Method "updateThread" to make sure each Thread is only running after a certain timespan from another, i do it like this:Java Code:public void startThread() { scheduler = Executors.newScheduledThreadPool( 1 ); scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { updateStuff(scheduler); System.out.println("UPDATED"); } }, 0, 50000, // run this every xx milliseconds TimeUnit.MILLISECONDS ); }
This is working, but with this code i (obviously) get a 100% cpu utilization from the while loop. So i would like to have something that doesn't.Java Code:static synchronized void updateStuff() { long time = new Date().getTime(); long actualTime = new Date().getTime(); while(actualTime < time + 5000) // wait 5 seconds before doing something actualTime = new Date().getTime(); ................. // doing work ................. }
- 10-28-2011, 04:16 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Making sure there is a delay between 2 threads
Use the Thread.sleep() method, or use a timer and wait/notify calls
- 10-28-2011, 04:47 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Re: Making sure there is a delay between 2 threads
Thanks for the answer - used Thread.sleep() :)
- 10-30-2011, 05:20 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Re: Making sure there is a delay between 2 threads
I now have another problem, i changed my updateStuff method to this:
The problem comes up when i want to kill threads. Say i want to kill all running threads, i would then call either scheduler.shutdown() or scheduler.shutdownNow() for each object in my List. If i kill them with scheduler.shutdown() all Threads will execute their last run of updateStuff, meaning if i eg have 20 Threads the last will ending in 20*5 seconds. If i cancel them with shutdownNow() all Threads will end immediately but this will also cancel the one thread that is currently "doing work". What i need would be something that only does shutdownNow() on all the Threads that currently aren't in the updateStuff method and shutdown() for the one that is.Java Code:static synchronized void updateStuff() { try { Thread.sleep(5000); System.out.println("UPDATED"); ................... // doing work ................... } catch (InterruptedException e) { System.out.println("Thread interrupted!"); Thread.currentThread().interrupt(); } }Last edited by AmFreak; 10-30-2011 at 05:35 PM.
- 10-30-2011, 07:49 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Re: Making sure there is a delay between 2 threads
Ok, doing it like this now:
Java Code:public void close() { scheduler.shutdownNow(); }It works but beeing new to this stuff i would feel better if someone with knowing of Threading Stuff could comment on this.Java Code:static synchronized void updateStuff() { boolean working = false; try { Thread.sleep(5000); working = true; Thread.sleep(5000); // Only for testing to catch the working part } catch (InterruptedException e) { System.out.println("Thread Interrupted!"); if(working != true) { Thread.currentThread().interrupt(); return; } } System.out.println("WORKING"); ............. // doing work ............. }
- 10-30-2011, 08:13 PM #6
Re: Making sure there is a delay between 2 threads
I'm not any better then you are at threads(i'm probably a lot worse), but at line 9 it should be if(!working) instead of if(working != true)
- 10-30-2011, 10:00 PM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Making sure there is a delay between 2 threads
Did you try this and test the result of each? what makes you think they are different? I ask because these two expressions are identical aside from syntaxif(!working) instead of if(working != true)
What do you want a comment on? Does the code work? Its an open ended question that I personally don't even know how to answerif someone with knowing of Threading Stuff could comment on this.
- 10-30-2011, 10:15 PM #8
Re: Making sure there is a delay between 2 threads
Yeah I have not on this but other programs. if(working != true) has a higher chance of failing.Did you try this and test the result of each? what makes you think they are different? I ask because these two expressions are identical aside from syntax
- 10-31-2011, 01:54 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Re: Making sure there is a delay between 2 threads
Thought so too.
Ya it works, but i actually find it a little weird that the execution of the code isn't canceled if the thread is "working" (aka past the catch block).What do you want a comment on? Does the code work? Its an open ended question that I personally don't even know how to answer
@ jMaster
Hmm i thought they were the same, and at first thought u were being a smartass when you mentioned itYeah I have not on this but other programs. if(working != true) has a higher chance of failing.
, but now that u have made it clear that u are not, could you give an example?
- 10-31-2011, 02:57 AM #10
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Making sure there is a delay between 2 threads
This information is based upon what? Do you have evidence to back up that it 'fails'? Try it for yourself. Write a small program to prove (or suggest) that if 'fails'.
Originally Posted by jMaster Last edited by doWhile; 10-31-2011 at 03:00 AM.
- 10-31-2011, 03:43 AM #11
- 10-31-2011, 06:44 PM #12
Re: Making sure there is a delay between 2 threads
Okay cool I found this out a few months ago when my friend made a program that worked perfectly on his home computer, but when he tried the same program at school it didn't work. It compiled fine but the outputs where total rubbish, so I looked around and changed the ifs so that they said if(whateverBoolean) instead of if(whateverBoolean== true) and it worked fine. So I assumed the same would be true for !true(!= true) or false(== false). Since then i've only used my way but I think its good if this goes for some good testing I wont mind being proven wrong but I was just sharing my experience. I'm looking, maybe i'll find the question that this happened its unlikely because there are so many programs though.
- 10-31-2011, 08:53 PM #13
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Making sure there is a delay between 2 threads
Its not about being proven wrong, its about learning. This situation is a great opportunity to write an SSCCE to test your assumptions (in fact, the SSCCE is so important, I would say you're original situation could have greatly benefited from - if you don't know what an SSCCE is, google it). When things go wrong with a program, there is always a logical reason...writing an SSCCE strips away a lot of unnecessary code and boils down assumptions to concrete logical facts.
Similar Threads
-
What type of delay will work for me?
By keo in forum New To JavaReplies: 8Last Post: 05-07-2011, 08:15 AM -
Read URL with delay
By Eviler in forum Advanced JavaReplies: 5Last Post: 04-04-2011, 07:16 AM -
Quick question about for looping and delay
By DouboC@gmail.com in forum JDBCReplies: 0Last Post: 12-29-2010, 09:00 PM -
how to give delay?
By shaluchandran in forum New To JavaReplies: 10Last Post: 12-17-2008, 05:57 PM -
Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07-26-2008, 04:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks