>  기사  >  Java  >  자바 데몬 스레드의 개념은 무엇입니까

자바 데몬 스레드의 개념은 무엇입니까

WBOY
WBOY앞으로
2023-05-01 18:07:13709검색

1. 데몬이 아닌 다른 스레드가 완료되면 데몬 스레드가 자동으로 종료됩니다.

2. 모든 스레드는 데몬 스레드가 될 수 있습니다. Thread.setdaemon()을 호출하여 스레드를 데몬 스레드로 선언합니다. 스레드의 공통점은 데몬이 아닌 스레드가 계속 작동할 때만 의미가 있다는 것입니다.

인스턴스

/**
 * 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);
    }
}

위 내용은 자바 데몬 스레드의 개념은 무엇입니까의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제