answersLogoWhite

0

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.

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

Can the run method be called directly to start a thread?

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.


How do you call repaint using thread in java applet?

You call the repaint method the same way you'd call any other method from a thread. final Applet appletToRepaint; new Thread() { public void run() { appletToRepaint.repaint(); } }.start();


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


Can you declare more than one single main method in java program why?

It doesn't really make sense. The JVM needs to know where to start running the program; that's what the "main" method is for. A class - one that can be run directly - needs to have a single entry point, to avoid ambiguity. This main method can then call any number of other methods. I assume you can call a second method "main", but using a different signature. However, to avoid confusion, I would recommend you don't do this.


How can you force the garbage collector to run?

You can't force it but you call System.gc(), which is a "hint" to the runtime engine that now might be a good time to run the GC. But garbage collection using this method is not guaranteed to be done immediately. there is another way to explicitly call the gc(). this method is also define in Runtime class of package java.lang. But u can not create a direct object of class Runtime like Runtime a = new Runtime(); //wrong For that u have to call the method getRuntime() which is static and it is also define in Runtime class the way to create object is Runtime run; //right run = Runtime.getRuntime(); //right now u can call the gc() through the "run " Object. like run.gc(); //right

Related Questions

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.


Can the run method be called directly to start a thread?

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.


How do you call repaint using thread in java applet?

You call the repaint method the same way you'd call any other method from a thread. final Applet appletToRepaint; new Thread() { public void run() { appletToRepaint.repaint(); } }.start();


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


Can the void run method be declared as private?

Sure you can. The run method is just another java method that you can modify to be private or protected or have return types as Strings etc. But, this run method will not be invoked when you start the thread. Only the default public void run() will be invoked when you do a thread.start()


How can you start Call of Duty 1?

Well first you have to buy it, then run it, then you can start it


How do you call a method in Java?

It's when you take a object and run some code in the class of that object.


Can you create object in java at run time?

Yes, you just have to implement a method that creates a new object, and call it.


Can you declare more than one single main method in java program why?

It doesn't really make sense. The JVM needs to know where to start running the program; that's what the "main" method is for. A class - one that can be run directly - needs to have a single entry point, to avoid ambiguity. This main method can then call any number of other methods. I assume you can call a second method "main", but using a different signature. However, to avoid confusion, I would recommend you don't do this.


How can you force the garbage collector to run?

You can't force it but you call System.gc(), which is a "hint" to the runtime engine that now might be a good time to run the GC. But garbage collection using this method is not guaranteed to be done immediately. there is another way to explicitly call the gc(). this method is also define in Runtime class of package java.lang. But u can not create a direct object of class Runtime like Runtime a = new Runtime(); //wrong For that u have to call the method getRuntime() which is static and it is also define in Runtime class the way to create object is Runtime run; //right run = Runtime.getRuntime(); //right now u can call the gc() through the "run " Object. like run.gc(); //right


What is method call in java?

Calling a method in Java is when you run code associated with a specific class, using the name of an instance object of a class, followed by the dot operator, followed by the name of the method, followed by the arguments of the method, enclosed in parentheses.


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().