gpt4 book ai didi

Java多线程程序不使用大量CPU

转载 作者:行者123 更新时间:2023-12-02 18:18:44 25 4
gpt4 key购买 nike

我是编程和Java初学者,这是我的第一个多核程序。问题是我的程序使用的 CPU 从未超过 13%。我不知道我这样做是否正确。

如何加快计算速度并使用更多 CPU 资源?

我的程序由三类组成:

  1. “用多个线程实例化 Work 对象的主类

  2. 扩展 Thread 并包含要执行的工作的“T1”类

  3. “工作”类,启动所需的线程号并显示所有线程执行工作所花费的时间

这是我的 Main 类的代码:

public static void main(String[] args) {

System.out.println("Number of CPUs available = " + Runtime.getRuntime().availableProcessors()); //Display the number of CPUs available
int iteration = 100000000; // Define a number of itterations to do by all threads

/*
Instantiates each work with a different number of threads (1, 4, 8, 12, and 24)
*/
Work t1 = new Work(1);
Work t4 = new Work(4);
Work t8 = new Work(8);
Work t12 = new Work(12);
Work t24 = new Work(24);


/*
Launch the work for each thread with the specified number of iterations
*/
t1.goWork(iteration);
t4.goWork(iteration);
t8.goWork(iteration);
t12.goWork(iteration);
t24.goWork(iteration);

}

这里是工作类代码:

public class Work {

static long time; // A variable that each thread increase by the time it takes to complete its task.
static int itterationPerThread; // A variable that stores the number of itterations Per Thread to do.
static int finish; // A variable that each thread incrase when it finish its task, used to wait until all thread has complete their task.
private int numberOfThreads; // The number of threads to launch.
/**
*
* The constructor, set the number Of threads to run
* @param numberOfThreads
*/
public Work(int numberOfThreads)
{
this.numberOfThreads = numberOfThreads; //Set the number of threads
}

/**
*
* A method that launch a specified number of thread in the constructor of the class, and distributes the a number of iteration of each thread.
* The method does nothing until each thread completes its task and print the time needed for all threads to complete their tasks.
* @param itterationPerThread
*/
public void goWork(int itterationPerThread)
{
finish = 0; //Reset the variable in the case that we call the method more than one time
time = 0; //Reset the variable in the case that we call the method more than one time
this.itterationPerThread = itterationPerThread/numberOfThreads; // Divide the given number of iterations by the number of threads specified in the constructor

for (int i=0; i<numberOfThreads; i++) //Launch the specified number of threads
{
new T1().run();
}

while (finish != numberOfThreads) //Do nothing until all thread as completed their task
{
}
System.out.println("Time for " + numberOfThreads + " thread = " + time + " ms"); //Display the total time
}

}

最后是我的 T1 类(class):

public class T1 extends Thread{

@Override
public void run()
{
long before = System.currentTimeMillis();

for (int i=0; i<Work.itterationPerThread; i++) //Get the thread busy with a number of itterations
{
Math.cos(2.1545); //Do something...
}

long after = System.currentTimeMillis(); //Compute the elapsed time
Work.time += after - before; //Increase the static variable in Work.java by the time elapsed for this thread
Work.finish++; // Increase the static variable in Work.java when the thread has finished its job
}
}

该程序在我的机器上提供了以下输出(四个物理核心和八个超线程):

可用 CPU 数量 = 8

1 个线程的时间 = 11150 毫秒

4 个线程的时间 = 4630 毫秒

8 个线程的时间 = 2530 毫秒

12 个线程的时间 = 2530 毫秒

24 个线程的时间 = 2540 毫秒

根据我的 CPU,这个结果似乎是正确的,但我的 CPU 使用率从未超过 13%。

我发现了以下Stack Overflow post ,但我并没有真正找到我的问题的答案。

最佳答案

您应该调用Thread.start(),而不是调用Thread.run()(它实现线程的功能),它将创建一个新线程并调用在新线程上 run()

现在您正在主线程上运行run(),而无需创建新线程。由于您的 CPU 负载为 13%,因此我预计您有 8 个核心(意味着您已完全填满单个核心)。

更好的是创建接口(interface)Runnable的自定义实现,而不是扩展Thread。然后您可以按如下方式在线程上运行它:

Thread t = new Thread(new MyRunnableTask());
t.start();

这是常见的方法,因为它使您可以灵活地(稍后)使用更高级的机制,例如 ExecutorService

编辑:正如一些评论中也指出的那样。您还可以从多个线程更改相同的变量(Work 中的静态变量)。你永远不应该这样做,因为它允许竞争条件。例如,增加一个变量可能会导致一个变量,如 here 所解释的那样。 。

关于Java多线程程序不使用大量CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com