在同步块内执行新线程
问题描述:
如果我在同步块中创建一个新线程,该块是否会保持锁定状态,直到线程执行完成为止?
如果没有,那么它何时会被锁定?
If i create a new thread inside a synchronized block, will the block remain locked till the thread execution is also complete? If not, then till when would it remain locked?
String sLine;
onClick(String line){
synchronized (lock) {
sLine = line;
new Thread(new Runnable() {
@Override
public void run() {
doProcessing(Sline);
}).start();
}
}
答
它只有代码 使用新创建的线程加入() d,等待它完成。由于没有 join()
,锁定将在 start()
的调用完成后释放。
It would only remained locked if the code join()
d with the newly created thread, thus waiting for it to finish. As there is no join()
the lock will be released after the call to start()
has completed.