从另一个线程调用线程的方法是否会暂停该线程的执行?

时间:2022-05-25 06:59:59

Say I call a thread's method from inside another thread as such

假设我从另一个线程内部调用线程的方法

public static void main(String[] args)
{
    //do something
    TestThread thread = new TestThread();
    thread.start();
    thread.doSomething();
    //Part A
}

class TestThread extends Thread
{
   public void run()
   {
       //do something
   }

    public void doSomething()
    {
        //Do something
    }
}`

Will the program do everything in the doSomething() method before moving onto partA or will it start running the doSomething() method then move straight onto part A?
I'm making an application with a server serving multiple clients and want the server to be able to quickly move through the requests, so it should be once it is sent it moves on rather than waiting for the client to process the instruction

程序在转移到partA之前是否会在doSomething()方法中执行所有操作,还是会开始运行doSomething()方法然后直接转到A部分?我正在使用服务器服务多个客户端的应用程序,并希望服务器能够快速移动请求,因此它应该是一旦发送它继续运行而不是等待客户端处理指令

1 个解决方案

#1


2  

No. The main thread will run doSomething() while the thread-thread will be running run() as usual.

不会。主线程将运行doSomething(),而线程将像往常一样运行run()。

Just because doSomething() is in a class that extends Thread doesn't mean that there's anything special about it as a method.

仅仅因为doSomething()在一个扩展Thread的类中并不意味着它作为一种方法有什么特别之处。

#1


2  

No. The main thread will run doSomething() while the thread-thread will be running run() as usual.

不会。主线程将运行doSomething(),而线程将像往常一样运行run()。

Just because doSomething() is in a class that extends Thread doesn't mean that there's anything special about it as a method.

仅仅因为doSomething()在一个扩展Thread的类中并不意味着它作为一种方法有什么特别之处。