Multithreading

When dealing with multiple threads, is it better to extend the Thread class or implement Runnable?
10 Replies
JavaBot
JavaBot5d ago
This post has been reserved for your question.
Hey @DueShoulder! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
bassus
bassus5d ago
I'd say Runnable, bc you can inherite from only one class but you can implement a lot of interfaces But in general there is no better or worse option as far as i know
valer
valerOP5d ago
How come when you implement runnable you don't have to extend Thread
bassus
bassus5d ago
Yeah, that is the point^^ Or was that a question?:Huh:
valer
valerOP5d ago
It is a question hahaha
bassus
bassus5d ago
Okay 😂 Consider this to be your class implementing Runnable:
public class TheRizzler implements Runnable {
//Write fields, constructors, methods, whatever
public void run() {
//Write what your Thread should run, for example:
System.out.println("Hawk Tuah Skibidi");
}
}
public class TheRizzler implements Runnable {
//Write fields, constructors, methods, whatever
public void run() {
//Write what your Thread should run, for example:
System.out.println("Hawk Tuah Skibidi");
}
}
Now later in your code you can execute your Thread, that is the run method you defined in your Runnable implementation, like this:
TheRizzler sigma = new TheRizzler();
Thread ohio = new Thread(sigma);
ohio.start();
TheRizzler sigma = new TheRizzler();
Thread ohio = new Thread(sigma);
ohio.start();
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Madjosz
Madjosz4d ago
Brainrot-infused coding tips 😅 Also please note Java naming conventions: It should be class TheRizzler.
bassus
bassus4d ago
Fair I'm so sad Java has no #define, there would be marvellous brainrot. I would mogg you all to Ohio.
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?