Java start()方法不使用';不存在
这是我的第一个多线程应用程序,我有点困难。我正在创建一个名为of类TextDistanceThread的新对象,该对象实现Runnable,但当我尝试调用start()时,编辑器告诉我不存在这样的方法,并且该文件不会编译。这是我的驾驶课:Java start()方法不使用';不存在,java,multithreading,runnable,Java,Multithreading,Runnable,这是我的第一个多线程应用程序,我有点困难。我正在创建一个名为of类TextDistanceThread的新对象,该对象实现Runnable,但当我尝试调用start()时,编辑器告诉我不存在这样的方法,并且该文件不会编译。这是我的驾驶课: public class Driver { static float [][] results = new float[30][6]; public static void main(String [] args) throws FileNot
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();
}
}
}
公共类驱动程序{
静态浮动[]结果=新浮动[30][6];
公共静态void main(字符串[]args)引发FileNotFoundException{
Runnable[]threads=新的TextDistanceThread[180];
int threadCount=0;
对于(int i=0;i<30;i++){
threads[threadCount]=新的TextDistanceThread(“Macbeth.txt”,“Othello.txt”,i,0);
线程[threadCount++].start();
threads[threadCount]=新的TextDistanceThread(“Macbeth.txt”,“HuckFinn.txt”,i,1);
线程[threadCount++].start();
threads[threadCount]=新的TextDistanceThread(“Macbeth.txt”,“TomSawyer.txt”,i,2);
线程[threadCount++].start();
threads[threadCount]=新的TextDistanceThread(“Othello.txt”,“HuckFinn.txt”,i,3);
线程[threadCount++].start();
threads[threadCount]=新的TextDistanceThread(“Othello.txt”,“TomSawyer.txt”,i,4);
线程[threadCount++].start();
threads[threadCount]=新的TextDistanceThread(“TomSawyer.txt”,“HuckFinn.txt”,i,5);
线程[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;
}
}
final int numtext;
字典[]文本;
字典[]规范化;
浮差;
int[]长度;
int row,col;
PublicTextDistanceThread(字符串文件1、字符串文件2、int行、int列)抛出FileNotFoundException{
numTexts=2;
text=新字典[numtext];
规范化=新字典[numTexts];
对于(int text=0;text
我试着用谷歌搜索了整个地方,似乎没有其他人有这个问题。我确信我遗漏了一些非常明显的东西
另外,关于调度还有什么我应该知道的吗?可运行的
接口没有声明start()
方法。这就是为什么你不能叫它
使用Runnable
启动线程的标准方法是将Runnable
实例传递给thread
,并在thread
上调用start()
创建一个Thread
s数组,用Runnable
s创建Thread
s,然后在那些Thread
s上调用start()
Thread[] threads = new Thread[180];
例如
JVM负责为您调度线程;您不必担心调度它们。可运行的接口没有声明start()
方法。这就是为什么你不能叫它
threads[threadCount] = new Thread(
new TextDistanceThread("Macbeth.txt", "Othello.txt", i, 0));
new Thread(threads[threadCount++]).start();