Java beats them all

J

John Bailo

John said:
Ok, here's my c# mono version.

And here's the java version.

As before, once the threads are started ( and I watched it in the eclipse
debugger, where I can see all 2000 threads being started ).

Shut down takes less than a millesecond!

18
523
18
523

Here's the code:

/*
* Created on Nov 25, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author jbailo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/


public class SimpleThread extends Thread {

public SimpleThread(String str) {
super(str);
}

public void run() {
//for(int i = 0; i < 10; i++) {
//System.out.println(getName());

while(!TwoThreadsDemo.Run)
try{
sleep((long)(0));
} catch(Exception e) {}

TwoThreadsDemo.counter++;
}

}




/*
* Created on Nov 25, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author jbailo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/


import java.util.*;

public class TwoThreadsDemo {

public static boolean Run = false;
public static int counter = 0;

public static void main(String[] args) {


Calendar cal = new GregorianCalendar();

//int time = sec*1000+ms;





for (int i = 0; i < 2000; i++)
new SimpleThread("#" + i).start();



int sec = cal.get(Calendar.SECOND); // 0..59
int ms = cal.get(Calendar.MILLISECOND); // 0..999

System.out.println(sec);
System.out.println(ms);

Run = true;

while(counter<1000)
{}

sec = cal.get(Calendar.SECOND); // 0..59
ms = cal.get(Calendar.MILLISECOND); // 0..999
System.out.println(sec);
System.out.println(ms);

//System.out.println("Time is" + time);

}
}

//






The run times so far produced:

1 - 1.2s

BUT -- .NET seems to tank at about 1100 threads.

So my test only uses 1000 threads.

Next up -- java !


using System;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace threadtest {

internal class ThreadClass
{
public void Performjob()
{

while(!Mainclass.Run)
Thread.Sleep(0);

Mainclass.TC++;

}

public class Mainclass
{

public static bool Run = false;
public static int TC = 0;


public static int Main (string[] args)
{

ThreadClass j = new ThreadClass();
try{

for(int i=0; i<1000; i++)
{
Thread backgroundThread =
new Thread(new
ThreadStart(j.Performjob));
backgroundThread.Start();
}
} catch ( Exception e )
{
Console.WriteLine( "Error: " + e.ToString());
}

int intsec =
DateTime.Now.Second*1000+DateTime.Now.Millisecond;

Run = true;

while(TC<1000)
Thread.Sleep(0);

intsec = DateTime.Now.Second*1000+DateTime.Now.Millisecond -
intsec;
Console.WriteLine("end main, intsec {0}", intsec);

return 0;
}
}
}




Set the global to 1 and then wait on the 2,000 terminations.
After the last thread has terminated,
mark the time and note the difference.
 
J

Jeff_Relf

Hi John, your: while( counter < 1000 ) {}
is not equivalent to my:
pH = TH - 1 ; Loop ( _2000 ) WaitForSingleObject( * ++ pH, INFINITE );

Ray did it right ( using C, but not 2.6 ), and it was 1.8 times slower.

By the way, Why all the blank lines... geez.

Re: <<
for (int i = 0; i < 2000; i++)
new SimpleThread("#" + i).start(); >>

Does that really spawn the threads,
....weren't you just watching the constructor in the debugger ?
 
J

Jeff_Relf

Hi John, You told me: << Sorry, Jeffie, Only The Best Languages ( Java )
Get to Launch 2000 Threads Fast >>

No, C++ on the 2.6 Linux kernel would be at least as fast, if not more.

And I noticed that you couldn't do it in C#.

And showing zero milliseconds is not too cool I think,
I'd either increase the precision or, better yet, try 100,000 threads.
 
J

John Bailo

Jeff_Relf said:
Hi John, You told me: << Sorry, Jeffie, Only The Best Languages ( Java
)
Get to Launch 2000 Threads Fast >>

No, C++ on the 2.6 Linux kernel would be at least as fast, if not more.

No, it's not.

I tried the threadtest.c code compiled in gcc and the results are:

jbailo@linux:~> ./threadtest
Starting, 1000 threads...
Total time: 6.440502 seconds.
jbailo@linux:~> vi threadtest.c //setting MAXTHREADS=2000


jbailo@linux:~> gcc threadtest.c -o threadtest -lpthread
jbailo@linux:~> ./threadtest
Starting, 2000 threads...
Total time: 13.413545 seconds.
jbailo@linux:~>
 
J

Jeff_Relf

Hi John, I wonder why threadtest.c
is over ten times slower than my code/PC ?

jbailo@linux:~> gcc threadtest.c -o threadtest -lpthread
jbailo@linux:~> ./threadtest
Starting, 2000 threads...
Total time: 13.413545 seconds.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top