简体   繁体   中英

Which thread is responsible for what in Java

If i create a two objects that are run to two different threads in Java. If one of the objects calls a method in another object. Is the method run in the thread of the object that calls the method or the object that has the method in it?

The method is run in the the thread that calls it.

Classes or objects don't run in threads, but rather code does. So what matters is which thread an object's methods are called.

As Thilo states: classes and objects don't run at all.

Unless a thread is explicitly started or the method called runs tasks in another thread (Swing Worker, for instance), it's the same thread.

In any case, the code that starts the new thread runs in the current thread and instructs the JVM to start a thread.

Each thread is an independent line of execution independent of classes or objects(except for when multiple threads try to access the same object; we have locking there), and can access many classes and objects during its lifetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM