静态方法如何获取实例详细信息或执行实例特定操作
How can a static method get instance details or do instance specific actions
下面的代码显然不会编译。
public class Machine {
public int id;
static Machine staticMethod() { return this; } //compilation error - "Cannot use this in a static context"
maskId() { }
}
因此不可能从 staticMethod() 到
(1) 获取特定于 Machine Object
的任何实例的信息
(2) 执行特定于 Machine object
的任何实例的操作
除非我错过了一些非常简单的东西。
线程Class的静态方法如何处理特定线程的actions/information
以下是 java 文档 Thread
中此类方法的签名
static Thread currentThread() //Returns a reference to the currently executing thread object.
static void dumpStack() //Prints a stack trace of the current thread to the standard error stream.
我是不是漏掉了什么。或者这些方法(或 Thread Class)不同。像本机实现等或以往任何时候都能够确定调用实例。
Thread
class不同,因为它处理的是核心 JVM(通常是操作系统)构造。 In OpenJDK, it's not only native
but also annotated with a hint that says that it's built in to the JRE itself instead of provided by an external library.
一个帖子可以问;我是哪个线程?在任何时候。它不需要像大多数值那样将 this 作为参数传递给它。有一个 static
native
方法给出当前线程,从那里这个对象可以调用实例方法。
我认为这是由调度程序完成的,
我在这里提到 class 结构(这是我的看法,它是如何工作的)
class Thread{
static currentThread();
}
Class Scheduler{
//it has queue of all threads and also has track on current running thread
//queue = {Thread1,Thread2,Thread3,Thread4,Thread5}
// let thread3 is running, whenever a call to Thread3.currentThread come up
//some how jvm get instance of Thread3 from this queue and returns.
}
下面的代码显然不会编译。
public class Machine {
public int id;
static Machine staticMethod() { return this; } //compilation error - "Cannot use this in a static context"
maskId() { }
}
因此不可能从 staticMethod() 到
(1) 获取特定于 Machine Object
的任何实例的信息
(2) 执行特定于 Machine object
除非我错过了一些非常简单的东西。
线程Class的静态方法如何处理特定线程的actions/information
以下是 java 文档 Thread
static Thread currentThread() //Returns a reference to the currently executing thread object.
static void dumpStack() //Prints a stack trace of the current thread to the standard error stream.
我是不是漏掉了什么。或者这些方法(或 Thread Class)不同。像本机实现等或以往任何时候都能够确定调用实例。
Thread
class不同,因为它处理的是核心 JVM(通常是操作系统)构造。 In OpenJDK, it's not only native
but also annotated with a hint that says that it's built in to the JRE itself instead of provided by an external library.
一个帖子可以问;我是哪个线程?在任何时候。它不需要像大多数值那样将 this 作为参数传递给它。有一个 static
native
方法给出当前线程,从那里这个对象可以调用实例方法。
我认为这是由调度程序完成的, 我在这里提到 class 结构(这是我的看法,它是如何工作的)
class Thread{
static currentThread();
}
Class Scheduler{
//it has queue of all threads and also has track on current running thread
//queue = {Thread1,Thread2,Thread3,Thread4,Thread5}
// let thread3 is running, whenever a call to Thread3.currentThread come up
//some how jvm get instance of Thread3 from this queue and returns.
}