Java thread, thread pool basic Application Demo sample code recall

Source: Internet
Author: User
Tags lenovo

Java thread, thread pool basic Application Demo sample code recall

Package org.rui.thread;/** * Define tasks *  * @author Lenovo * */public class LiftOff implements Runnable {protected int count down=10;private static int taskcount=0;private final int id=taskcount++;p ublic LiftOff () {}public LiftOff (int countdown) {This.countdown=countdown;} Public String status () {return "#" +id+ "(" + (Countdown>0?countdown: "liftoff!") +"),";} @Overridepublic void Run () {while (countdown-->0) {System.out.print (status));//Part of the mechanism that allows the CPU to be transferred from one thread to another with a single thread  //It declares: I have run through the most important part of my life cycle, and this is a good time to switch to other tasks for a period of time  //To be completely first choice. Thread.yield ();//Thread Scheduling}}}

Package org.rui.thread;/** * Run  is not a separate thread driver. It is called directly in Main and  still uses threads here. That thread that is always assigned to main * @author Lenovo * */public class Mainthread {public static void main (string[] args) {LiftOff launch=new L Iftoff (); Launch.run ();}} /** *output: #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff!), * *

Package org.rui.thread;/** * Thread class driver liftoff * @author Lenovo * */public class Basicthreads {public static void main (STR Ing[] args) {thread t=new thread (new LiftOff ()); T.start (); System.out.println ("Waiting for Liftoff ()");}} /**output:waiting for Liftoff () #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff!), */

Package org.rui.thread;/** * Many other thread drivers liftoff * @author Lenovo * */public class Morebasicthreads {public static void main (string[] args) {for (int i=0;i<5;i++) {thread t=new thread (new LiftOff ()); T.start (); System.out.println ("Waiting for Liftoff ()");}}} /**output:waiting for Liftoff () #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff!), Waiting for Liftoff () waiting for Liftoff () #2 (9), #2 (8), #2 (7), #2 (6), #2 (5), #2 (4), #2 (3), #2 (2), #2 (1), #2 (liftoff!), Waiting for Liftoff () waiting for Liftoff () #4 (9), #1 (9), #3 (9), #4 (8), #3 (8), #4 (7), #3 (7), #4 (6), #3 (6), #4 (5), #3 (5), #4 (4), #3 (4), #4 (3 ), #3 (3), #4 (2), #3 (2), #4 (1), #3 (1), #4 (liftoff!), #3 (liftoff!), #1 (8), #1 (7), #1 (6), #1 (5), #1 (4), #1 (3), #1 (2), #1 (1), #1 ( liftoff!), */

Package Org.rui.thread;import Java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * Use executor * @author Lenovo * */public class Cachedthreadpool {public static void main (string[] args) {//Create and return settings with frequently used configuration string Method of Executorservice. /** * Newcachedthreadpool * Create a thread pool that can be created on the basis of the need to create a new thread. However, the threads that were once constructed will be reused when they are available. */executorservice Exec=executors.newcachedthreadpool (); for (int i=0;i<5;i++) {Exec.execute (New LiftOff ());} Starts a sequential shutdown and runs the task that was submitted. But do not accept new tasks.

Exec.shutdown ();//Prevent new tasks from being submitted to executor}}/** * OUTPUT: #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 ( liftoff!), #1 (9), #3 (9), #1 (8), #3 (8), #1 (7), #3 (7), #1 (6), #3 (6), #1 (5), #3 (5), #1 (4), #3 (4), #1 (3), #3 (3), #1 (2), #3 (2), #1 (1 ), #3 (1), #1 (liftoff!), #3 (liftoff!), #2 (9), #2 (8), #2 (7), #2 (6), #2 (5), #2 (4), #2 (3), #2 (2), #2 (1), #2 (liftoff!), #4 (9), #4 ( 8), #4 (7), #4 (6), #4 (5), #4 (4), #4 (3), #4 (2), #4 (1), #4 (liftoff!), * *


Package Org.rui.thread;import Java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * Use executor *  * in whatever thread pool the existing thread is actively re-used, if possible @author Lenovo * */public class Fixedthreadpool {public static void Ma In (string[] args) {/** * Creates a thread pool that can reuse a fixed number of threads to execute these threads in a shared, unbounded queue. */executorservice Exec=executors.newfixedthreadpool (5); for (int i=0;i<5;i++) {Exec.execute (New LiftOff ());} Exec.shutdown ();}} /** * OUTPUT: #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff!), #1 (9), #3 (9), #1 (8), #3 (8), #1 (7), #3 (7), #1 (6), #3 (6), #1 (5), #3 (5), #1 (4), #3 (4), #1 (3), #3 (3), #1 (2), #3 (2), #1 (1), #3 (1), #1 (liftoff!), #3 (liftoff!), #2 (9), #2 (8), #2 (7), #2 (6), #2 (5), #2 (4), #2 (3), #2 (2), #2 (1), #2 (liftoff!), #4 (9), #4 (8), #4 (7), #4 (6), #4 (5), #4 (4), #4 (3), #4 (2) #4 ( 1), #4 (liftoff!), */

Package Org.rui.thread;import Java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * Use executor * * As a demo example if you have a large number of threads then the tasks they perform will use the file system. * You can use single.

。 To execute these threads. To make sure that there is only a single task to execute at random time on whatever thread. * This way you don't need to process synchronization on a shared resource.

。 * @author Lenovo * */public class Singlethreadpool {public static void main (string[] args) {/** * creates an Exec with a single worker thread Utor, executes the thread */executorservice exec=executors.newsinglethreadexecutor () in a unbounded queue, and for (int i=0;i<5;i++) { Exec.execute (New LiftOff ());} Exec.shutdown ();}} /** * OUTPUT: #0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (liftoff!), #1 (9), #3 (9), #1 (8), #3 (8), #1 (7), #3 (7), #1 (6), #3 (6), #1 (5), #3 (5), #1 (4), #3 (4), #1 (3), #3 (3), #1 (2), #3 (2), #1 (1), #3 (1), #1 (liftoff!), #3 (liftoff!), #2 (9), #2 (8), #2 (7), #2 (6), #2 (5), #2 (4), #2 (3), #2 (2), #2 (1), #2 (liftoff!), #4 (9), #4 (8), #4 (7), #4 (6), #4 (5), #4 (4), #4 (3), #4 (2) #4 ( 1), #4 (liftoff!), */



Java thread, thread pool basic Application Demo sample code recall

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.