Reputation: 33
Recently I've started to learn Java and I have a question about IO streams and threads. In our language, both of the definitions translate like "Поток" and it confuses me.
I'm wondering about conception in general for both topics: streams and threads. I will provide an example that will show my point of view related to this topic.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try{
FileOutputStream fos = new FileOutputStream("C:\\Users\\TMS\\Desktop\\Workspace\\src\\File.txt");
FileInputStream fis = new FileInputStream("C:\\Users\\TMS\\Desktop\\Workspace\\src\\FileWithInfo");
int supnum = 0;
try{
while((supnum = fis.read()) != -1){
fos.write(supnum);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e){
e.printStackTrace();
}
}
}
As I can understand I created instances of two classes: FileOutputStream and FileInputStream. Instances of these classes we can use as a "controller" which helps us to control our streams by providing with the methods (.write and .read for example) and at the same time implicitly Java "speaks" with the processor and tells the processor to create new streams (I'm not sure, but probably if we create lots of streams, we even will see them in Task Manager).
So in the and we have a chain:
And if we talk about threads the chain is almost the same:
public class Program {
public static void main(String[] args) {
System.out.println("Main thread started...");
Runnable r = ()->{
System.out.printf("%s started... \n", Thread.currentThread().getName());
try{
Thread.sleep(500);
}
catch(InterruptedException e){
System.out.println("Thread has been interrupted");
}
System.out.printf("%s finished... \n", Thread.currentThread().getName());
};
Thread myThread = new Thread(r,"MyThread");
myThread.start();
System.out.println("Main thread finished...");
}
}
We have an instance (myThread), which helps us to control our thread with the methods.
The difference that I see is that streams can use only for transferring data but threads are a more complex tool, which allows us to use them as an additional .main(String[] args) method.
I want to know:
Is my guesses correct?
Why IO related classes don't implement Runnable interfaces like class Thread?
Can we use threads for transferring data like we use streams?
Upvotes: 3
Views: 2491
Reputation: 11
An interesting note: There are different terms in Russian that describe these two things. A Stream is indeed "Поток", while a Thread is actually called "Нить".
Unfortunately, though, in the Russian programming tradition, there can indeed be confusion between these two concepts, because multithreading is translated as "многопоточность", which literally means "multistreaming" or "multicurrenting".
I don't know why such a confusion appeared in the Russian language (maybe because of some "creative" translators who thought that coming up with a more poetic-sounding term is better that translating a term literally)
I would personally translate "multithreading" literally, saying something like "многонитевость", but a lot of people would disagree (I guess some people like gatekeeping obscure terms to confuse the newcomers).
This is one of the reasons why I try relying on documentation in English first, and only after start reading it in Russian or Spanish, because there are a lot of little discrepancies like that. For example, Spanish speakers would call an Array "Arreglo", a List would be "Lista" , but "ArrayList" would be pronounced as "Arrai List" and not "Arreglo Lista".
Anyways, yeah, there is a difference between Threads and IO Streams. Threads allow the use of concurrent programming, while IO Streams are just a way to show where the information is coming from and where it is coming to.
For example, reading with your eyes would be an Input Stream, your brain processing the information would be a Thread, and you speaking would be an Output Stream.
Upvotes: 1
Reputation: 988
It's unfortunate that these terms translate to the same thing in your language since they are completely different concepts.
Streams control a flow of data. In your example, this is the flow of data out of and into files, but you can use them for other things too. See a ll the subclasses of inputstream here: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html
Threads control a flow of instructions. In your example, you delayed an instruction by half a second. The most valuable part of controlling instructions with threads is the ability to create more than one thread, which can allow 2 flows of instructions to execute at the same time. The concept of multithreading/parallelism is a bit too in depth for a single stackoverflow answer. If you want more information on multithreading, there are a lot of resources about it online. I strongly recommend reading around the subject, since there are many, many things that can go wrong when manually implementing multithreading.
Upvotes: 7