Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the code in the Java creation thread

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report