How to make threads share CPU (almost) equally?

From: TheOne (sudarshan.lamkhede_at_gmail.com)
Date: 03/10/05


Date: 9 Mar 2005 20:20:41 -0800

I am developing a multithreaded application. I have a main thread
(default java main thread)
which creates and starts n number of new "worker threads" whose
priority is set to MAX_PRIORITY at the
time of creation. It then calls join() on each of these threads.

The run method of these threads is something like this:
(I am giving steps here instead of actual code for space reasons)

1. //get some work to do

2. //request data over the network(IO bound operation)

3. //Do some calculations on the data (CPU bound operation)

4. //update some shared data structures

Now I want the IO operations(2) of one thread going simultaneouly
with calculations(3) of another thread (thats the whole purpose of
multithreading !!!)
But it does not happen. The first thread created (thread-0) does not
give up
CPU.

I tried lowering current thread's priority to MIN_PRIORITY before
it enters step(2) and reverting it back to MAX_PRIORITY before step(3)
but it was of no use too.

One thing I did not try (and would not like to write code for) is to
make
the currently running thread sleep for a brief amount of time between
each IO request in
step(2). I feel that it is not a proper way of making threads share CPU
equally.
Am I right?

Do you have some good suggestions on how to make ensure that one thread
does not
monopolize CPU time even when it is waiting for IO ?

I am using Java 1.5 on Windows XP platform.



Relevant Pages

  • Re: How to make threads share CPU (almost) equally?
    ... > time of creation. ... > I tried lowering current thread's priority to MIN_PRIORITY before ... I feel that it is not a proper way of making threads share CPU ... > I am using Java 1.5 on Windows XP platform. ...
    (comp.lang.java.programmer)
  • Re: How to make threads share CPU (almost) equally?
    ... > priority is set to MAX_PRIORITY at the ... It doesn't make your computer any faster, you'll just be stealing cpu ... > multithreading!!!) ... > the currently running thread sleep for a brief amount of time between ...
    (comp.lang.java.programmer)
  • Re: Background process only taking idle time, how to do it?
    ... It forces the thread to give up CPU time if other programs ... compuation and memory). ... The short answer is that you probably want to lower the priority of your process, rather than the priority of your thread; unfortunately, I don't think you can do this in Java without resorting to JNI or native calls, and thus making your program OS dependent. ...
    (comp.lang.java.programmer)
  • Re: multi-core software
    ... Threads have been part of Java since Day 1. ... multithreading. ... processor or a 256 CPU machine (We got to run our Athena Integer Java ... concern yourself about the difference between a uniprocessor and a 256 CPU ...
    (comp.lang.python)
  • Re: Why RosAsm Breaks on a large number of symbols
    ... I wonder why you started talking about multithreading etc, ... Would you write a soundcard driver, network interface card driver, ... >> and your priority hack was... ...
    (alt.lang.asm)