answersLogoWhite

0


Best Answer

No. If you want to start a new thread of execution, you need to call the start() method of the thread.

Also, the run() is like any other java method and you can invoke it directly but if you do so, it would be called as part of the current programs thread and not as a new thread.

When the start() method is invoked, the JVM creates a new thread and automatically calls the run() method and that is why a new thread gets started and not by calling run() directly.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can the run method be called directly to start a thread?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you use two or more run methods for a single thread in same method?

No. Each thread can have only one run method. You can overload the run method because it is just another java method but only the default run method with void return type will get called when you start the thread


What do you mean by multithreaded program Explain the life cycle of a thread?

Thread exists in several states. A thread just created is in the born state. When the threads start method is called, it enters the runnable (ready) state. Then the system assigns a processor to the thread. A thread enters the dead state when its run method completes or terminates for any reason. When a sleep method is called in a running thread, that thread becomes ready after the designated sleep time expires. Even if a processor is available, sleeping thread can not use it. A running thread can enter a blocked state.


How do you call repaint using thread in java applet?

To call repaint using a thread in a Java applet, you can use the repaint method directly within the run method of a Thread or within the run method of a Runnable that is executed by a Thread. Inside the run method, you can call repaint to update the applet's graphics. For example: public void init() { // ... Thread thread = new Thread(new Runnable() { public void run() { // Perform background tasks // ... repaint(); // Call repaint to update graphics } }); thread.start(); } Alternatively, you can also extend the Thread class itself and override its run method to include the repaint call.


How do you call start method into run method?

In Java a Thread object has two methods that the programmer needs to know: start and run.run is where we put the code that we want to execute when the Thread begins.The start method is what tells the Java Virtual Machine that it should create a new thread and tell it to execute its run method.While you can make a call to thread.run() in order to execute the code in the run method, this will not actually make a new thread in the JVM, but will execute just like any normal method call.


Life cycle of thread in java?

# New state - After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.# Runnable (Ready-to-run) state - A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on processor. # Running state - A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.# Dead state - A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again. # Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.

Related questions

Can you use two or more run methods for a single thread in same method?

No. Each thread can have only one run method. You can overload the run method because it is just another java method but only the default run method with void return type will get called when you start the thread


What do you mean by multithreaded program Explain the life cycle of a thread?

Thread exists in several states. A thread just created is in the born state. When the threads start method is called, it enters the runnable (ready) state. Then the system assigns a processor to the thread. A thread enters the dead state when its run method completes or terminates for any reason. When a sleep method is called in a running thread, that thread becomes ready after the designated sleep time expires. Even if a processor is available, sleeping thread can not use it. A running thread can enter a blocked state.


How start method call the run method?

I think you're referring to the Thread class, and how calling Thread.start() simply appears to call the Thread.run() method. What happens is that the start() method tells the Java Virtual Machine to actually create and run a new thread. If you manually call the run() method, then your Thread object will execute its code, but it will run like a normal method - that is to say it will not spawn a new thread in the JVM.


How do you call repaint using thread in java applet?

To call repaint using a thread in a Java applet, you can use the repaint method directly within the run method of a Thread or within the run method of a Runnable that is executed by a Thread. Inside the run method, you can call repaint to update the applet's graphics. For example: public void init() { // ... Thread thread = new Thread(new Runnable() { public void run() { // Perform background tasks // ... repaint(); // Call repaint to update graphics } }); thread.start(); } Alternatively, you can also extend the Thread class itself and override its run method to include the repaint call.


How do you call start method into run method?

In Java a Thread object has two methods that the programmer needs to know: start and run.run is where we put the code that we want to execute when the Thread begins.The start method is what tells the Java Virtual Machine that it should create a new thread and tell it to execute its run method.While you can make a call to thread.run() in order to execute the code in the run method, this will not actually make a new thread in the JVM, but will execute just like any normal method call.


Describe the Life cycle of a thread in java.?

Life cycle of a thread New: when a thread is not yet initialized, it is in New state Runnable or Ready : Once the thread is initialized and its start method is called, then the thread becomes ready for execution, which is otherwise called runnable or ready state Not Runnable : When a thread is asked to sleep for some time or wait for some time by using the sleep or wait method, thread goes to Not Runnable state for the specified time. Dead state: Once the thread is executed,it turns to dead state. Hope this would answer your question. let me know if you are still not clear


Life cycle of thread in java?

# New state - After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.# Runnable (Ready-to-run) state - A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on processor. # Running state - A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.# Dead state - A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again. # Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.


What is the name of the method used to schedule a thread for execution?

"start" You've created a Thread object and it knows its target. Now it's time to get the whole thread thing running. It's pretty straight forward: t.start(); Prior to calling start() on a Thread instance, the thread (when we use lowercase t, we're referring to the thread of execution rather than the Thread class) is said to be in the new state as we said. The new state means you have a Thread object but you don't yet have a true thread. So what happens after you call start()? • A new thread of execution starts (with a new call stack). • The thread moves from the new state to the runnable state. • When the thread gets a chance to execute, its target run() method will run.


What are the threads called?

tl;dr: They're called threads because thread is an apt metaphor. When you start a thread, you rely on the operating system to allocate processing time so that your thread can execute. While your thread is executing, the processor (or core) is placing all of its attention on your thread.


Multithreading in java?

I am assuming that you want to know how to multithread in Java. 1) Write a class that implements Runnable. Put just the method run() in it. 2) Inside the run() method, put the code that you want your thread to run. 3) Instantiate the class (example: Runnable runnable = new MyRunnable();) 4) Make a new Thread (example: Thread thread = new Thread(runnable, <the name of your thread(optional)>); 5) Start the thread (example: thread.start();) 6) That's it! Your thread is now running. PS. Check the Java API for more information. Did that answer your question?


How are java threads created?

Thread can be created in two ways either by extending Thread or implementing runnable interface Examples: public class A extends Thread { } public class A implements Runnable { } By using both the methods we can create a Java Thread. The disadvantage of extending the Thread class is that, you cannot extend the features of any other class. If we extend the Thread class we cannot extend any other class whose features we may require in this class. So it is usually advisable to implement the Runnable Interface so that we can extend the required features and at the same time use the Thread functionality.


Under Linux can you start a thread from a thread?

yes