Ok, so I wrote a very simple programme, just to practice multithreading.
I know it can be coded better, but I'm still learning about this, so please be lenient.
This is my code so far:
package threads;
// Create a thread
import java.io.File;
class threads implements Runnable {
// This should scan the directory every 2 minutes
Thread masterThread;
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 threads
Thread currentThread = Thread.currentThread();
Thread firstThread = new Thread();
Thread secondThread = new Thread();
String fileName = "";
while (masterThread == currentThread) {
// Scan the folders, assign the files to threads 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 threads for each of the contained files, up to a limit of 3 threads.
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
firstThread.start();
} else if (!secondThread.isAlive()) {
// Assign secondThread the task of handling the file
secondThread.start();
}
}
}
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);
} 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);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
class ThreadStarter {
[B]public static void main(String[] args) {[/B]
// This thread will scan the folders every 2 minutes, and assign other threads to handle files found in the directories
new threads();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
I marked the one line bold
(this line)
public static void main(String[] args) {
because I get an error on that line that keeps me from compiling.
Here is the error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - inner classes cannot have static declarations
at threads.threads$ThreadStarter.main(threads.java:88)
I'm pretty confused. I've been browsing around trying to find out what's causing it, but no luck so far. Was wondering if someone here at DaniWeb could lend me a hand?