Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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,我想在另一个类B中使用在类A的实例x上创建的线程。 我已经在下面的评论中以更好的方式陈述了我的问题 我有这样的想法: Class A implements Runnable{ public static int num; public void setNum(int i) { num = i; } public int getNum() { return num; } public void run(){ while(true){} //I want to kee

我想在另一个类B中使用在类A的实例x上创建的线程。 我已经在下面的评论中以更好的方式陈述了我的问题

我有这样的想法:

Class A implements Runnable{
  public static int num;

  public void setNum(int i) { num = i; }

  public int getNum() { return num; }

  public void run(){
     while(true){} //I want to keep this thread running continuously
  }
}

Class B{
 A a;

 //I will call this method in class C to use the same instance of class A 
 public A getInstanceOfA() { return a; } 

 public static void main(String[] args){
   a = new A();
   Thread t = new Thread(a);
   t.start();

   a.setNum(5);
   System.out.println(a.getNum()); //getting output as 5. Okay as Expected. 
  }
 }

 class C{
  A a;
  public static void main(String[] args){
   a = getInstanceOfA();

   System.out.println(a.getNum()); 
   //Here I'm getting output 0 not 5 why? As Thread created on instance a is 
   //already running, and also I am using the same instance of class A 
   //so I should get the updated value 5, but getting 0. Why it is re-initializing num?

  }
 }
请帮忙。谢谢

I am not giving you the exact code but you should do it something like this.

Class B{
 A a;


public B(){
initialize();

}


 //I will call this method in class C to use the same instance of class A 
 public A getInstanceOfA() { return a; } 

// this method should not be main
 public void initialize{
   a = new A();
   Thread t = new Thread(a);
   t.start();

   a.setNum(5);
   System.out.println(a.getNum()); //getting output as 5. Okay as Expected. 
  }
 }

 class C{
  A a;
  public static void main(String[] args){

   B b = new B();
   a = b.getInstanceOfA();

   System.out.println(a.getNum()); 
   //Here I'm getting output 0 not 5 why? As Thread created on instance a is 
   //already running, and also I am using the same instance of class A 
   //so I should get the updated value 5, but getting 0. Why it is re-initializing num?

  }
 }
您也可以使用Singleton


您有两种主要方法吗?你是说你想在应用程序实例之间共享一个对象吗?
你有两个主要方法
我的意思是你正在运行哪一个?你确定你已经准备好多线程了吗?好的,我想同时运行B和C的main方法。你可以一个接一个地运行,但不能同时运行它们。但我的要求是我应该能够同时运行B和C的main()方法。好吧,如果我在B和C中都使用main(),那么main()线程每次都会为B和C创建单独的上下文吗,这就是为什么我没有得到num的更新值的原因?我说得对吗?请纠正我。如果你认为这是答案,那么就这样做。我引用Stack Overflow中的话,只有Java程序具有单入口点,这是通过main方法实现的。因此,在单个项目中,只有一个类应该具有main方法,并且编译器在运行它时会查找该方法。如果一个接一个地运行主管道,它们之间就没有关系。是的,它们是独立的上下文,上一个上下文已被删除。很好,非常感谢。我需要改变我的要求。