Home>

1 Reasons for introducing thread poolsBecause the life cycle of a thread includes create, ready, run, block, and destroy phases,When we have a small number of tasks,We can create several threads ourselves to handle the corresponding tasks,But when there are a lot of tasks,Because creating and destroying threads requires a lot of overhead,These problems are greatly alleviated by using thread pools.

2 Use of the thread poolWe just need to use the static methods provided by the executors class,You can create the corresponding thread pool:

Public static executorsevice newsinglethreadexecutor ()
Public static executorsevice newfixedthreadpool ()
Public static executorsevice newcachedthreadpool ()

Newsinglethreadexecutor returns a single-threaded executor. When multiple tasks are delivered to this sector, this thread finishes processing one task and then processes the next task.If the thread is abnormal,There will be a new thread to replace it.

Newfixedthreadpool returns a thread pool containing a specified number of threads,If there are more tasks than threads,Then no unexecuted tasks must wait,Until a task is completed.

Newcachedthreadpool creates a corresponding thread to process according to the number of user tasks,The thread pool does not limit the number of threads,Depends entirely on the number of threads the JVM can create,May cause insufficient memory.

We just need to put the task to be executed in the run method,The implementation class of the runnable interface is given to the execute method of the thread pool as one of its parameters.As follows:

executor executor=executors.newsinglethreadexecutor ();
executor.execute (new runnable () {
  public void run () {
    //executed task
 }
}

If you need to pass parameters to the task,This can be done by creating an implementation class for the runnable interface.

3. Examples(1):newsinglethreadexecutor

mythread.java

publicclassmythread extends thread {
  @override
  publicvoid run () {
    system.out.println (thread.currentthread (). getname () + "In progress.
. . ");
  }
}
testsinglethreadexecutor.java
publicclasstestsinglethreadexecutor {
  publicstaticvoid main (string [] args) {
    //Create a thread pool with a reusable fixed number of threads
    executorservice pool=executors. newsinglethreadexecutor ();
    //Create an object that implements the runnable interface,The thread object of course also implements the runnable interface
    thread t1=new mythread ();
    thread t2=new mythread ();
    thread t3=new mythread ();
    thread t4=new mythread ();
    thread t5=new mythread ();
    //Put the thread into the pool for execution
    pool.execute (t1);
    pool.execute (t2);
    pool.execute (t3);
    pool.execute (t4);
    pool.execute (t5);
    //Close the thread pool
    pool.shutdown ();
  }
}

Output result

pool-1-thread-1 is executing.
. .
pool-1-thread-1 is executing.
. .
pool-1-thread-1 is executing.
. .
pool-1-thread-1 is executing.
. .
pool-1-thread-1 is executing.
. .

(2):newfixedthreadpool

testfixedthreadpool.java

publicclass testfixedthreadpool {
  publicstaticvoid main (string [] args) {
    //Create a thread pool with a reusable fixed number of threads
    executorservice pool=executors.newfixedthreadpool (2);
    //Create an object that implements the runnable interface,The thread object of course also implements the runnable interface
    thread t1=new mythread ();
    thread t2=new mythread ();
    thread t3=new mythread ();
    thread t4=new mythread ();
    thread t5=new mythread ();
    //Put the thread into the pool for execution
    pool.execute (t1);
    pool.execute (t2);
    pool.execute (t3);
    pool.execute (t4);
    pool.execute (t5);
    //Close the thread pool
    pool.shutdown ();
  }
}

Output result

pool-1-thread-1 is executing.
. .
pool-1-thread-2 is executing.
. .
pool-1-thread-1 is executing.
. .
pool-1-thread-2 is executing.
. .
pool-1-thread-1 is executing.
. .

(3):newcachedthreadpool

testcachedthreadpool.java

publicclass testcachedthreadpool {
  publicstaticvoid main (string [] args) {
    //Create a thread pool with a reusable fixed number of threads
    executorservice pool=executors.newcachedthreadpool ();
    //Create an object that implements the runnable interface,The thread object of course also implements the runnable interface
    thread t1=new mythread ();
    thread t2=new mythread ();
    thread t3=new mythread ();
    thread t4=new mythread ();
    thread t5=new mythread ();
    //Put the thread into the pool for execution
    pool.execute (t1);
    pool.execute (t2);
    pool.execute (t3);
    pool.execute (t4);
    pool.execute (t5);
    //Close the thread pool
    pool.shutdown ();
  }
}

Output results:

pool-1-thread-2 is executing.
. .
pool-1-thread-4 is executing.
. .
pool-1-thread-3 is executing.
. .
pool-1-thread-1 is executing.
. .
pool-1-thread-5 is executing.
. .

(4):newscheduledthreadpool

testscheduledthreadpoolexecutor.java

publicclass testscheduledthreadpoolexecutor {
  publicstaticvoid main (string [] args) {
    scheduledthreadpoolexecutor exec=new scheduledthreadpoolexecutor (1);
    exec.scheduleatfixedrate (new runnable () {//An exception is triggered every so often
           @override
           publicvoid run () {
              //throw new runtimeexception ();
              system.out.println ("================);
           }
         }, 1000, 5000, timeunit.milliseconds);
    exec.scheduleatfixedrate (new runnable () {//print the system time every time,Prove that the two are unaffected
           @override
           publicvoid run () {
              system.out.println (system.nanotime ());
           }
         }, 1000, 2000, timeunit.milliseconds);
  }
}

Output result

=================
8384644549516
8386643829034
8388643830710
=================
8390643851383
8392643879319
8400643939383
  • Previous php classic algorithm highlights
  • Next Teach you how to solve the problem of plugins not working after XCODE upgrade