簡體   English   中英

start()方法不存在

[英]start() method doesn't exist

這是我的第一個多線程應用程序,遇到了一些困難。 我正在創建一個名為Runsable的TextDistanceThread類的新對象,但是當我嘗試調用start()時,編輯器告訴我不存在這樣的方法,並且該文件將無法編譯。 這是我的驅動程序類:

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;
    }
}

我在整個地方都使用Google搜索,似乎沒有其他人遇到這個問題。 我確信我缺少了非常明顯的東西。

另外,關於調度我應該了解什么?

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM