1

I'm writing a small program to create 4 threads to do 4 tasks: increase value of every element in list, decrease value of odd elements and print out, remove last element in list every 20 ms, add element to list every 20 ms but thread 2 does not show output

public class MyClass {
    static volatile ArrayList<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

    Thread th1 = new Thread() {
        public void run() {
            synchronized (list) {
                while (true) {
                    if (list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            list.set(i, list.get(i) + 1);
                        }
                    }
                }
            }
        }
    };

    Thread th2 = new Thread() {
        public void run() {
            while (true) {
                synchronized (list) {
                    if (list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            if ((list.get(i) % 2) != 0) {
                                list.set(i, list.get(i) - 1);
                                System.out.println(list.get(i));
                            }
                        }
                    }
                }
            }
        }
    };

    Thread th3 = new Thread() {
        public void run() {
            while (true) {
                synchronized (list) {
                    if (list.size() > 0) {
                        list.remove(list.size() - 1);
                    }
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    Thread th4 = new Thread() {
        public void run() {
            for (int i = 0;; i++) {
                synchronized (list) {
                    list.add(i);
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    th4.start();
    th2.start();
    th1.start();
    th3.start();        
}
}

Is there anything wrong with my code?

1
  • 1
    @harpun a program doesn't end when the main method returns. It ends when all the non-daemon threads stop running.
    – JB Nizet
    Feb 1, 2015 at 15:16

1 Answer 1

2

The first thread locks the list and then enters an infinite loop. So the second one can never lock the list and do its job, since the first thread holds the lock forever.

3
  • Thanks, but after removing synchronized of thread 1, I got an IndexOutOfBoundsException, could you please help me to fix it Feb 1, 2015 at 15:25
  • 1
    You should not remove the synchronized of thread 1. You should put it inside the while loop rather than outside. That way, at each iteration, you leave the synchronized block and thus let other threads waiting on the lock to acquire it.
    – JB Nizet
    Feb 1, 2015 at 15:27
  • Also add some idle time in the thread one impl to yield the processor to the other threads. not mandatory but it will help.
    – LhasaDad
    Feb 1, 2015 at 16:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.