0

I have two methods in a class and one is synchronized and other one is non-synchronized. When I am calling these methods with two different threads , I am seeing the execution becomes serial not parallel .

I am confused , as per theory r.processSomething() should not wait for r.doSomething() to complete its execution.

If anyone has some idea on executing two methods(synchronized and non-synchronized) simultaneously. Please share.

class LockObjectDemo{

    public static void main(String[] args){

        SharedResource r = new SharedResource();

        MyThread t1 = new MyThread(r);
        t1.start();

        MyThread t2 = new MyThread(r);
        t2.start();

    }
}


class MyThread extends Thread{
    private SharedResource r ;

    MyThread(SharedResource r){
        this.r = r;
    }

    public void run() {     
        try {
            r.doSomething(); // Only after completing this method , the other thread enters into the next method which is not locked
            r.processSomething();           
        } catch(Exception ex){
            System.out.println(ex);
        }

    }
}

class SharedResource{

    public void doSomething() throws Exception{
        synchronized(this){ 
            System.out.println("Doing Something -> "+Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println("Done Something->"+Thread.currentThread().getName());
        }
    }

    public void processSomething() throws Exception{
        System.out.println("Processing Something -> "+Thread.currentThread().getName());
        Thread.sleep(1000);
        System.out.println("Done Processing->"+Thread.currentThread().getName());
    }
}
JavaUser
  • 25,542
  • 46
  • 113
  • 139

2 Answers2

3

You have 2 things, taking ~5s, executing serially (because of synchronized).

When the first thread completes that 5s action, it starts something taking ~1s, and at the same time, the second thread starts the 5s action.

The first thread will complete the 1s action before the second thread completes the 5s action. Then the second thread executes the 1s action.

So, the 1s actions don't execute concurrently.

Graphically:

Thread 1:  |<----- 5s ----->|<- 1s ->|
Thread 2:                   |<----- 5s ----->|<- 1s ->|
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thanks Andy . Here my question is , why Thread1 is waiting 5s , it can start working on the other method in parallel . right? – JavaUser Apr 17 '19 at 13:16
  • 1
    @JavaUser: It's waiting 5s because you have a sleep(5000) in the doSomething method. – President James K. Polk Apr 17 '19 at 13:18
  • Meanwhile , it can execute the other method as it is not synchronized . Am I correct? – JavaUser Apr 17 '19 at 13:20
  • @JavaUser: No. Within a thread, execution proceeds serially. First doSomething is executed, then processSomething. – President James K. Polk Apr 17 '19 at 13:22
  • Ok got it James , can you please share me any example reference where a thread executes both synchronized and non-synchronized methods simultaneously. – JavaUser Apr 17 '19 at 13:25
  • @JavaUser reverse the lengths of the sleeps, so the synchronized method takes 1s and the non-synchronized method takes 5s. – Andy Turner Apr 17 '19 at 13:30
  • 2
    @JavaUser, a thread can *never* execute two methods simultaneously. It doesn't matter whether they are synchronized or not. ***A*** thread always executes program statements one-by-one, in the order that they are written. The only way that any method calls can overlap is if one thread executes one of them while a _different_ thread is executing the other one. – Solomon Slow Apr 17 '19 at 14:44
0

Further analysis to the above solution shared by "@Andy Turner".

In this problem we have to take into account following two things.

1) Both MyThread - t1 and t2 share the same SharedResource r. This will limit the execution of "doSomething", thus execute will happen in serial manner. Depending upon whichever thread is picked first the other one has to wait till the first thread execution is completed successfully. i.e. sleep for 5 sec.

For this problem lets assume

a) Thread t1 first started running i.e. t1.doSomething()

b) None of the other threads i.e. t1 and t2 can execute any other methods due to the fact that t2 is depending on synchronization lock and t1 is waiting for the first method "doSomething" to finish before it can start "processSomething".

2) Once the first thread (t1) "doSomething" is executed successfully, both the thread are in position to execute simultaneously.

Here the threads will be executed as commented by "@Andy Turner" above.

Mayank Maria
  • 146
  • 5