2.1 create thread, 2.1 thread

Source: Internet
Author: User

2.1 create thread, 2.1 thread

Public class custom thread creation {
Public static void main (String [] args ){
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (2,
5,
0,
TimeUnit. SECONDS,
New ArrayBlockingQueue <Runnable> (2 ),
New ThreadFactory (){
Public Thread newThread (Runnable r ){
Thread t = new Thread (r );
System. out. println ("Creat ");
Return t;
}
},
// New ThreadPoolExecutor. AbortPolicy (); // first type of denial policy: directly throw an exception AbortPolicy
New ThreadPoolExecutor. CallerRunsPolicy (); // Method 2: Create a thread
// New ThreadPoolExecutor. DiscardOldestPolicy (); // The third type of denial policy: the earliest one in the queue
// New ThreadPoolExecutor. DiscardPolicy (); // type 4: discard directly.
// New RejectedExecutionHandler (){
// Public void rejectedExecution (Runnable r, ThreadPoolExecutor executor ){
// System. out. println (Thread. currentThread (). getName () + "abandoned ");
// Try {
// Thread. sleep (2000 );
//} Catch (InterruptedException e ){
// E. printStackTrace ();
//}
// System. out. println (Thread. currentThread (). getName () + "REJECTED ");
//
//}
//}); // Type: discard directly.
For (int I = 0; I <10; I ++ ){
Final int finalI = I;
ThreadPoolExecutor.exe cute (new Runnable (){
Public void run (){
System. out. println (Thread. currentThread (). getName () + "start" + finalI );
Try {
Thread. sleep (10000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println (Thread. currentThread (). getName () + "end" + finalI );}
});
}
}
}

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.