aspose file tools*
The moose likes Threads and Synchronization and the fly likes Need more information on working of join() and setPriority() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Need more information on working of join() and setPriority()" Watch "Need more information on working of join() and setPriority()" New topic
Author

Need more information on working of join() and setPriority()

Miths Machew
Greenhorn

Joined: Dec 15, 2012
Posts: 3
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.

Any idea why it behaved so?
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

join() doesn't care about sequence.You have three joins, those will block main thread three times, that should be.


My one regret in life is that I am not someone else.
- Woody Allen
Miths Machew
Greenhorn

Joined: Dec 15, 2012
Posts: 3
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?.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Miths Machew wrote:
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,

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Miths Machew
Greenhorn

Joined: Dec 15, 2012
Posts: 3
Thanks. I got myself confused. You made it clear. Now coming to setPriority() method.

It seems to be setPriority() is not working as expected. Is there something wrong in this chunk?



INPUT:

threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority() + 1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Started Thread A");
threadA.start();
System.out.println("Started Thread B");
threadB.start();
System.out.println("Started Thread C");
threadC.start();
System.out.println("End of main thread");

OUTPUT:

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Miths Machew wrote:Thanks. I got myself confused. You made it clear. Now coming to setPriority() method.

It seems to be setPriority() is not working as expected. Is there something wrong in this chunk?



INPUT:

threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority() + 1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Started Thread A");
threadA.start();
System.out.println("Started Thread B");
threadB.start();
System.out.println("Started Thread C");
threadC.start();
System.out.println("End of main thread");

OUTPUT:

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.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need more information on working of join() and setPriority()
 
Similar Threads
Thread's Priority
Thread doubt
Multithreading doubt
Threads and Synchronization examples
How to check whether multiple threads execution is completed or not?