Package CN; import java. util. Concurrent .*;
Import java. util. date;
Import java. util. List;
Import java. util. arraylist ;/**
** Java thread: A thread with a returned value
* @ Author wb_qiuquan.ying
*/
@ Suppresswarnings ("unchecked ")
Public class test {
@ Suppresswarnings ("rawtypes ")
Public static void main (string [] ARGs) throws executionexception,
Interruptedexception {
Int sizenum = 5;
// Create a thread pool
Executorservice pool =
Executors. newfixedthreadpool (sizenum );
// Create multiple tasks with returned values
List <future> List = new arraylist <future> ();
For (INT I = 0; I <sizenum; I ++ ){
// Callable c = new mycallable (I + ""); it can also be written in this way, and the running result is the same
Mycallable c = new mycallable (I + "");
// Execute the task and obtain the future object
Future F = pool. Submit (C );
System. Out. println ("the value of this thread during thread running is:" + F. Get (). tostring ());
List. Add (f );
}
// Close the thread pool. Shutdown ();
// Obtain the running results of all concurrent tasks
System. Out. println ("");
For (Future F: List ){
// Obtain the task return value from the future object and output it to the console
System. Out. println ("the value of each thread after the thread pool is closed:" + F. Get (). tostring ());
}
System. Exit (0 );
}
} Class mycallable implements
Callable <Object> {private string tasknum; mycallable (string tasknum ){
This. tasknum = tasknum;
}
// Call () method
Public object call ()
Throws exception {
System. Out. println (">>>" + tasknum + "Call () method task start ");
Date datetmp1 = new date ();
Thread. Sleep (1000 );
Date datetmp2 = new date ();
Long time = datetmp2.gettime ()-datetmp1.gettime ();
System. Out. println (">>>" + tasknum + "task termination ");
Return "Thread" + tasknum + "return the running time [" + time + "millisecond ]";
}
} // Output >>> 0 call () method to start the task
>>> 0. Task termination
When the thread is running, the value of this thread is: Thread 0 returns the running time [1000 ms]
>>> 1 call () method task start
>>> 1. Task termination
The value of this thread during thread running is: the time when thread 1 returns to run [1000 ms]
>>> 2 call () method to start a task
>>> 2. Task termination
The value of this thread during thread running is: thread 2 returns the running time [1000 ms]
>>> 3 call () method task start
>>> 3. Task termination
The value of this thread during thread running is: thread 3 returns the running time [1000 ms]
>>> 4 call () method task start
>>> 4. Task termination
When the thread is running, the value of this thread is: thread 4 returns the running time [1000 milliseconds] After the thread pool is closed, the value of each thread is: Thread 0 returns the running time [1000 milliseconds]
After the thread pool is closed, the value of each thread: thread 1 returns the running time [1000 ms]
After the thread pool is closed, the value of each thread is: thread 2 returns the running time [1000 ms]
After the thread pool is closed, the value of each thread: thread 3 returns the running time [1000 ms]
After the thread pool is closed, the value of each thread is: thread 4 returns the running time [1000 ms]
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