Java线程:请澄清这种理解 [英] Java Thread: Please clarify this understanding
问题描述
public class Qn {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
// recipient - the person who greets
public synchronized void sayHi(Friend recipient) {
System.out.format("%s" + " said hi to %s %n",
this.name, recipient.getName());
}
}
public static void main(String[] args) {
final Friend john = new Friend("John");
final Friend peter = new Friend("Peter");
new Thread(new Runnable() {
public void run() {
john.sayHi(peter);
}
}, "thread1").start();
}
}
问题:
如果理解正确,请澄清以下内容
Please clarify the ones below if understanding is correct
-
调用
john.sayHi()
,线程thread1
已获取john的内在
对象按顺序访问
锁定joH
对象的sayHi()
方法。
When invoking
john.sayHi()
, the Threadthread1
has acquired intrinsic lock ofjohn
object in-order to access thesayHi()
method ofjohn
object.
线程 thread1
在JVM中独立运行。
The Thread thread1
is running independently in JVM.
我在线阅读这些陈述,不确定它们的含义! [线程如何在对象上运行!!! Infact Threads执行代码,对吗?]
I read these statements online, not sure what they mean! [How can a thread run on objects!!! Infact Threads execute the code, correct?]
-
线程
thread1
未在JVM内的任何其他对象上运行。
The thread
thread1
is not running on any other object inside the JVM.
线程永远不会在任何对象上运行。线程永远不会被对象执行。
线程永远不会在任何其他线程上运行。线程总是直接在JVM中运行。
A thread never runs on any object. A thread is never executed by an object. A thread never runs on any other thread. A thread always run directly in JVM.
推荐答案
当调用
john.sayHi()
时,线程thread1
已获得的内部锁定john
对象按顺序访问john对象的sayHi()
方法。
When invoking
john.sayHi()
, the Threadthread1
has acquired intrinsic lock ofjohn
object in-order to access thesayHi()
method of john object.
更准确地说:
当调用 john.sayHi()
时,线程在$ sayHi
。它将在方法退出时释放锁。
When invoking john.sayHi()
, the Thread thread1
will wait until it can acquire a lock on john
before executing sayHi
. Once it has obtained the lock, it will execute sayHi
. It will release the lock when the method exits.
线程
thread1
正在运行独立于JVM。
The Thread
thread1
is running independently in JVM.
独立于什么?其他线程?是的,直到它试图获得锁定。此时,它可能会受到其他线程的阻碍。当它有一个锁时,它可以阻止其他线程。
Independently of what? Other threads? Yes, until it tries to obtain a lock. At that point, it can be impeded by other threads. When it has a lock, it can impede other threads.
线程
thread1
没有在JVM内的任何其他对象上运行。
The thread
thread1
is not running on any other object inside the JVM.
线程在CPU上运行,而不是在对象上运行。
Threads run on CPUs, not objects.
您是否询问某个线程是否可以并行执行多个方法?如果是这样,答案是否定的。
Are you asking if a thread can execute more than one method in parallel? If so, the answer is no.
线程永远不会在任何对象上运行。
A thread never runs on any object.
线程在CPU上运行,而不是在对象上运行。
Threads run on CPUs, not objects.
线程永远不会被对象执行。
A thread is never executed by an object.
线程不会被任何东西执行。线程执行代码。
Threads aren't executed by anything. Threads execute code.
线程永远不会在任何其他线程上运行。
A thread never runs on any other thread.
线程在CPU上运行,而不是线程。
Threads run on CPUs, not threads.
线程总是直接在JVM中运行。
A thread always run directly in JVM.
JVM有一个运行该线程的虚拟CPU。
The JVM has a virtual CPU on which the thread runs.
这篇关于Java线程:请澄清这种理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!