java守护线程的概念是什么
作者:RiverSoul
时间:2023-04-27
来源:互联网
浏览:0
1、当其他非守护线程完成时,守护线程将自行结束。2、任何线程都可以成为守护线程。通过调用Thread.setdaemon()来声明一个线程是一个守护线程。线程的共性是只有在非守护线程还在工作时才有意义。实例/***Createstenthreadstosearchforthemaximumvalueofalargematrix.*Eachthreadsearchesoneportionofthematrix.*/publicclassTenThreads{privatestaticclassWorkerT
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);
}
}
作者最新文章
微软推出Project Zenith:面向Windows 11开发者的AI硬件加速方案
2026-09-08 18:15
打破流量垄断,让平台经济释放普惠红利
2026-09-08 18:07
Arm AGI CPU详解:136核Neoverse V3,3nm双芯粒架构与AI数据中心部署
2026-09-08 17:18
Windows安装Docker教程:启用WSL2并运行第一个容器验证
2026-09-04 09:26
PDF转Word操作指南:在线与本地转换方法及格式检查
2026-09-03 16:03
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















