Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java多线程访问方法_Java_Multithreading - Fatal编程技术网

Java多线程访问方法

Java多线程访问方法,java,multithreading,Java,Multithreading,即使不创建Runner对象,我也可以从线程的run方法访问Runner类中的process方法吗?为什么 class Runner { public void process() { // some multithreaded code } } main() { Thread t1 = new Thread(new Runnable() { public void run() { process();

即使不创建Runner对象,我也可以从线程的run方法访问Runner类中的process方法吗?为什么

class Runner {
  public void process() {
    // some multithreaded code
  }
}

main() {
Thread t1 = new Thread(new Runnable() {
                public void run() {
                    process();
                }
           });
t1.start();        
}

如果将其设置为
静态
,则可以:

class Runner {
      public static void process() {
        // some multithreaded code
      }
    }
然后:


但是,如果没有
Runner.
限定或没有实例化,您将无法引用
process()
方法。这是因为
Java
是一种
面向对象的
语言。

如果运行程序的进程方法是静态的,您可以


如果没有实例化,就无法访问对象的非静态方法。您只需要在使该方法线程安全时进行尽职调查

你不能。有什么原因不想创建Runner的对象吗?我不认为我们“访问”方法,我也不认为这个语句对我来说有意义,而是我们访问对象。什么类是
main()
?为什么它没有参数?您所说的“多线程代码”是什么意思?如果main()方法在类运行程序中,则是。但是,如果不首先启动非静态方法,就不能从类外部调用它。
public static void main() {
    Thread t1 = new Thread(new Runnable() {
                public void run() {
                    Runner.process();
                }
           });
    t1.start();        
}