This is my first multithreaded application and I'm having a bit of difficulty. I am making a new object called of class TextDistanceThread that implements Runnable but when I try to call start(), the editor tells me that no such method exists and the file won't compile. Here's my 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();
}
}
}
and here's the TextDistanceThread class: public class TextDistanceThread implements 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;
}
}
I tried googling all over the place and no one else seems to be having this problem. I'm convinced I'm missing something terribly obvious.
In addition is there anything I should know about scheduling?
The Runnable
interface doesn't declare the start()
method. That is why you can't call it.
The standard way of starting a thread with a Runnable
is to pass the Runnable
instance to a Thread
, and call start()
on the Thread
.
Create an array of Thread
s, create the Thread
s with your Runnable
s, and then call start()
on those Thread
s.
Thread[] threads = new Thread[180];
E.g.
threads[threadCount] = new Thread(
new TextDistanceThread("Macbeth.txt", "Othello.txt", i, 0));
The JVM takes care of scheduling threads for you; you don't need to worry about scheduling them.