Compare differences in running time (efficiency) between multithreaded programming and single-threaded programming in JAVA
Code:
Package thread;
public class J extends Thread
{
private int a[];
J (int b[])
{
A=new Int[b.length];
int len=b.length;
for (int i=0;i<len;i++) a[i]=b[i];
}
public void Run ()
{
int sum=0;
for (int i=0;i<a.length;i++) sum+=a[i];
System.out.println ("Total added " +a.length+ " times " );
}
public static void Main (String args[])
{
Long T1=system.currenttimemillis ();
SYSTEM.OUT.PRINTLN (t1);
int n=0;
while (n<1000000)
{n= (int) (Math.random () *10000000);}
int a[]=new Int[n];
for (int i=0;i<n;i++) a[i]= (int) math.random () *100;
J J1=new J (a);
Try
{
J1.start ();
Long T2=system.currenttimemillis ();
System.out.println (T2);
Long t=t2-t1;
System.out.println ("The time required for single-threaded calculation is " +t);
}
catch (Exception e)
{
E.printstacktrace ();
}
int b[]=new INT[A.LENGTH/2];
for (int i=0;i<b.length;i++) b[i]=a[i];
int c[]=new Int[a.length-b.length];
for (int i=0;i<c.length;i++) c[i]=a[b.length+i];
Long T3=system.currenttimemillis ();
System.out.println (T3);
J J2=new J (b); J J3=new J (c);
Try
{
J2.start (); J3.start ();
Long T4=system.currenttimemillis ();
System.out.println (T4);
System.out.println ("The time required for double-threading calculation is " + (T4-T3));
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
Operation Result:
Java multithreaded programming