简体   繁体   English

从另一个类调用一个扩展Thread的类的方法

[英]Calling a method of a class which extends Thread, from another class

I know this is a bit naive question but I want to understand the basic working principle behind multi-threading in java. 我知道这是一个有点天真的问题,但我想了解java中多线程背后的基本工作原理。 Consider the following code and say A is executed in Main thread and it starts execution of another worker thread ,defined in class B. I want to know that can B.func1 called from A and run method of B, be executed in parallel or not? 考虑以下代码,并说A在主线程中执行,它开始执行另一个工作线程,在B类中定义。我想知道可以从A调用B.func1并运行B的方法,并行执行或不执行?

public class A {
    public static void main(String[] args) {
        B obj = new B();
        obj.start();
        obj.func1();
    }
}

public class B extends Thread {
    public B() {
        //constructor
    }
    public void run() {
        while(true) {
            //do somethings
        }
    }
    public void func1() {
        //do someotherthings
    }
}

There is no magic behind a method call. 方法调用背后没有魔力。 If you call method from a thread, it is called in exactly the same thread. 如果从线程调用方法,则在完全相同的线程中调用它。 So since obj.func1() is called from main , it will be run in the main thread. 因此,从main调用obj.func1() ,它将在主线程中运行。 It doesn't matter which class it belongs to or whether or not it extends Thread . 它属于哪个类或者是否扩展Thread无关紧要。

The new thread starts by executing run . 新线程从执行run开始。 Everything called from run and so on will be executed in parallel to main . 从run等调用的所有东西都将与main并行执行。

There are multiple issues with your code. 您的代码存在多个问题。 I have corrected them and added one more statement to print Thread Name in func1(). 我更正了它们并添加了一个语句来在func1()中打印线程名称。

Working code: 工作代码:

public class A {
    public static void main(String args[]){
        B obj = new B();
        obj.start();
        obj.func1();
    }
}

class B extends Thread{
    public B (){
        //constructor
    }
    public void run(){
        while(true){
            //do somethings
        }
    }
    public void func1 (){
        //do someotherthings
        System.out.println("Thread name="+Thread.currentThread().getName());
    }
}

output: 输出:

Thread name=main

Since you are directly calling func1() from main method (A.java) , you will get Thread name = main in output. 由于您直接从main方法(A.java)调用func1(),因此您将在输出中获得Thread name = main

If you add same print statement run() method, you will get output as : Thread name=Thread-0 如果添加相同的print语句run()方法,则输出为: Thread name=Thread-0

It's important to understand the difference between a thread and a Thread . 理解线程Thread之间的区别非常重要。

A thread is an independent execution of your code. 线程是代码的独立执行。 Often when we talk about how some method or another works we say things like, "It tests the variable x , and if x is less than zero it calls the foobar method..." 通常,当我们谈论某个方法或其他方法如何工作时,我们会说“它测试变量x ,如果x小于零,则调用foobar方法......”

Ok, but what is the "it" in that sentence? 好的,那句话中的“它”是什么? It is not the method. 不是方法。 Methods don't do anything. 方法什么都不 A method is just a list of instructions, like the list of chores that somebody left for their housemate to perform. 一个方法只是一个指令列表,比如有人留给他们的室友执行的家务清单。 The list doesn't do the chores, it's the housemate that does the work (or so we might hope). 这份清单不做家务,而是工作的室友(或许我们希望如此)。

The "it" is a thread . “它”是一个线程 Threads are entities in the operating system that execute methods (ie, they do the chores). 线程是操作系统中执行方法的实体(即,他们做家务)。

A Thread , on the other hand, is a Java object that your program can use to create and manage new threads . 另一方面, Thread是一个Java对象,程序可以使用它来创建和管理新线程 Your program creates a new Thread object by doing: 您的程序通过执行以下操作创建新的Thread对象:

thread t = new Thread(...);

[Oops! [哎呀! See what I just did? 看看我刚才做了什么? It's not your program , that does the work, it's your program's main thread , or maybe some other thread in your program. 这不是你的程序 ,它可以完成工作,它是程序的主线程 ,也可能是程序中的其他一些线程。 It's an easy thing to forget!] 这是一件容易忘记的事!]

Anyway, it subsequently creates the new thread by calling t.start(); 无论如何,它随后通过调用t.start();创建新线程 t.start();


Once you understand all that, then Sergey Tachenov's answer becomes obvious: Calling the methods of a Thread object really is no different from calling methods of any other kind of object. 一旦你理解了这一切,那么Sergey Tachenov的答案就变得很明显了:调用Thread对象的方法与调用任何其他类型的对象的方法没有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果一个方法属于另一个扩展Thread的类但从主线程调用,则该方法是由主线程还是子线程执行? (JAVA) - If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main or a child thread? (Java) 在Java中,如果我调用一个从另一个可运行对象扩展Thread的类,哪个线程将执行? - In Java, if I call a class that extends Thread from another runnable object, which Thread executes? 扩展Fragment的类的调用方法 - Call method from class which extends Fragment 从扩展视图的类中调用活动类中的变量或方法? - Calling a variable or method in the activity class from a class that extends view? 使用来自另一个类的jbutton调用paint和thread的main方法 - Calling a main method with paint and thread using a jbutton from another class 从另一个都属于同一类的方法中调用一个方法 - Calling a method from another method in which both are in the same class 调用从MainActivity类扩展Activity的Fragment扩展的类Fragment - Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity 在线程中从创建线程的类中调用方法 - Calling a method from the class that created a thread, in the thread 另一个与Arraylist相关的类的调用方法 - Calling method from another class which relates to Arraylist 从另一个类调用方法 - Calling a method from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM