Threading in Java: How to lock an object?

前端 未结 7 710
长发绾君心
长发绾君心 2020-12-29 03:33

The following Function is executing in its own thread:

private void doSendData()
{
    try {

           //writeToFile(); // just a temporary location of a c         


        
7条回答
  •  情歌与酒
    2020-12-29 04:22

    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.

提交回复
热议问题