cpu 使用多线程 java
cpu usage multithreading java
public class Main {
public static void main(String[] args) {
LinkedList<Th> myThreads = new LinkedList<>();
for (int i = 0; i < 100; i++) {
myThreads.add(new Th());
myThreads.get(i).start();
}
}
}
public class Th extends Thread {
int myInt;
@Override
public void run() {
while(true) {
doSomething();
}
}
private void doSomething() {
switch (myInt) {
case 0: {
// System.out.println("comment part");
break;
}
case 2:{
System.out.println("2");
break;
}
}
}
}
你好,
我在 java 中遇到简单的多线程问题。
当我 运行 这个程序时,cpu 使用率突然变成 100%,我的电脑崩溃了,我无法停止这个程序。
在这里你可以看到代码。
当我取消评论评论部分时,一切正常!但我需要在不在控制台中打印任何内容的情况下解决这个问题。
PS。我已经知道案例 2 代码块永远不会编译。
加一觉。只需调用打印所在的睡眠方法即可。
您的 CPU 应该休眠几毫秒以避免 100% 消耗它。
在您的情况下,console-io 充当睡眠。
尝试减少线程并使用循环法进行计算。
Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices (also known as time quanta) are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). See https://en.wikipedia.org/wiki/Round-robin_scheduling
建议修改:
private void doSomething() {
switch (myInt) {
case 0: {
try {
sleep(100);
} catch (InterruptedException e) {
interrupt();
}
break;
}
case 2:{
System.out.println("2");
break;
}
}
}
您应该调用中断方法重新中断线程。
参考:Why invoke Thread.currentThread.interrupt() when catch any InterruptException?
这段代码本身并没有错。
是您的机器无法处理您要求它完成的工作量。
您的代码生成 100
线程,这些线程将向标准输出打印一些内容(操作成本高,因为您必须要求 OS 来执行此操作) 与它们一样快可以。即使对于现代机器来说,这也是很多工作。
打印完留言后,尝试主动放弃CPU一段时间,再睡一会。
switch (myInt) {
case 0: {
System.out.println("message");
Thread.sleep(50);
break;
}
此外,100%
使用 CPU 并没有错。您只是在使用所有可用的计算能力。这是个好东西,尤其是在科学计算方面。
public class Main {
public static void main(String[] args) {
LinkedList<Th> myThreads = new LinkedList<>();
for (int i = 0; i < 100; i++) {
myThreads.add(new Th());
myThreads.get(i).start();
}
}
}
public class Th extends Thread {
int myInt;
@Override
public void run() {
while(true) {
doSomething();
}
}
private void doSomething() {
switch (myInt) {
case 0: {
// System.out.println("comment part");
break;
}
case 2:{
System.out.println("2");
break;
}
}
}
}
你好, 我在 java 中遇到简单的多线程问题。 当我 运行 这个程序时,cpu 使用率突然变成 100%,我的电脑崩溃了,我无法停止这个程序。 在这里你可以看到代码。 当我取消评论评论部分时,一切正常!但我需要在不在控制台中打印任何内容的情况下解决这个问题。 PS。我已经知道案例 2 代码块永远不会编译。
加一觉。只需调用打印所在的睡眠方法即可。 您的 CPU 应该休眠几毫秒以避免 100% 消耗它。 在您的情况下,console-io 充当睡眠。
尝试减少线程并使用循环法进行计算。
Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices (also known as time quanta) are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). See https://en.wikipedia.org/wiki/Round-robin_scheduling
建议修改:
private void doSomething() {
switch (myInt) {
case 0: {
try {
sleep(100);
} catch (InterruptedException e) {
interrupt();
}
break;
}
case 2:{
System.out.println("2");
break;
}
}
}
您应该调用中断方法重新中断线程。 参考:Why invoke Thread.currentThread.interrupt() when catch any InterruptException?
这段代码本身并没有错。 是您的机器无法处理您要求它完成的工作量。
您的代码生成 100
线程,这些线程将向标准输出打印一些内容(操作成本高,因为您必须要求 OS 来执行此操作) 与它们一样快可以。即使对于现代机器来说,这也是很多工作。
打印完留言后,尝试主动放弃CPU一段时间,再睡一会。
switch (myInt) {
case 0: {
System.out.println("message");
Thread.sleep(50);
break;
}
此外,100%
使用 CPU 并没有错。您只是在使用所有可用的计算能力。这是个好东西,尤其是在科学计算方面。