Some or all of the numbers 0 through 9 could be printed. There is no guarantee that anything will be printed.
All of the threads started in method B.m1 are daemon threads so the program can run to completion even if some or all of the daemon threads have not run.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Originally posted by cyril vidal:
But if I delete t1.setDeamon(true) in the above code, I would expect that the program will not exit unless all of the threads have run. And that all the threads waiting in the instance b waiting pool will run one after the other.
When I run this program on my computer by command-line, I have instead the following result:
O and a waiting cursor but no more...
Why haven't I relatively quickly 0123456789?
Thanks in advance for your help,
Cyril.
notifyall() will notify all the waiting threads - those that managed to enter the monitor and isssued the wait
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Maybe threads that need to acquire locks are on a different queue from the ready queue.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Thread-2 and Thread-3 don't need the lock because they don't enter an synchronized block
So, in your schema, perhaps one step (Seeking lock) is missing.
notifyAll() has for effect to move all the waiting threads of the wait set of an object to the Seeking Lock state for that object, whereas notify will juste move one of these threads to the Seeking Lock state: the choice of Threads depends on the environment
Try checking this by giving the newly created threads a very high and then by giving a very low priority. When you give high priority then there may not be a single thread waiting and in case of low priority all threads may be waiting.
So the two coding for m1() are :
code:
--------------------------------------------------------------------------------
private void m1(){for (int i = 0; i < 10; i++){A t1 = new A(this);t1.setName(String.valueOf(i));//t1.setDaemon(true); t1.setPriority(10); //this linet1.start();}synchronized (this) {notifyAll();}}
--------------------------------------------------------------------------------
Probably the output will be 0123456789
and for the second code just change this line to t1.setPriority(1); and then probably there will be no output and the application will keep waiting.
2�) When notifyall is executed, then one of the threads waiting in the pool for instance b acquires the lock, and continues executing.
In my case, it's the first one, so I have 0 printed in the console.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Did you mean by this that when the notifyall is executed, depending on thread scheduler' s behavior, all the threads (from 0 to 9) are not in the waiting pool?
1. Threads 0 to 9 started
2. Thread-0 obtains the lock, issue wait, goes into a wait state, and releases the lock
3. Thread-1 obtains the lock, issue wait, goes into a wait state, and releases the lock
4. Thread-main obtains the lock, issue notifyall, exit, and releases the lock.
5. Thread-0 and Thread-1 wakes up
6. Thread-0 obtains the lock, completes the method (prints 0), and releases the lock.
7. One after the other Threads 2 to 9 obtain the lock, issue wait, go into the wait-state, and release the lock.
8. Thread 1 obtains the lock, completes the method(prints 1), and releases the lock.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
1. Threads 0 to 9 started
2. Thread-0 obtains the lock, issue wait, goes into a wait state, and releases the lock
3. Thread-1 obtains the lock, issue wait, goes into a wait state, and releases the lock
4. Thread-main obtains the lock, issue notifyall, exit, and releases the lock.
5. Thread-0 and Thread-1 wakes up
6. Thread-0 obtains the lock, completes the method (prints 0), and releases the lock.
7. One after the other Threads 2 to 9 obtain the lock, issue wait, go into the wait-state, and release the lock.
8. Thread 1 obtains the lock, completes the method(prints 1), and releases the lock.
Why would Thread-0 and Thread-1 again obtain the lock and then print. Once out of the wait state they do not need to again accquire a lock.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Originally posted by Anupam Sinha:
Hi Alton
Why would Thread-0 and Thread-1 again obtain the lock and then print. Once out of the wait state they do not need to again accquire a lock.