What is “runnable” in Java, in layman’s terms? I am an AP programming student in high school, whose assignment is to do research, or seek out from others what “runnable” is (we are just getting into OOP, and haven’t touched threads yet).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A Runnable is basically a type of class (Runnable is an Interface) that can be put into a thread, describing what the thread is supposed to do.
The Runnable Interface requires of the class to implement the method
run()like so:And then use it like this:
If you did not have the
Runnableinterface, the Thread class, which is responsible to execute your stuff in the other thread, would not have the promise to find arun()method in your class, so you could get errors. That is why you need to implement the interface.Advanced: Anonymous Type
Note that you do not need to define a class as usual, you can do all of that inline:
This is similar to the above, only you don’t create another named class.