How does run method in runnable work




















Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics.

Ethical Hacking. Once a thread is created, you can start it by calling the start method. Every thread has a name. If no name is specified then a new name is automatically chosen for the thread. Runnable interface is the primary template for any object that is intended to be executed by a thread. It defines a single method run , which is meant to contain the code that is executed by the thread.

Any class whose instance needs to be executed by a thread should implement the Runnable interface. The Thread class itself implements Runnable with an empty implementation of run method.

For creating a new thread, create an instance of the class that implements Runnable interface and then pass that instance to Thread Runnable target constructor. Anonymous classes enable you to make your code more concise. At first you may not notice anything because the Runnable 's run method is executed like you expected. However, it is NOT executed by the new thread you just created.

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. When you create a Java thread you can give it a name.

The name can help you distinguish different threads from each other. For instance, if multiple threads write to System.

Here is an example:. Notice the string "New Thread" passed as parameter to the Thread constructor. This string is the name of the thread. The name can be obtained via the Thread 's getName method.

You can also pass a name to a Thread when using a Runnable implementation. Here is how that looks:. Notice however, that since the MyRunnable class is not a subclass of Thread , it does not have access to the getName method of the thread executing it. The Thread. This way you can get access to the Java Thread object representing the thread executing a given block of code. Here is an example of how to use Thread. Once you have a reference to the Thread object, you can call methods on it.

For instance, you can get the name of the thread currently executing the code like this:. Here is a small example. First it prints out the name of the thread executing the main method.

This thread is assigned by the JVM. Each thread then prints its name out, and then stops executing. Note that even if the threads are started in sequence 1, 2, 3 etc. This is because the threads are in principle executing in parallel and not sequentially. This order does not have to be the same order in which they were started. A thread can pause itself by calling the static method Thread.

The sleep takes a number of milliseconds as parameter. The sleep method will attempt to sleep that number of milliseconds before resuming execution. Here is an example of pausing a Java thread for 3 seconds 3. The thread executing the Java code above, will sleep for approximately 10 seconds Example, Attention reader!

Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Output shows two active threads in the program — main thread and Thread-0, main method is executed by the Main thread but invoking start on RunnableImpl creates and starts a new thread — Thread What happens when Runnable encounters an exception? Example, import java. Skip to content.

Change Language.



0コメント

  • 1000 / 1000