Scheduling policy of newly started threads

K

kg

Hi,
If a new thread is start()'ed, does it by default become the running
thread (e.g. on a single CPU machine) or is it just in a runnable state
and may or may not be given the cpu immediately?

Thanks!
 
N

nialltimpson

Hey,

From what I know as soon as a thread is created i.e
start(), this will automatically call run(), which will now be an active
thread.

Any use?

Niall

Hi,
If a new thread is start()'ed, does it by default become the running
thread (e.g. on a single CPU machine) or is it just in a runnable state
and may or may not be given the cpu immediately?
 
K

kg

Hi,
So does this mean that when it's run method is called, it is now the
thread that is running on the CPU and the thread that was previously
running is kicked off? (Assuming single CPU environment)

Thanks
 
C

Cram TeXeD

Hi,
So does this mean that when it's run method is called, it is now the
thread that is running on the CPU and the thread that was previously
running is kicked off? (Assuming single CPU environment)

No. It's depending on scheduling and it's undertimistic.
Previously thread still running. In fact they're running both. They're
thread :)
One or 256 CPU don't matter.
 
M

Matt Humphrey

nialltimpson said:
Hey,

From what I know as soon as a thread is created i.e
start(), this will automatically call run(), which will now be an active
thread.

The run method will not necessarily be invoked immedately and the newly
started thread will most likely not immediately become the active thread.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
B

blmblm

No. It's depending on scheduling and it's undertimistic.
Previously thread still running. In fact they're running both. They're
thread :)
One or 256 CPU don't matter.

Well, it matters if you think that "running" means "actually running on
a CPU", i.e., having its instructions executed right now, rather than
"ready to be run if/when a scheduler assigns it to a CPU". It's true
that multithreading / multitasking can make it appear that multiple
threads are running at the same time on a single CPU, but when writing
multithreaded applications it's probably a good idea to realize that
this *is* an illusion .... (I might need to add some caveats about
hyperthreading technology, but I think the basic point I'm making --
which is that the number of threads that are actually running (in my
sense of the word) is limited by the hardware -- works there too.)

You are right, however -- and this is a good point to make -- that
which thread is running (in the sense of actually running) depends
on scheduling decisions, and the Java spec doesn't spell out too
many requirements about how those should be made, presumably to
give implementers some flexibility. And in fact implementations
*do* differ in how they schedule threads -- e.g., whether they use
time-slicing or just run each thread until it ends or blocks.

The point I wanted to make, however, is that the standard advice for
writing multithreaded applications in Java is that you should *NOT*
depend on scheduling decisions to make your program work, but instead
should code so that your program will work on any standard-conforming
implementation. If you need for the thread that was just started to
make some progress before other threads do something else, you should
use explicit synchronization (waits, synchronized methods, whatever)
to make sure that happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

threads and GUIs 18
java thread question 9
Of GUIs threads and scheduling woes 11
Threads vs subprocesses 0
THREADS - SOCKETS 2
Java Threads 2
synchronize threads 6
ASP Thread Scheduling Anomalies 6

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top