Runnable
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
The Runnable
interface should be implemented by any
class whose instances are intended to be executed by a thread. The
class must define a method of no arguments called run
.
This interface is designed to provide a common protocol for objects that
wish to execute code while they are active. For example,
Runnable
is implemented by class Thread
.
Being active simply means that a thread has been started and has not
yet been stopped.
In addition, Runnable
provides the means for a class to be
active while not subclassing Thread
. A class that implements
Runnable
can run without subclassing Thread
by instantiating a Thread
instance and passing itself in
as the target. In most cases, the Runnable
interface should
be used if you are only planning to override the run()
method and no other Thread
methods.
This is important because classes should not be subclassed
unless the programmer intends on modifying or enhancing the fundamental
behavior of the class.
Public Method Summary
abstract
void
|
run()
When an object implementing interface Runnable is used
to create a thread, starting the thread causes the object's
run method to be called in that separately executing
thread.
|
Public Methods
public
abstract
void
run
()
When an object implementing interface Runnable
is used
to create a thread, starting the thread causes the object's
run
method to be called in that separately executing
thread.
The general contract of the method run
is that it may
take any action whatsoever.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eRunnable\u003c/code\u003e interface provides a standardized way for objects to execute code in a separate thread.\u003c/p\u003e\n"],["\u003cp\u003eAny class intending to be executed by a thread should implement \u003ccode\u003eRunnable\u003c/code\u003e and define a \u003ccode\u003erun()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eRunnable\u003c/code\u003e allows a class to be active without subclassing \u003ccode\u003eThread\u003c/code\u003e by passing itself as the target to a new \u003ccode\u003eThread\u003c/code\u003e instance.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003erun()\u003c/code\u003e method, which contains the code to be executed in the thread, has no arguments and can perform any action.\u003c/p\u003e\n"]]],["The `Runnable` interface is for classes whose instances will be executed by a thread. Implementing classes must define a `run()` method, which is invoked when the thread starts. `Runnable` allows for code execution without subclassing `Thread` by passing itself as the target to a `Thread` instance. `Runnable` is preferred when only `run()` is overridden. Key subclasses include `ForkJoinWorkerThread`, `FutureTask`, `RunnableFuture`, `RunnableScheduledFuture`, and `TimerTask`. The `run()` method can perform any action.\n"],null,["# Runnable\n\npublic interface **Runnable** \n\n|---|---|---|\n| Known Indirect Subclasses [ForkJoinWorkerThread](../../../reference/java/util/concurrent/ForkJoinWorkerThread.html), [FutureTask](../../../reference/java/util/concurrent/FutureTask.html)\\\u003cV\\\u003e, [RunnableFuture](../../../reference/java/util/concurrent/RunnableFuture.html)\\\u003cV\\\u003e, [RunnableScheduledFuture](../../../reference/java/util/concurrent/RunnableScheduledFuture.html)\\\u003cV\\\u003e, [TimerTask](../../../reference/java/util/TimerTask.html) |------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ForkJoinWorkerThread](../../../reference/java/util/concurrent/ForkJoinWorkerThread.html) | A thread managed by a [ForkJoinPool](../../../reference/java/util/concurrent/ForkJoinPool.html), which executes [ForkJoinTask](../../../reference/java/util/concurrent/ForkJoinTask.html)s. | | [FutureTask](../../../reference/java/util/concurrent/FutureTask.html)\\\u003cV\\\u003e | A cancellable asynchronous computation. | | [RunnableFuture](../../../reference/java/util/concurrent/RunnableFuture.html)\\\u003cV\\\u003e | A [Future](../../../reference/java/util/concurrent/Future.html) that is [Runnable](../../../reference/java/lang/Runnable.html). | | [RunnableScheduledFuture](../../../reference/java/util/concurrent/RunnableScheduledFuture.html)\\\u003cV\\\u003e | A [ScheduledFuture](../../../reference/java/util/concurrent/ScheduledFuture.html) that is [Runnable](../../../reference/java/lang/Runnable.html). | | [TimerTask](../../../reference/java/util/TimerTask.html) | A task that can be scheduled for one-time or repeated execution by a Timer. | |||\n\nThe `Runnable` interface should be implemented by any\nclass whose instances are intended to be executed by a thread. The\nclass must define a method of no arguments called `run`.\n\n\nThis interface is designed to provide a common protocol for objects that\nwish to execute code while they are active. For example,\n`Runnable` is implemented by class `Thread`.\nBeing active simply means that a thread has been started and has not\nyet been stopped.\n\n\nIn addition, `Runnable` provides the means for a class to be\nactive while not subclassing `Thread`. A class that implements\n`Runnable` can run without subclassing `Thread`\nby instantiating a `Thread` instance and passing itself in\nas the target. In most cases, the `Runnable` interface should\nbe used if you are only planning to override the `run()`\nmethod and no other `Thread` methods.\nThis is important because classes should not be subclassed\nunless the programmer intends on modifying or enhancing the fundamental\nbehavior of the class. \n\n##### See Also\n\n- [Thread](../../../reference/java/lang/Thread.html)\n- [Callable](../../../reference/java/util/concurrent/Callable.html) \n\n### Public Method Summary\n\n|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [run](../../../reference/java/lang/Runnable.html#run())() When an object implementing interface `Runnable` is used to create a thread, starting the thread causes the object's `run` method to be called in that separately executing thread. |\n\nPublic Methods\n--------------\n\n#### public abstract void\n**run**\n()\n\nWhen an object implementing interface `Runnable` is used\nto create a thread, starting the thread causes the object's\n`run` method to be called in that separately executing\nthread.\n\n\nThe general contract of the method `run` is that it may\ntake any action whatsoever. \n\n##### See Also\n\n- [Thread.run()](../../../reference/java/lang/Thread.html#run())"]]