I have a java class
SomeClass implements Runnable
Which has a method display().
When I create a thread of this class
Thread thread1 = new Thread(new SomeClass());
Now how I can call the display() method using the thread instance?
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.
You will end up calling
start()onthread1.SomeClasswill overriderun()method which in turn need to calldisplay()method.This way when you call
start(), run method ofSomeClass()object will be invoked and display() method will be executed.Example:
Update:
NOTE: Example is to illustrate the concept, there may be syntax errors.
If you don’t use join(), as Andrew commented, there may be inconsitence in results.