top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: After locking of first thread how other thread are accessing the list?

+2 votes
941 views
import java.util.ArrayList;
import java.util.List;

public class ThreadB extends Thread {
    private static List<String> list1   = new ArrayList<String>();
    private static List<String> list2   = new ArrayList<String>();
    private static final Object mutex   = new Object();

    public void run() {
        try {
            sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (mutex) {
            list1.add("Hello");
            System.out.println("Added for 1   " + list1.size());
            list2.add("Added for 2");
            System.out.println("Added for 2   " + list2.size());
            mutex.notify();
        }
    }

    public static void main(String[] args) {
        ThreadB a1 = new ThreadB();
        ThreadB a2 = new ThreadB();
        a1.start();
        a2.start();
        new ThreadB().m1();    
    }

    private void m1() {         
        synchronized (list1) {
            System.out.println(list1.size());
            System.out.println(list2.size());
            try {
                list1.add("hello");
                System.out.println("m1  :::   " + list1.size());
                sleep(5000l);
                System.out.println("Oh mY God");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

Output is

0
0
m1  :::   1
Added for 1   2
Added for 2   1
Added for 1   3
Added for 2   2
Oh mY God

After locking list1, how other threads are going to access that list?

posted Dec 11, 2014 by Narayana Manohar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Gave a quick look (I may be wrong), you have 20001 unit of sleep for thread 1 and same for thread 2. So you are executing thread 1 completely  then notify and thread 2 is executed and after 50001 unit (master sleep) you came out.

Your threads are executed one after another which seems to be the intention of the program. Please correct me if I am wrong.

1 Answer

0 votes

1) In Java programming start from Main by creating Main Thread.
2) Main Thread creating two thread a1 and a2.
3) a1.start(); Statement invoked run method for thread a1 and make it sleep for 2000ms
4) a2.start(); Statement invoked run method for thread a2 and make it sleep for 2000ms
5) As of now Thread a1 and a2 are in sleep. Main Thread invokes and execute a line new ThreadB().m1(); and calls method m1()
Note: In synchronized block only one thread can execute.
6) Initially size of list1 and list2 is zero so it print size as 0 and add 'hello' to list1 then it print size as 1 and make Main thread sleep for 5000ms.
7) In between Thread a1 invokes first and print size of list1 as 2 (Main thread already added in List1) and list2 as 1.
8) Then Thread a2 invokes and print size of list1 as 3 and list2 as 2.
9) Later Main Thread again invokes and print 'Oh mY God'.

answer Dec 22, 2014 by Karthick.c
...