Is it unsafe to call methods from a thread?

Why can't i call Start() method twice on same thread object?

  • example code: public class MyThread extends Thread{ public void run(){ System.out.println("hi"); } } Thread mythread=new MyThread(); mythread.start(); mythread.start();// Exception why? Also Explain me What happens on calling Start() method? Note:don't say me it calls the run() method i know it Explain me Somewhat more Special things means internals details.  Thanks!

  • Answer:

    Solution- use a different object Eg- mythread.start() mythread2.start() Internal procssing- Calling start places the virtual CPU embodied in the thread into a runnable state, meaning it bcmz viable fr scheduling for execution by the JVM. In other words, by calling the start() you hav told the JVM to schedule your thread in the runnable threads queue. The nxt step automatically is to execute or run mythread. (Y do u want to re-request to place ur thread in the runnable state when the first call already does this for u. It is neither of a fruitful consequence nor semantically correct (poor compiler has to giv n error)) This request is generated by run(). (v say this as -the thread is in started state)Then when the thread reaches the start of the queue it starts running actually.

Nivedita Angel at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.