PDA

View Full Version : Java threads context switch



tashe
January 23rd, 2009, 01:48 AM
Hi
I have two threads,each from different class. In each of the thread's run method there is a while cycle. I've noticed that when a threads time finishes and the processor switches to the other thread, he does it almost always when the thread is in the middle of the while cycle.
Is it possible to make the context switch at the end of a iteration in the while loop?
thank you

slavik
January 23rd, 2009, 01:58 AM
yes. sometimes compilers put in code in loops that once a while make a system call for house keeping.

myrtle1908
January 23rd, 2009, 01:59 AM
I think you want the Thread.yield() method. Basically it says end my time-slice prematurely, look around for other threads to run. If there is nothing better than me, continue.

Edit: By "better", I mean with a higher priority.

CptPicard
January 23rd, 2009, 02:05 AM
I haven't heard of any way to have absolute explicit control over this, but there is the "yield" method you could try calling at the end of your while block to signal that now would be a great time to give processor to the other thread.

myrtle1908
January 23rd, 2009, 02:09 AM
Further to what has been said previously, the following is an interesting (short) read: http://www.javamex.com/tutorials/threads/yield.shtml, specifically the section regarding how linux deals with calls to yield().

tashe
January 23rd, 2009, 02:22 AM
First, thanks for the answers in such a short time
From what I can read, the CPU will give control to the other thread when called yield, but my bigger concern is how to be sure that when the time slice of the thread is finished, this same thread won't give up the CPU until the last line in the while loop, that is, the yield method.

slavik
January 23rd, 2009, 02:34 AM
you can't be certain. :) there are ways to sort of force this, but I am sure you don't need it at this point in your adventures ;)

but if you really want to know, we can always tell you :)

CptPicard
January 23rd, 2009, 02:39 AM
when the time slice of the thread is finished, this same thread won't give up the CPU until the last line in the while loop, that is, the yield method.

Welcome to the world of pre-emptive multitasking... you're not really allowed to do that. Priorities might help some, but if these are equal-priority threads, I am afraid you're probably SOL...

tashe
January 23rd, 2009, 02:43 AM
If you have the time, feel free :D
And if its not too complex,I'll probably try it on my project. ;)

slavik
January 23rd, 2009, 04:23 AM
basically, if you want to force a thread to run instead of other threads, create a global mutex and give it to the thread, then once it is done, it can release it and then you have to somehow decide who gets it next (free for all, round robin, etc.) :)

jespdj
January 23rd, 2009, 04:05 PM
You should not write multi-threaded programs in a way that depends on how the scheduler schedules your threads. There is no guarantee that your threads are scheduled in a particular order or with a particular timing. On a different operating system with a different implementation of Java it might work differently, or in a next version of Java on Ubuntu it might work differently. It might even work differently on another computer with a dual- or quad-core processor, running the same version of Ubuntu and Java as you.

If you need to coordinate how threads work together, you should use some thread synchronization method. Java has the synchronized keyword, and for more sophisticated thread synchronization mechanisms use classes from the package java.util.concurrent.