start() 方法不存在

start() method doesn't exist

这是我的第一个多线程应用程序,我遇到了一些困难。我正在创建一个名为 class TextDistanceThread 的新 object,它实现了 Runnable,但是当我尝试调用 start() 时,编辑器告诉我不存在这样的方法,文件将无法编译。这是我的 Driver class:

public class Driver {
    static float [][] results = new float[30][6];
    public static void main(String [] args) throws FileNotFoundException {

        Runnable [] threads = new TextDistanceThread[180];
        int threadCount = 0;

        for(int i = 0 ; i < 30 ; i++) {
            threads[threadCount] = new TextDistanceThread("Macbeth.txt", "Othello.txt", i, 0);
            threads[threadCount++].start();
            threads[threadCount] = new TextDistanceThread("Macbeth.txt", "HuckFinn.txt", i, 1);
            threads[threadCount++].start();
            threads[threadCount] = new TextDistanceThread("Macbeth.txt", "TomSawyer.txt", i, 2);
            threads[threadCount++].start();
            threads[threadCount] = new TextDistanceThread("Othello.txt", "HuckFinn.txt", i, 3);
            threads[threadCount++].start();
            threads[threadCount] = new TextDistanceThread("Othello.txt", "TomSawyer.txt", i, 4);
            threads[threadCount++].start();
            threads[threadCount] = new TextDistanceThread("TomSawyer.txt", "HuckFinn.txt", i, 5);
            threads[threadCount++].start();
        }
    }
}

这是 TextDistanceThread class: public class TextDistanceThread 实现 Runnable {

    final int numTexts;
    Dictionary<String, Integer>[] texts;
    Dictionary<String, Float>[] normalized;
    float difference;
    int [] lengths;
    int row, col;

    public TextDistanceThread(String file1, String file2, int row, int col) throws FileNotFoundException {
        numTexts = 2;
        texts = new Dictionary[numTexts];
        normalized = new Dictionary[numTexts];
        for (int text = 0 ; text < numTexts ; text++) {
            texts[text] = new Dictionary<String, Integer>();
            normalized[text] = new Dictionary<String, Float>();
        }
        difference = 0;
        lengths = new int[numTexts];
        this.row = row;
        this.col = col;

        //Read file into dictionary without punctuation
        if(new File(file1).exists()) {System.out.println("File " + file1 + " found");}
        Scanner text = new Scanner(new File(file1));
        while (text.hasNext()) {
            lengths[0]++;
            String temp = text.next().toLowerCase().replaceAll("(?!\')\p{Punct}", "");
            if (!texts[0].add(temp, 1)) {
                texts[0].set(temp, texts[0].lookup(temp) + 1);
            }
        }

        if(new File(file2).exists()) {System.out.println("File " + file2 + " found");}
        text = new Scanner(new File(file2));
        while (text.hasNext()) {
            lengths[1]++;
            String temp = text.next().toLowerCase().replaceAll("(?!\')\p{Punct}", "");
            if (!texts[1].add(temp, 1)) {
                texts[1].set(temp, texts[1].lookup(temp) + 1);
            }
        }
    }

    public void run() {

        System.out.println("Normalizing:");
        //Normalize dictionaries
        for(int i = 0 ; i < numTexts ; i++) {
            texts[i].reset();
            normalized[i].add((String) texts[i].getCurrentPair().getKey(), (float)texts[i].getCurrent() / lengths[i]);

            while(texts[i].hasNext()) {
                texts[i].next();
                normalized[i].add((String) texts[i].getCurrentPair().getKey(), (float)texts[i].getCurrent() / lengths[i]);
            }
        }

        //Find the difference
        texts[0].reset();

        System.out.println("Cross-checking:");
        while(normalized[0].hasNext()) {
            if(normalized[1].contains(normalized[0].getCurrentPair().getKey())) {
                difference += Math.abs(normalized[0].getCurrent() - normalized[1].lookup((String)normalized[0].getCurrentPair().getKey()));
                //System.out.println(normalized[0].getCurrentPair() + Float.toString(Math.abs(normalized[0].getCurrent() - normalized[1].lookup((String) normalized[0].getCurrentPair().getKey()))));
                normalized[1].remove(normalized[0].getCurrentPair().getKey());
                normalized[0].remove();
                normalized[0].reset();
            }
            else {
                normalized[0].next();
            }
        }

        System.out.println("Adding:");
        for(int i = 0 ; i < numTexts ; i++) {
            normalized[i].reset();
            difference += normalized[i].getCurrent();
            //System.out.println(normalized[i].getCurrentPair() + Float.toString(normalized[i].getCurrent()));
            while(normalized[i].hasNext()) {
                difference += Math.abs(normalized[i].getNext());
                //System.out.println(normalized[i].getCurrentPair() + Float.toString(normalized[i].getCurrent()));
            }
        }

        Driver.results[row][col] = difference;
    }
}

我到处搜索谷歌,似乎没有其他人遇到过这个问题。我确信我遗漏了一些非常明显的东西。

此外,关于日程安排还有什么我应该知道的吗?

Runnable 接口没有声明 start() 方法。这就是为什么你不能调用它。

使用 Runnable 启动线程的标准方法是将 Runnable 实例传递给 Thread,然后在 Thread 上调用 start() ].

创建一个 Thread 数组,用您的 Runnable 创建 Thread,然后在这些 Thread 上调用 start()

Thread[] threads = new Thread[180];

例如

threads[threadCount] = new Thread(
    new TextDistanceThread("Macbeth.txt", "Othello.txt", i, 0));

JVM 负责为您安排线程;你不需要担心安排他们。

因为Runnable接口没有start()方法。您应该将 Runnable 实例传递给 Thread。你可以像

new Thread(threads[threadCount++]).start();