The following Function is executing in its own thread:
private void doSendData()
{
try {
//writeToFile(); // just a temporary location of a c
In order to call wait() on an object, you have to hold the synchronized lock on that object (though the lock is actually released while the thread is waiting):
synchronized (serverAddr) {
serverAddr.wait();
}
I have to admit that why you're wanting to do this baffles me in this case...
Maybe the method you are looking for is Thread.sleep(long)? This method will wait (as in stop the execution of the thread) for the specified time in milliseconds before resuming.
object.wait(long) (which is what you are using) does something entirely different. It waits for another object from another thread to notify it (ie: send it a sort of wakeup message), and will wait at most the specified number of milliseconds. Given the code you posted, I highly doubt this is what you really want.
If Thread.sleep() is not what you want, then you should use the synchronized block as mentioned by the other posters.
To avoid that error message, use the synchronized keyword:
synchronized(serverAddr){
serverAddr.wait(60000);
}
I always cringe when I see this kind of code. Do yourself a favour and have a look at the java.util.concurrent
package.
In general when you have multi-threaded program in Java you need to lock the shared variable by using synchronized (key-word) then in anytime just one thread can access the shared memory.
The above are correct. You can use a synchronized block of code. Or you can create what they call a mutex. A mutex can actually be any object. Lots of people just use Object itself as a mutex. Then you can lock on the mutex. Any threads wanting to get access must wait for thread holding the mutex to release it.
There was also a suggestion by Apocalisp. I would also recommend that you look at the java.util.concurrent package.