Cli*_*ote 3 java multithreading asynchronous
我正在启动一个无限循环的线程,直到某个事件发生.问题是,我想启动这个线程,然后返回到我的程序的正常执行.但是,在启动线程后,代码似乎卡住了.
码:
public void init()
{
Runnable thread = new Runnable()
{
public void run()
{
while(something)
{
//do something
}
}
};
System.out.println("Starting thread..");
new Thread(thread).run();
System.out.println("Returning");
return;
}
Run Code Online (Sandbox Code Playgroud)
当我开始这个时,我得到输出"Starting thread"但是我没有得到"返回",直到run()stop中的while循环的条件为真.
有什么想法我可以让它异步工作吗?
Rei*_*eus 13
使用start而不是run开始Thread.后者只是run同步调用该方法
new Thread(thread).start();
Run Code Online (Sandbox Code Playgroud)
阅读:定义和启动线程