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?

December 15th, 2012, 08:46 PM

Norm

Re: Multithreading

Can you post a small program that compiles, executes and shows what you are asking about.

December 15th, 2012, 10:38 PM

Miths

Re: Multithreading

Just now realized the working of join() method. But still setPriority() is a bit confusing me.

ThreadC has got maximum priority which is 10, so it has to be processed at first. [ threadC.setPriority(Thread.MAX_PRIORITY); ]
ThreadB has priority which is in between ThreadA and threadC. [ threadB.setPriority(threadA.getPriority() + 1); ]
ThreadA is of least priority, so i expect it has to be processed at last. [ threadA.setPriority(Thread.MIN_PRIORITY); ]

December 16th, 2012, 09:09 AM

Norm

Re: Multithreading

Quote:

ThreadC has got maximum priority which is 10, so it has to be processed at first.

ThreadA is started first and finishes immediately. ThreadC has not been started yet.

You need the threads to do something that takes longer so that their priority can have an effect.

December 16th, 2012, 09:23 AM

Miths

Re: Multithreading

Just added a for loop for each thread so that it performs some action for a while.

But in this case also i see that Thread A got displayed 100 times and Thread B has displayed 100 times followed by "End of main thread" and at last displayed Thread C 100 times. Why the threads are not executing as per priority.

December 16th, 2012, 10:07 AM

Norm

Re: Multithreading

Try adding some calls to yield() or sleep() inside the threads.
The tight loops in the code don't give the OS time to get control and pass control to another thread.

December 17th, 2012, 02:46 AM

lina20

Re: Multithreading

you should balance your codes....if you are placing 100 lines in class A...you should place 100 lines in class b and c to compare the results....with 1 line of coding to execute it is difficult to check for priority...

try to insert these lines for the loop in class b and c as well...im sure the code is working....but the processing time is too quick to have a good comparative result....