I'm confused on how I would use a single thread to open and read multiple files compared to having a thread per file. Basically, I'm confused with the syntax and how I could establish lets say 50, 60, or even 100 different threads depending on how many files need to be read. Also, can you give me a brief explanation of what exactly "runnable" means??
-
Do you need to open multiple threads at a time – Noxious Reptile Nov 29 '15 at 07:33
-
i have to do two different things.I give the user of my program the option of how they want their files read. I can either have a thread that reads many files or have a thread per file. My problem is how am i supposed to establish a thread per file without knowing beforehand the amount of files that need to be read. Should I just use a forloop for that? – Sam Nov 29 '15 at 19:29
2 Answers
Multithreading is prefered when you need a responsive program which has lots of time consuming operations. If your file operations are time consuming then you can use multithreading so that your main thread wont get unresponsive till the operation finishes. This concept is extensively used in GUI programs .If you use the main thread to open and read multiple files,then your program will get unresponsive for some time if your files are so large. In that case I prefer multithreading. See these Links. Maybe you will get an inspiration from this.
http://www.tutorialspoint.com/java/java_files_io.htm
http://beginnersbook.com/2013/03/multithreading-in-java/
http://java2novice.com/java_thread_examples/implementing_runnable/
Runnable represent a task in Java which is executed by Thread. java.lang.Runnable is an interface and defines only one method called run(). When a Thread is started in Java by using Thread.start() method it calls run() method of Runnable task which was passed to Thread during creation. Code written inside run() method is executed by this newly created thread. Since start() method internally calls run() method its been a doubt among Java programmers that why not directly call the run() method. This is also asked as what is difference between start() and run() method in Java. Well when you call Runnable interface run() method directly , no new Thread will be created and task defined inside run() method is executed by calling thread. There is another interface added in Java 1. 5 called Callable which can also be used in place of Runnable interface in Java. Callable provides additional functionality over Runnable in terms of returning result of computation. Since return type of run() method is void it can not return anything which is sometime necessary. On the other hand Callable interface defines call() method which has return type as Future which can be used to return result of computation from Thread in Java.
Read more: http://java67.blogspot.com/2012/08/what-is-thread-and-runnable-in-java.html#ixzz3src0C72H

- 838
- 1
- 7
- 24
Runnable is the interface that classes must implement in Java, so that they may be executed in a Thread. Basically, it forces the programmer to implement the method(s) that the thread requires to run your customized statements when it executes (void run()) - http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html
To open multiple files within a single thread, your runnable object will need to have access to the list of files that you intend to open. Not knowing if you intend to open the same set of files (in the same location) or dynamic lists of files, I won't make recommendations on how to manage that activity. The easiest way to effectively learn this would be to use a collection of your files, that is available to your runnable class.
Creating a Thread is relatively straightforward - Here is a definition for a class:
public class MyThread extends Thread {
public void run(){
System.out.println("MyThread running");
}
}
Here is the implementation of that class thar would start the thread
class HelloWorldApp { public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start
}
}
Creating individual Threads would require making multiple instances of the MyThread class - each one is tied to a specific thread (i.e: MyThread mythread2,mythread3, etc)
Hope this gets you on your way!

- 1,460
- 11
- 19