Postingan

Menampilkan postingan dengan label thread

Difference between notify and notifyAll in Java - When and How to use

notify vs notifyAll in Java What is the difference between notify and notifyAll method is one of th e tricky Java questions , w hich is easy to answer but once Interviewer asks follow-up questions, you either got confused or not able to provide clear-cut and to the point answers? The main difference between notify and notifyAll is that notify method will only notify o ne Thread a nd notifyAll method will notify all Threads which are waiting on that monitor or lock. By the way, this is something you have been reading in all over places and to be frank, this statement despite being correct is not complete and its very difficult to understand difference between notify vs notifyAll by just reading this statement. Lot of questions comes in mind like Which thread will be notified if I use notify() ? How do I know how many threads are waiting, so that I can use notifyAll() ? How to call notify()? What are these thread waiting for being notified etc? Actually, discussion of notify

What is Timer and TimerTask in Java � Tutorial Example

Timer in Java is a utility class which is used to schedule tasks for both one time and repeated execution. Timer is similar to alarm facility many people use in mobile phone. Just like you can have one time alarm or repeated alarm, You can use java.util.Timer to schedule one time task or repeated task. In fact we can implement a Reminder utility using Timer in Java and that's what we are going to see in this example of Timer in Java. Two classes java.util.Timer and java.util.TimerTask is used to schedule jobs in Java and forms Timer API. TimerTask is actual task which is executed by Timer . Similar to Thread in Java , TimerTask also implements Runnable interface and ove rrides run method to spe cify task details. This Java tutorial will also highlight difference between Timer and TimerTask class and explains how Timer works in Java. By the way difference between Timer and Thread is also a popular Java questions on fr esher level interviews. Read more �