Hey,
Recently I am trying to comprehend the multithreading concept. I was actually able to build a small app for the purpose. A class was GUI, a class was calculating Hailstone sequence and sending the process to the GUI.
Let me explain what I am failing to understand so that you can at least guide me:
Think of two classes:
public class ClassOne implements Runnable{ public static void main(String[] args) { ClassOne cOne = new ClassOne(); ClassTwo cTwo = new ClassTwo(); Thread t1 = new Thread(cOne); Thread t2 = new Thread(cTwo); t1.start(); t2.start(); cOne.someMethod(); cTwo.angryMethod(); } @Override public void run() { for(int i=0;i<20;i++){ System.out.println("ClassOne run method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void someMethod(){ for(int i=0;i<20;i++){ System.out.println("ClassOne some method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void someMethod2(){ for(int i=0;i<20;i++){ System.out.println("ClassOne some method 2"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void someMethod3(){ for(int i=0;i<20;i++){ System.out.println("ClassOne some method 3"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
This is the another one:
public class ClassTwo implements Runnable{ @Override public void run() { for(int i=0;i<20;i++){ System.out.println("Class Two run method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void coolMethod(){ for(int i=0;i<20;i++){ System.out.println("Class Two cool method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void notSoCoolMethod(){ for(int i=0;i<20;i++){ System.out.println("Class Two not so cool method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void angryMethod(){ for(int i=0;i<20;i++){ System.out.println("Class Two pissed method"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
So, suppose I created threads to run. A thread executes only what is inside run() methods, right? But I have some other methods here, they will have to be executed, and maybe they will need to communicate.
Considering we cannot create another method in a method (run method), how do they make it work?
A thread will come and only read run() method.
If I call angryMethod() or someMethod2(), this is not multithreading.
If I call methods from inside of run() methods, all of them will run regardless what I actually wanted.
So that makes me wonder... Tutorials are simple as expected, they print some stuff to console. I searched for some open source projects and actually downloaded some (One is endlos - a multi-threaded fractal generator for example). I opened every single java file, but I couldn't find Thread or Runnable words
Another one was too complex for me to understand.
And I am curious how they implement this?
Do they make tons of classes for the sake of multithreading? I mean what is this? Please explain or guide me.