Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread starts running before calling Thread.start

Tags:

People also ask

What happens when thread start () method is called?

If you call start() method on Thread, Java Virtual Machine will call run() method and two threads will run concurrently now - Current Thread and Other Thread or Runnable implementation.

What will happen if we call run method instead of calling the start method when starting a thread?

The run method is just another method. If you call it directly, then it will execute not in another thread, but in the current thread. If start isn't called, then the Thread created will never run. The main thread will finish and the Thread will be garbage collected.

Can we call the Run method instead of start to start the execution of thread after you create it you must?

Instead the run() method is executed by the thread that created the thread. In other words, the thread that executed the above two lines of code. To have the run() method of the MyRunnable instance called by the new created thread, newThread , you MUST call the newThread. start() method.

How does run () method is invoked by thread run ()?

When a Thread object's run() method is invoked directly, the statements in the run() method are executed by the current thread rather than by the newly created thread.


t1=threading.Thread(target=self.read()) print("something") t2=threading.Thread(target=self.runChecks(), args=(self,)) 

self.read runs indefinitely, so the program won't ever reach the print line. How is this possible without calling t1.start()? (Even if I call that, it shold start running and go on to the next line, shouldn't it?)