Running a new thread inside a synchronized block

If I create a new thread inside a synchronized block, will the block remain locked until the thread completes? If not, until when will it remain blocked?

String sLine;
onClick(String line){
    synchronized (lock) {
        sLine = line;
        new Thread(new Runnable() {
            @Override
            public void run() {
                 doProcessing(Sline);    
        }).start(); 
    }
}
+5
source share
2 answers

It will only remain blocked if the code join()d is with a newly created thread, waiting for it to complete. Since no join(), the lock will be released after the call ends start().

+7
source

the thread does not have a separate life. the synchronized block will be blocked only to the starting point of the flow in the above case.

0
source

All Articles