Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze the code in the Java creation thread

Shulou Source: shulou.com Published: 2022-06-02 12:40:23 10月31日 Update

How to analyze the Java creation thread of the code, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Java creation threads often appear in our coding, and there are a lot of problems bothering us when we use them. Let's first learn about the code related to the creation of threads in Java. I hope you can help.

Import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; / * Java threads: thread pool-* * @ author Administrator 2009-11-4 23:30:44 * / public class Test {public static void main (String [] args) {

Java creates threads that can be scheduled to run commands after a given delay or to execute them periodically. ScheduledExecutorService pool = Executors.newScheduledThreadPool (2); create and implement the Runnable interface object, Thread object 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 threads into the pool to execute pool.execute (T1); pool.execute (T2); pool.execute (T3); / / use deferred execution style methods pool.schedule (T4, 10, TimeUnit.MILLISECONDS); pool.schedule (T5, 10, TimeUnit.MILLISECONDS) / / close thread pool pool.shutdown ();}} class MyThread extends Thread {@ Override public void run () {System.out.println (Thread.currentThread (). GetName () + "executing.") ;} import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; / * Java thread: thread pool-* * @ author Administrator 2009-11-4 23:30:44 * / public class Test {public static void main (String [] args) {

Java creates threads that can be scheduled to run commands after a given delay or to execute them periodically. ScheduledExecutorService pool = Executors.newScheduledThreadPool (2); create and implement the Runnable interface object, Thread object 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 threads into the pool to execute pool.execute (T1); pool.execute (T2); pool.execute (T3); / / use deferred execution style methods pool.schedule (T4, 10, TimeUnit.MILLISECONDS); pool.schedule (T5, 10, TimeUnit.MILLISECONDS) / / close thread pool pool.shutdown ();}} class MyThread extends Thread {@ Override public void run () {System.out.println (Thread.currentThread (). GetName () + "executing.") ;}} Java code pool-1-thread-1 is being executed. Pool-1-thread-2 is executing. Pool-1-thread-1 is executing. Pool-1-thread-1 is executing. Pool-1-thread-2 is executing. Will Process finished with exit code 0 be helpful to you after reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Thread current object interface delay code help command method style run analyze clear have content for this article novice time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info macOS Microsoft Apple