I've written the code already, and it compiles, but gets the following runtime exception:
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:595)
at threads.ThreadStarter.run(ThreadStarter.java:52)
at java.lang.Thread.run(Thread.java:619)
I've higlighted the line that throws the exception. I have no idea what's wrong, my knowledge in threads is sadly lacking.
If anyone could help me out it would be great.
package threads;
// Create a thread
import java.io.File;
class ThreadStarter implements Runnable {
public static void main(String[] args) {
// This thread will scan the folders every 2 minutes, and assign other ThreadStarter to handle files found in the directories
while (true) {
new ThreadStarter().Threads();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// This should scan the directory every 2 minutes
Thread masterThread;
void Threads() {
// Create a second thread - this thread will handle the files found in the folder
masterThread = new Thread(this, "first child");
masterThread.start();
}
// Entry point for second thread
public void run() {
// TODO:: Make more ThreadStarter
Thread currentThread = Thread.currentThread();
Thread firstThread = new Thread();
Thread secondThread = new Thread();
String fileName = "";
while (masterThread == currentThread) {
// Scan the folders, assign the files to ThreadStarter for processing
File main_dir = new File("C:\\Thread test");
File[] child_dirs = main_dir.listFiles();
for (int i = 0; i < child_dirs.length; i++) {
// [get filename] dir_name = child_files[i].getName();
// start thread to look for files in this dir.
File[] new_files = child_dirs[i].listFiles();
// create ThreadStarter for each of the contained files, up to a limit of 3 ThreadStarter.
try {
for (int ii = 0; ii < new_files.length; ii++) {
fileName = new_files[ii].getName();
if (!firstThread.isAlive()) {
// Assign firstThread the task of handling the file
[B]firstThread.start();[/B]
} else if (!secondThread.isAlive()) {
// Assign secondThread the task of handling the file
secondThread.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
while (firstThread == currentThread) {
try {
// Handle file
File file = new File(fileName);
// Destination directory
File dir = new File("C:\\Thread test\\retain");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
System.out.println(fileName + " was moved succesfully");
if (!success) {
System.out.println(fileName + " was not moved succesfully");
}
Thread.sleep(5000);
firstThread.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
while (secondThread == currentThread) {
try {
//Handle file
File file = new File(fileName);
// Destination directory
File dir = new File("C:\\Thread test\\retain");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
System.out.println(fileName + " was moved succesfully");
if (!success) {
System.out.println(fileName + " was not moved succesfully");
}
Thread.sleep(5000);
secondThread.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
}