say I have a class called "Example"
inside "Example" I have an inner class which is a runnable
I execute the runnable inside "Example"
public class Example {
public Example() {
//executing the runnable here
}
private void a() {
}
public void b() {
}
public class RunMe implements Runnable {
public void run() {
a();
b();
}
}
}
what happens here assuming that Example runs on the main thread?
does a and b run from the RunMe thread or the main thread?
does it matter that a is private and b is public?
You missed out the key piece of code to enable us to answer your question - the constructor.
If your constructor looks like this:
public Example() {
(new Thread(new RunMe())).start();
}
Then a() and b() will run on the thread you just created (the "RunMe" thread as you call it).
However, if your constructor looks like this:
public Example() {
(new RunMe()).run();
}
Then you're not actually running it on another thread (you're just calling a method in another class, it just happens to be called 'run'), and so a() and b() will run on the 'main' thread.
The private/public thing is irrelevant here because RunMe is an inner class so can access even private methods of Example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With