I know that multithreads in java allows me do multi tasks in the same time, but in the following code I'm not make a sense that the three threads done in the same time

public class myThreads
{

 
 public static void main(String [] args)
 {
   System.out.println("Begin main Function");
   mThread Thread_One = new mThread ("One",0,3);
   mThread Thread_Two = new mThread ("Two",4,7);
   mThread Thread_Three = new mThread("Three",8,11);
   
   Thread_One.start();
   Thread_Two.start();
   Thread_Three.start();
   
   System.out.println("End main Function");
   
 }// main
}//myThreads


class mThread extends Thread
{
 private String ThreadName;
 private int StartFrom;
 private int FinishTo;
 
 public mThread(String name, int n1, int n2)
 {
   ThreadName = name;
   StartFrom = n1;
   FinishTo = n2;
 }

 public void run(){
   for (int i=StartFrom; i<=FinishTo; i++)
     System.out.println(ThreadName+ "Counter:" +i);
 }


}//mThread

the output of the program are

Begin main Function
End main Function
OneCounter:0
OneCounter:1
OneCounter:2
OneCounter:3
TwoCounter:4
TwoCounter:5
TwoCounter:6
TwoCounter:7
ThreeCounter:8
ThreeCounter:9
ThreeCounter:10
ThreeCounter:11

why the statement " End main Function " printed before excute the threads ?
and if the 3 threads done simultanously
why the output beomes
OneCounter:0
TwoCounter:4
ThreeCounter:8
OneCounter:1
TwoCounter:5
ThreeCounter:9
and so on.........

You have 4 threads in your program - the three that you have created and the main thread. The main thread (the main method) is finished after creating the three other threads. There is no guarantee regarding the order in which the threads will run and for how long, which explains why you are getting different output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.