I need to know what is the difference between sleep() and wait() methods in Thread Class.
my second question:
How to make two threads shared a block of memory.
Thanks a lot.
Printable View
I need to know what is the difference between sleep() and wait() methods in Thread Class.
my second question:
How to make two threads shared a block of memory.
Thanks a lot.
First of all, when you call Thread.sleep(ms, ns) which btw is a static method, you sleep the currently running thread for the time specified. When using wait() you tell a thread to wait on a certain object. So for instance if you were to synchronize on an object and then call wait, you would tell the thread that it should just hang on and do nothing until someone calls notify on the object you are waiting for. Once you call notify any waiting thread that gets awaken will start right from were it started waiting.
An example of this could be if you have an event system with a shared pool of events to take care of. Lets say you have 3 threads all polling the event queue and popping whatever is on the stack at the moment. What you could then do is check if the stack is empty and if it is you just tell the thread to wait(), after a while all 3 threads will be waiting and not doing anything and whenever there is an event coming in you simply call notify on the queue object which will awake 1 or more threads waiting for the object and they will continue working on it.
I hope that makes any sense.
Over to having several threads share an object or in your case you might be thinking of just a stack of events or a list or whatever. I made a small example to show most of this stuff off. Enjoy!
Code :
import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; public class Testing { public static void main(String... arguments) { // Create our selfs a little queue final Queue<String> eventQueue = new LinkedBlockingQueue<String>(); // Start worker 1 final Thread thread1 = new Thread(new MyWorkerThread(eventQueue)); thread1.start(); // Start worker 2 final Thread thread2 = new Thread(new MyWorkerThread(eventQueue)); thread2.start(); // Start employer final Thread employer = new Thread(new MyEmployerThread(eventQueue)); employer.start(); } private static class MyEmployerThread implements Runnable { private Queue<String> eventQueue; private int counter = 0; public MyEmployerThread(final Queue<String> eventQueue) { this.eventQueue = eventQueue; } @Override public void run() { while (true) { try { synchronized (this.eventQueue) { ++this.counter; this.eventQueue.add("event " + this.counter); this.eventQueue.notify(); } System.out.println("Added event to queue"); Thread.sleep(150); } catch (InterruptedException e) { e.printStackTrace(); } } } } private static class MyWorkerThread implements Runnable { private Queue<String> eventQueue; public MyWorkerThread(final Queue<String> eventQueue) { this.eventQueue = eventQueue; } @Override public void run() { while (true) { synchronized (this.eventQueue) { if (this.eventQueue.isEmpty()) { try { this.eventQueue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } // Carry on executing from this point once we are notified final String eventString = this.eventQueue.poll(); if (eventString != null) { System.out.println("[" + Thread.currentThread().getName() + "] executing " + eventString + " leaving the queue at size [" + this.eventQueue.size() + "]"); } // Lets sleep a little just in case the queue // is endless we might want to give the rest // of the system some CPU time try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
Happy programming!
// Json
Thanks a lot for your effort
Perfect sample....Good Person
if you r using sleep() means you have to stop the thread for a particular time period given in neno seconds and the thread automaticaly resumes after that time but wait() is used to stop a thread for a particular condition you have to resume the thread explicitly.
Its important to note that sleep is actually a method on the Thread class while wait is a method on the Object class. You tell a thread to wait for an object's notify to be called while sleep is just a way of telling the thread to wait for no particular reason.
// Json