Cost of Java thread creation

C

Christian

Hi all,

does anyone have some data on how much time it costs on
Windows/Solaris to create a new thread in Java? If possible compared
to thread pool performance.

We're discussing about using a thread pool or not. Everywhere it's
stated that thread creation is very resource consuming, but so far I
haven't found any data on that.

Thx
Christian
 
N

NOBODY

about 17 to 1 on my w2k jdk1.4.2_03

//-----------

package test;

public class SimpleThreadcost {

public static void main(String[] args) throws Exception {
test();
test();
test();
}

static void test() throws Exception {
Runnable r = new Runnable() {
public void run() {
}
};

int N = 5000;
long t1, t2, d;

//------
t1 = System.currentTimeMillis();
T1 thr = new T1();
thr.start();
for (int i = 0; i < N; i++) {
synchronized(r) {
thr.setJob(r);
r.wait();
}
}

t2 = System.currentTimeMillis();
d = t2 - t1;
System.out.println("test1: "+d+" ms ("+(1000.0*d/N)+" us
avg)");
thr.interrupt();

//-----

t1 = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
synchronized(r) {
final Runnable fr = r;
Thread t = new Thread() {
public void run() {
fr.run();
synchronized(fr) {
fr.notify();
}
}
};

t.start();
r.wait();
}
}

t2 = System.currentTimeMillis();
d = t2 - t1;
System.out.println("test2: "+d+" ms ("+(1000.0*d/N)+" us
avg)");


}

//======================================

static class T1 extends Thread {
final Object job_lock = "";
Runnable job;

public final void run() {
synchronized(job_lock) {
try {
while(true) {
if(job!=null) {
try {
job.run();
} finally {
synchronized(job) {
job.notify();
}
job = null;
}
}

job_lock.wait();//idle
}
} catch(InterruptedException e) {
//e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
job = null;
}
}
}

void setJob(Runnable job) {
synchronized(job_lock) {
this.job = job;
job_lock.notify();//wake up
}
}
}

}







//-----------







(e-mail address removed) (Christian) wrote in
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top