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