- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图用自定义对象创建一个新线程,然后从主线程调用这个自定义对象方法。这个想法是主线程可以继续做其他事情,而自定义对象继续在第二个线程中工作:
public class Multithreading {
public static void main(String[] args) {
Multithreading mt = new Multithreading();
mt.multiTest();
}
public void multiTest() {
SomeObject someObject = new SomeObject();
Thread thread = new Thread(someObject);
thread.setDaemon(true);
thread.start();
someObject.startit();
int i = 0;
while (i < 3) {
try {
System.out.println("this is the main thread being busy");
Thread.sleep(3000);
i += 1;
} catch (InterruptedException e) {
}
}
}
class SomeObject implements Runnable {
public void sayHello() {
System.out.println("this is the second thread being busy");
}
public void startit() {
int i = 0;
while (i < 3) {
try {
sayHello();
Thread.sleep(3000);
i += 1;
} catch (InterruptedException e) {
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
}
输出是:
this is the second thread being busy
this is the second thread being busy
this is the second thread being busy
this is the main thread being busy
this is the main thread being busy
this is the main thread being busy
应该更像这样:
this is the second thread being busy
this is the main thread being busy
this is the second thread being busy
this is the main thread being busy
this is the second thread being busy
this is the main thread being busy
所以主线程被阻塞,直到方法完成。主线程是否在等待第二个线程中的 someObject.startit()
完成(返回类型为 void,我认为情况不会如此)?还是它在第一个线程中执行,因此阻塞了它?
我知道使用下面的代码我可以在另一个线程中执行 someObject.startit()
,但每次都会从头开始创建,我无法承受线程创建的开销:
new Thread(() -> {someObject.startit();}).start();
一个线程如何在不阻塞的情况下从另一个线程中的对象调用方法?
最佳答案
Is the main thread waiting for completion of someObject.startit() in the second thread(Being void as return type, I would think that this would not be the case)? Or is it executed in the first thread, therefore blocking it?
当您在multiTest
中直接调用someObject.startit()
时,它会在第一个调用线程中执行。
了解 Runnbale
不是 Thread
非常重要,Runnbale
只是普通对象,除了它的 run
方法将在您使用它创建并启动一个新的 Thread
时执行,如下所示:
new Thread(new Runnable()).start;
所以,实际上,在这种情况下与线程阻塞无关。您可以将 startit()
移动到 run
方法,这样它将由第二个线程执行:
@Override
public void run() {
startit();
}
并且,为了避免线程创建开销,您可以使用线程池来执行它:
ExecutorService executorService = Executors.newCachedThreadPool(); // It should a singlton
// ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
executorService.execute(() -> {
someObject.startit();
});
关于java - 从另一个线程调用方法时主线程被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50024888/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!