I have a question regarding join(),setPriority() methods. How we can I see the working of these methods?. I could not get the reality in windows OS when i coded in java. Is it due to Windows is preemtive scheduling and due to this it dint behave appropriately? Ideally when join() is invoked on a thread, the current thread which has applied join() on a thread, current thread waits until the thread on which join is performed gets done but this was not the case when i ran the program.
Example : t3.join(); t1.join(); t2.join(); This has to work as mentioned below.
T3 thread completes its process and then T1 gets done and finally it goes with T2. But what i saw is T1, T2 and T3 got done in sequential manner which should not be.
meeta gaur wrote:join() doesn't care about sequence.You have three joins, those will block main thread three times, that should be.
Correct join() does not go with the sequence. It goes with the task completion. So in the above example T1 cannot process until T3 is complete, is this correct?.
The join() method has no effect on the priority -- it merely tells the current thread to wait for another thread. So, with...
Example : t3.join(); t1.join(); t2.join();
The current thread waits for thread t3, then waits for thread t1, then waits for thread t2. And if a thread has already finished when the current thread calls the join() method, then the join() methods return immediately. The join() method has no affect on the completion order. The join() method has no affect on the priority,
Started Thread A
Started Thread B
Started Thread C
Thread B started
Exit from B
Thread A started
Exit from A
Thread C started
Exit from C
End of main thread
My Expectation : Thread C should have done its task first and ThreadA at last but when i ran the program it dint work so.
The thread priority is used as a hint to the operating system to give preference to the higher priority runnable thread -- when it has to choose between runnable threads.
The thread priority has no effect on the starting order of threads. It has no affect on completed threads. It has no affect on threads waiting for I/O, waiting for notifications, joining with other threads, sleeping ... etc.
Some things to try:
1. Make your threads run longer -- much longer. Make it do some mathematical calculation that will take at least a minute.
2. Don't have your threads sleep(), wait(), join(), or do anything that will take it out of the runnable state.
3. Don't have your thread print anything to the screen (or any type of I/O).... except for in the very beginning and end, so you know when they start and finish
4. Make your main thread the highest priority. This will make sure that all the threads will be started at nearly the same time.
And also, if you are on a multi-core machine, then it may still not work -- as a four core machine won't have to choose between your three runnable threads, it will run all three at the same time.