start()方法不存在

时间:2015-09-21 22:35:40

标签: java multithreading runnable

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

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类:     公共类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;
    }
}

我试着在各地搜索谷歌,似乎没有其他人遇到这个问题。我确信我错过了一些非常明显的东西。

另外,我应该知道有关安排的事情吗?

2 个答案:

答案 0 :(得分:2)

Runnable界面并未声明start()方法。这就是你无法召唤它的原因。

使用Runnable启动主题的标准方法是将Runnable实例传递给Thread,并在start()上调用Thread

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

Thread[] threads = new Thread[180];

E.g。

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

JVM负责为您安排线程;你不必担心安排它们。

答案 1 :(得分:2)

由于Runnable接口没有start()方法。您应该将Runnable实例传递给Thread。您可以使用

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