Java, Threads, will only run () be part of a thread or code after calling start ()?

I am starting Threads in java, I would like to have a clear idea of ​​how start() / run() works in my situation.

I created a calle t thread, and I placed t.start() , followed by the for loop.

Will the for loop be part of the .t stream or is part of the main stream


 class Job implements Runnable{ Thread t; Job(String tName){ t=new Thread(this, tName); System.out.println("This is thread: "+t.getName()); t.start(); System.out.println("This is the end of constructor"); try{ /*<<<<<<<------------WILL THIS RUN in my MAIN thread or in the t thread??*/ for(int i=0;i<5;i++){ System.out.println("xxxThis is the count i value: "+i+" "+ t.getName()); Thread.sleep(1000); } } catch(InterruptedException e){ System.out.println("The thread has been interrupted"); } } public void run(){ System.out.println("This is the start of RUN()"); try{ for(int i=0;i<5;i++){ System.out.println("This is the count i value: "+i+" "+ t.getName()); Thread.sleep(1000); } } catch(InterruptedException e){ System.out.println("The thread has been interrupted"); } finally{ System.out.println("Fnally block reached: "+t.getName()); } } } 
+4
source share
3 answers

I would like to have a clear idea of ​​how start () / run () works in my situation

start() method spawns a new thread of execution and executes the run method on that thread. In this case, the thread will have its own call stack . Whereas calling the run() method does not directly generate a new thread. Instead, this will cause the run() method to execute on the current executable thread that has the old call stack.

Will the for loop be part of the .t stream or is part of the main stream

The for loop will be part of Thread (in your case, this is the main thread, if you create an instance of Job in the main thread), from which Thread t spawned. And if you want to confirm, just type the name Thread , which is executed for the loop using Thread.currentThread().getName() . Example:

 try{ /*<<<<<<<------------WILL THIS RUN in my MAIN thread or in the t thread??*/ System.out.println(Thread.currentThread().getName()+" is executing this for loop"); for(int i=0;i<5;i++){ System.out.println("xxxThis is the count i value: "+i+" "+ t.getName()); Thread.sleep(1000); } 
+2
source

The t.start() method and the following try/for code are executed in the same thread. This is the main thread if you called the Job (String) constructor from the main thread.

The run() method executes in a new thread.

+1
source

Will the for loop be part of thread.t or is it part of the main thread

Main stream. When a thread branches, the new Thread simply calls the run() method. In your case, the main thread calls start() , and then continues to run the for() method. This will most likely be called before a new thread starts. The new thread that forks only calls the run() method and any other methods used by run() .

FYI is considered a very bad practice to start a thread from an object constructor. This is a "leak" of references to the current object while it is still initializing. You should consider adding the start() method on Job or calling start() after the completion of the Job constructor.

  Job job = new Job(...); job.start(); 

Also, since the main thread is the one that your for loop executes, it will throw an InterruptedException only if the main thread is interrupted, not the Job thread.

  catch(InterruptedException e){ System.out.println("The thread has been interrupted"); } 
+1
source

All Articles