Execution of new thread inside a synchronized block
Asked Answered
F

2

14

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(); 
    }
}
Fasciation answered 10/4, 2013 at 12:52 Comment(2)
duplicate - #5295565Somniferous
@Somniferous no. The question you reference covers the scope of this question, but is a different question.Bonds
A
18

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.

Agricola answered 10/4, 2013 at 12:54 Comment(1)
so in the above case it block would be locked only till the new thread has been created? so it won't wait till run() is completed?Fasciation
W
1

no thread has a separate life. synchronized block will be blocked only till starting point of the thread in above case.

Watcher answered 10/4, 2013 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.