JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label wait(). Show all posts
Showing posts with label wait(). Show all posts

Difference between sleep and wait in java

7:59 AM
sleep It causes current executing thread to sleep for specific amount of time. Its accuracy depends on system timers and schedulers. It keeps the monitors it has acquired, so if it is called from synchronized context, no other thread can enter that block or method. If we call interrupt() method , it will wake up the sleeping thread. 1 2 3 4 5 6 7 8        synchronized ( lockedObject ) {           Thread . sleep ( 1000 ) ; // It does not release the lock on lockedObject.        // So either after 1000 miliseconds, current thread will wake up, or after we call        //t. interrupt() method.           }   wait It causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object It must be called from synchronized context i.e. from block or method.It means before wait() method is called,current thread must have lock on that object. It releases lo

Explain wait(), notify() and notifyAll() in Java Thread?

1:08 AM
If you ask me one concept in Java which is so obvious yet most misunderstood, I would say the wait(), notify() and notifyAll() methods. They are quite obvious because they are the one of the three methods of total 9 methods from java.lang.Object but if you ask when to use the wait(), notify() and notfiyAll() in Java, not many Java developer can answer with surety. The number will go down dramatically if you ask them to solve the producer-consumer problem using wait() and notify(). Many will use if block instead of while loop, many others will get confused on which object they should call wait() and notify()method? Some of them even succeed in creating livelock, deadlock, and other multithreading issues. That's why it's become very important to know as much as possible about these three methods. In this article, I am going to share some practical tips and points about wait(), notify() and notifyAll() in Java. Two books, which helped me a lot while understanding this core c

.