【问题标题】:I need help accessing this class我需要帮助才能访问此类
【发布时间】:2022-01-26 18:38:34
【问题描述】:

我真的需要解决这个问题,但我不知道如何解决,因此非常感谢任何形式的帮助。我是Java的新手,但我有编写代码的知识。我想使用类 AddComputeThread 添加一个新线程,但我不确定如何访问它或调用其中的方法。任何想法?在尝试创建该类的新实例时,它给了我 AddComputeThread 的构造函数未定义的错误。

这是java文件

package pgdp.pingulib.math.matrix;

import java.util.concurrent.atomic.AtomicInteger;

public class SquareMatrixAdd {

    private static final int MIN_DIM = 1 << 1;

    /**
     * Sequential matrix addition of A and B
     * 
     * @param A matrix
     * @param B matrix
     * @return result of the matrix addition in a new matrix
     */
    public static SquareMatrix addSequential(SquareMatrix A, SquareMatrix B) {
        if (A == null) {
            throw new IllegalArgumentException("A can not be null");
        }
        if (B == null) {
            throw new IllegalArgumentException("B can not be null");
        }
        if (A.getDimension() != B.getDimension()) {
            throw new IllegalArgumentException("A and B have different dimensions");
        }
        SquareMatrix C = new SquareMatrix(A.getDimension());
        for (int i = 1; i <= A.getDimension(); i++) {
            for (int j = 1; j <= A.getDimension(); j++) {
                C.set(i, j, A.get(i, j).add(B.get(i, j)));
            }
        }
        return C;
    }

    /**
     * Parallel matrix addition of A and B. If the dimensions are smaller or equal
     * than the default value <MIN_DIM>, the sequential matrix addition will be
     * applied.
     * 
     * @param A matrix
     * @param B matrix
     * @return result of the matrix addition in a new matrix
     */
    public static SquareMatrix addParallel(SquareMatrix A, SquareMatrix B) {
        return addParallel(A, B, MIN_DIM);
    }

    /**
     * Parallel matrix addition of A and B. If <minDim> is smaller than the default
     * value <MIN_DIM>, the default value will be used. If the dimensions are
     * smaller or equal than <minDim>, the sequential matrix addition will be
     * applied.
     * 
     * @param A      matrix
     * @param B      matrix
     * @param minDim dimension
     * @return result of the matrix addition in a new matrix
     */
    public static SquareMatrix addParallel(SquareMatrix A, SquareMatrix B, int minDim) {
        // TODO
        SquareMatrix C = new SquareMatrix(A.getDimension());
        if(minDim < MIN_DIM)
            minDim = MIN_DIM;
        else if (A == null) {
            throw new IllegalArgumentException("A can not be null");
        }
        if (B == null) {
            throw new IllegalArgumentException("B can not be null");
        }
        if (A.getDimension() != B.getDimension()) {
            throw new IllegalArgumentException("A and B have different dimensions");
        }
        if (A.getDimension() <= minDim) {
            C = addSequential(A, B);
        } else {
            for( int i = 1; i < A.getDimension()/2; ++i ) {
                for( int j = 1; j < A.getDimension()/2; ++j ) {
                    AddComputeThread trd = new AddComputeThread(1, A, B, minDim, C);
                }
            }
        }

        return C;
    }

    private static class AddComputeThread extends ComputeThread {

        private static AtomicInteger computeThreadCounter = new AtomicInteger(0);

        /**
         * initialize compute thread for matrix addition.
         * 
         * @param threadID index where result will be stored
         * @param A        first matrix
         * @param B        second matrix
         * @param minDim   threshold for sequential computation
         * @param results  array reference where the result will be stored
         */
        AddComputeThread(int threadID, SquareMatrix A, SquareMatrix B, int minDim, SquareMatrix[] results) {
            super(threadID, A, B, minDim, results);
            this.setName("AddComputeThread-" + computeThreadCounter.incrementAndGet());
        }

        /**
         * resets the thread counter
         */
        public static synchronized void resetThreadCount() {
            computeThreadCounter = new AtomicInteger(0);
        }

        /**
         * retrieves the current thread count
         * 
         * @return thread count
         */
        public static AtomicInteger getThreadCount() {
            return computeThreadCounter;
        }

        @Override
        public void run() {
            // TODO
            this.start();
            
        }
    }
}

【问题讨论】:

  • 错误可能告诉您的是,您的 AddComputeThread 构造函数的最后一个参数应该是一个数组。您正在尝试在那里使用 SquareMatrix 的实例而不是数组。
  • @khelwood 非常感谢您
  • 糟糕的标题。重写以总结您的具体技术问题。

标签: java multithreading oop


【解决方案1】:

这里:

AddComputeThread trd = new AddComputeThread(1, A, B, minDim, C);

您正在尝试调用此 AddComputeThread 构造函数:

AddComputeThread(int threadID, SquareMatrix A, SquareMatrix B, int minDim, SquareMatrix[] results)

但是你给出的第五个参数 C 是 SquareMatrix 的一个实例,而不是所需的数组。

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多