Hi to everyone, actually i wanna know why we have to use Threads and in this case what the guy above has posted what is purpose of that code since we can output that he wants to output just with writting this code in main class:
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
---------------------------------------------------------------------------------------------
public class HelloRunnable implements Runnable {
//run method for HelloRunnable i.e. for the thread #1
public void run(){
System.out.print("thread #1: ");
for(int i=0; i<10; i++){
System.out.print(i+" ");
}
System.out.println();
}
public class HelloRunnable2 implements Runnable {
// run method for HelloRunnable2 i.e. for the thread #2
public void run() {
System.out.print("thread #2: ");
for (int i = 10; i < 20; i++) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
(new Thread(new HelloRunnable())).start();
(new Thread(new HelloRunnable2())).start();
}
}
what is actually the difference doing this with thread and doing without threads since the result is the same?
Anyone who can explain it ?