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:
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 );
}
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:
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
.................
}
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.
Re: Making sure there is a delay between 2 threads
Use the Thread.sleep() method, or use a timer and wait/notify calls
Re: Making sure there is a delay between 2 threads
Thanks for the answer - used Thread.sleep() :)
Re: Making sure there is a delay between 2 threads
I now have another problem, i changed my updateStuff method to this:
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();
}
}
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.
Re: Making sure there is a delay between 2 threads
Ok, doing it like this now:
Code:
public void close()
{
scheduler.shutdownNow();
}
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
.............
}
It works but beeing new to this stuff i would feel better if someone with knowing of Threading Stuff could comment on this.
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)
Re: Making sure there is a delay between 2 threads
Quote:
if(!working) instead of if(working != true)
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
Quote:
if someone with knowing of Threading Stuff could comment on this.
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
Re: Making sure there is a delay between 2 threads
Quote:
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
Yeah I have not on this but other programs. if(working != true) has a higher chance of failing.
Re: Making sure there is a delay between 2 threads
Quote:
Originally Posted by
doWhile
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
Thought so too.
Quote:
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
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).
@ jMaster
Quote:
Yeah I have not on this but other programs. if(working != true) has a higher chance of failing.
Hmm i thought they were the same, and at first thought u were being a smartass when you mentioned it :(blush):, but now that u have made it clear that u are not, could you give an example?
Re: Making sure there is a delay between 2 threads
Quote:
Originally Posted by jMaster
if(working != true) has a higher chance of failing.
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'.
Re: Making sure there is a delay between 2 threads
Quote:
Originally Posted by
jMaster
if(working != true) has a higher chance of failing.
That one is going straight to the pool room.
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.
Re: Making sure there is a delay between 2 threads
Quote:
Originally Posted by
jMaster
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.
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.