Call the method of Java class which implements runnable after creating its thread object
java, multithreading, producer-consumer, runnable
Solution
You will end up calling `start()` on `thread1`.
`SomeClass` will override `run()` method which in turn need to call `display()` method.
This way when you call `start()`, run method of `SomeClass()` object will be invoked and display() method will be executed.
Example:
public class SomeClass implements Runnable {
private List yourArrayList;
public void run() {
display();
}
public void display() {
//Your display method implementation.
}
public List methodToGetArrayList()
{
return yourArrayList;
}
}
Update:
SomeClass sc = new SomeClass()
Thread thread1 = new Thread(sc);
thread1.join();
sc.methodToGetArrayList();
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.
Problem
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?