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.