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

What is the concept of java daemon thread

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

1. When other non-daemon threads finish, the daemon thread will end on its own.

2. Any thread can become a daemon thread. Declare that a thread is a daemon thread by calling Thread.setdaemon (). What threads have in common is that it only makes sense when non-daemon threads are still working.

Example

/ * Creates ten threads to search for the maximum value of a large matrix. * Each thread searches one portion of the matrix. * / public class TenThreads {private static class WorkerThread extends Thread {int max = Integer.MIN_VALUE; int [] ourArray; public WorkerThread (int [] ourArray) {this.ourArray = ourArray;} / / Find the maximum value in our particular piece of the array public void run () {for (int I = 0; I < ourArray.length) Max = Math.max (max, ourArray [I]);} public int getMax () {return max;}} public static void main (String [] args) {WorkerThread [] threads = new WorkerThread [10]; int [] [] bigMatrix = getBigHairyMatrix (); int max = Integer.MIN_VALUE / / Give each thread a slice of the matrix to work with for (int iTun0; I < 10; iBigMatrix +) {threads [I] = new WorkerThread (bigMatrix [I]); threads [I] .start ();} / / Wait for each thread to finish try {for (threads [I] .join () Max = Math.max (max, thread [I] .getMax ());} catch (InterruptedException e) {/ / fall through} System.out.println ("Maximum value was" + max) }} after reading this, the article "what is the concept of java daemon thread" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report