本篇內(nèi)容主要講解“什么是java守護(hù)線程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“什么是java守護(hù)線程”吧!
創(chuàng)新互聯(lián)是專業(yè)的中寧網(wǎng)站建設(shè)公司,中寧接單;提供網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行中寧網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
1、當(dāng)其他非守護(hù)線程完成時,守護(hù)線程將自行結(jié)束。
2、任何線程都可以成為守護(hù)線程。通過調(diào)用Thread.setdaemon()來聲明一個線程是一個守護(hù)線程。線程的共性是只有在非守護(hù)線程還在工作時才有意義。
實例
/** * 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; i++) 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 i=0; i < 10; i++) { threads[i] = new WorkerThread(bigMatrix[i]); threads[i].start(); } // Wait for each thread to finish try { for (int i=0; i < 10; i++) { threads[i].join(); max = Math.max(max, threads[i].getMax()); } } catch (InterruptedException e) { // fall through } System.out.println("Maximum value was " + max); } }
到此,相信大家對“什么是java守護(hù)線程”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁名稱:什么是java守護(hù)線程
URL網(wǎng)址:http://muchs.cn/article30/jehcso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)