Suppose I have the following thread:
public class MyThread {
public void run() {
while (true) {
// do something forever
}
}
}
Then I instantiate the thread as follows:
MyThread thread = new MyThread();
What happens if I now call
thread.performSomeFunction()
Specifically, how does performSomeFunction interact with the infinite loop above? Does it have to wait for the loop to sleep? Or can they both run “concurrently”?
If your
thread.performSomeFunction()is called from another thread, it does not have to contend with the infinite loop that is being run in therun()method. In this case, yourMyThreadinstance is treated like another object that can have methods called on it.Note that your infinite loop will not start until you start your
threadinstance.You can test this out by putting the following line in both the
run()method and yourperfomrSomeFunction()method:and replace the
[METHOD NAME]with the actual method name.