Learn about thread pools and how to create them from scratch in Java. This article includes an introduction, implementation, and testing of a simple thread pool version.

Create and run threads

Thread creation is a well-known process that can be created in two ways:

  • Extend the thread and override the run method
  • The Feed Runnable of the thread constructor

Let’s implement these two options:

public static void main(String[] args) throws Exception { // Thread from runnable Thread thread = new Thread(() -> System.out.println("hey from runnable")); // Thead from extened thread class Thread myThread = new MyThread(); } static class MyThread extends Thread { @Override public void run() { System.out.println("hey from thread extended class"); }}Copy the code

The difference between the startup and run methods

Each thread provides two methods to start its activity, but with one significant difference:

  • The run method executes instructions in the currently running thread.
  • The Start method executes instructions in a new separate thread.

Let’s describe these two situations:

Thread pooling: Reuse existing threads to save memory

The thread pool pattern represents reusing existing threads and running all new instructions without allocating new threads. Thread pools control the number of running threads and are widely used in production.

This pattern is implemented in Java:

  • executors . newscheduledthreadpool(THREAD _ POOL _ NUMBER)
  • Executors.newCachedThreadPool()

Instead of exploring existing Java thread pools, we will try to create our own.

The thread Runnable cannot be changed

The existing thread design does not allow you to change instructions (what can be run). Therefore, all available setters are:

There is no setRunnable in the setters provided. So how does Java’s thread pool work?

You can run another Runnable in the current thread

Java allows other runnable programs to run in the current thread. So, to “change” runnables dynamically, we can run runnables in a loop of a thread. Let’s just create a worker thread that will perform other runnable tasks inside an infinite loop:

The thread executes the Runnable internally without calling the new thread

As you saw in the previous code, when the main thread executed newTaskBeExecuted, it used the run() method instead of the start() method. This does not invoke a new thread, but executes directly internally. To double check that other threads are used, we print the name of the current thread and verify that only one thread is used.

Use the created thread pool

Now that we have our own thread pool, we can feed our tasks with Runnables. Once the main worker thread (the only thread) becomes available, it will run:

After execution, we can see that different runnables are executed in a main worker thread. This proves that our thread pool is working as expected.

conclusion

In this article, we reviewed the core ideas of the thread pool pattern and implemented an overly simplified version. Our example is not suitable for production, but it illustrates how a real thread pool works. Here you can copy the examples used.

The last

The opportunity is to prepare for the people, like + favorites, more Java course learning route, notes, and other structure information, want to learn friends pay attention to the blog + private letter “learning” can get free information!!