JAVA Callback Efficiency test

Source: Internet
Author: User
Tags date1

In the recent network e-commerce project, want to achieve a single web container payment requests, all can be unified macro synchronization, to achieve granularity above the asynchronous, looking for, no related aspects of the framework or scheme, and finally based on the real scenario, intends to use Java callback multi-threaded way, False pseudo concurrency of 100000


/**
*
* @author Gyx
*
*/
public class A
{
Private double id = 0;
Public A (double ID)
{
This.setid (ID);
}
Public double getId ()
{
return ID;
}
public void SetId (double id)
{
This.id = ID;
}


/**
* Threading Class
* @author Gyx
*
*/
public class Mycallable implements Callable<a>
{
Private A obj;

/**
* Thread class constructor, incoming line program number
* @param tasknum
*/
Public mycallable (A obj)
{
This.setobj (obj);
}

/**
* Method of overriding interface, sub-thread invocation <br/>
* This method is to generate a number of 0-99, (The hundred indicates the sub-line program number) returned with the list
*/
Public A Call () throws Exception
{
Date DATETMP1 = new Date ();
SYSTEM.OUT.PRINTLN ("business_start.............and------Begin" + this.getobj (). GetId ());
This.getobj (). SetId (Math.random () * 1000000000);
Date DATETMP2 = new Date ();
Long time = Datetmp2.gettime ()-datetmp1.gettime ();
SYSTEM.OUT.PRINTLN ("Threads" + thread.currentthread (). GetId () + "task Time" + "duration +" milliseconds "");
return This.getobj ();
}


Public A Getobj ()
{
return obj;
}


public void Setobj (A obj)
{
This.obj = obj;
}



/**
* @author Gyx
* Main thread call
*/
public class Test
{
public static void Main (string[] args) throws Interruptedexception, Executionexception
{
Try
{
SYSTEM.OUT.PRINTLN ("----program starts running----");
Date date1 = new Date ();
int tasksize = 100000;
Create a pool of threads
Executorservice pool = Executors.newfixedthreadpool (4);
Executorservice pool = Executors.newcachedthreadpool ();
Create multiple tasks with return values
list<future<a>> list = new arraylist<future<a>> ();
for (int i = 0; i < tasksize; i++)
{
System.out.println ("The current is ........." + i);
callable<com.athena.ckx.module.transtime.a> C = new mycallable (new A (0));
Perform tasks and get future objects
future<a> f = pool.submit (c);
System.out.println ("Get-result----------------------" + f.get (). GetId ());
List.add (f);
}
Close the thread pool
Pool.shutdown ();
Date Date2 = new Date ();
SYSTEM.OUT.PRINTLN ("----program ends running----, program run Time" "+ (Date2.gettime ()-date1.gettime ()) +" milliseconds ");
Get run results for all concurrent tasks
for (future<a> f:list)
//          {
Gets the return value of the task from the future object and outputs it to the console
System.out.println ("Get-result----------------------" + f.get (). GetId ());
//          }
}
catch (Exception e)
{
E.printstacktrace ();
System.exit (0);
}
Finally
{
System.exit (0);
}
}
}



The result is:----program ends running----, program run time "26351 milliseconds"



To adjust the thread pool:

Executorservice pool = executors.newfixedthreadpool (2);

The result is:----program ends running----, program run time "17128 milliseconds"



To adjust the thread pool:

Executorservice pool = executors.newfixedthreadpool (1);

The result is: program run time "16269 milliseconds"



Conclusion: The Newcachedthreadpool efficiency is the worst, the number of threads less than Cpuh core 2/1 is relatively high, the cost is more stable, processing the present hypothetical, basically satisfies the requirement.






JAVA Callback Efficiency test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.