0

In the following program, somehow the program doesn't end. When i debugged, during inspecting the lock variable was showing that the lock was being held by the Producer thread as follows

java.util.concurrent.locks.ReentrantLock@55584367[Locked by thread pool-1-thread-1] java.util.concurrent.locks.ReentrantLock$NonfairSync@5b9fb7bc[State = 8, empty queue]

public class LocksTest {

    public static void main(String[] args) {
        List<String> contents = new ArrayList<>();
        Lock lock = new ReentrantLock();
        String greenColor = "\u001B[32m";
        String yellowColor = "\u001B[33m";
        String redColor = "\u001B[31m";
        MyProducer myProducer = new MyProducer(greenColor, contents, lock);
        MyConsumer myConsumer1 = new MyConsumer(yellowColor, contents, lock);
        MyConsumer myConsumer2 = new MyConsumer(redColor, contents, lock);

        ExecutorService service = Executors.newFixedThreadPool(3);
        service.execute(myProducer);
        service.execute(myConsumer2);
        service.execute(myConsumer1);
// new Thread(myProducer).start();
// new Thread(myConsumer1).start();
// new Thread(myConsumer2).start();
        service.shutdown();
    }
}

class MyProducer implements Runnable {

    private String color;
    private List<String> contents;
    private Lock lock;

    public MyProducer(String color,
                      List<String> contents,
                      Lock lock) {
        this.color = color;
        this.contents = contents;
        this.lock = lock;
    }

    @Override
    public void run() {

        int count = 0;
        while (true) {
            if (lock.tryLock()) {
                try {
                    contents.add("" + ++count);
                    System.out.println(color + "Adding " + count);
                    if (count == 5) {
                        System.out.println(color + "Adding " + "EOF");
                        contents.add("EOF");
                        break;
                    }

                    try {
                        Thread.sleep(1000l);
                    } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();
                    }

                } finally {
                    lock.unlock();
                }
            }
        }
    }
}

class MyConsumer implements Runnable {

    private String color;
    private List<String> contents;
    private Lock lock;

    public MyConsumer(String color,
                      List<String> contents,
                      Lock lock) {
        this.color = color;
        this.contents = contents;
        this.lock = lock;
    }

    @Override
    public void run() {
        String value = null;
        while (true) {
            if (lock.tryLock()) {
                try {
                    if (!contents.isEmpty()) {
                        Iterator<String> stringItr = contents.iterator();

                        while (stringItr.hasNext())
                            value = stringItr.next();
                        System.out.println(color + "Consuming " + value);
                        stringItr.remove();
                    }
                    if ("EOF".equals(value)) {
                        break;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}

Here are the results :

[32mAdding 1
[33mConsuming 1
[32mAdding 2
[33mConsuming 2
[32mAdding 3
[33mConsuming 3
[32mAdding 4
[33mConsuming 4
[32mAdding 5
[32mAdding EOF
[33mConsuming EOF
[31mConsuming 5

and

[32mAdding 1
[32mAdding 2
[32mAdding 3
[32mAdding 4
[32mAdding 5
[32mAdding EOF
[33mConsuming EOF
[31mConsuming 5
[31mConsuming 4
[31mConsuming 3
[31mConsuming 2
[31mConsuming 1

While the results are as expected, i am not sure why the program doesn't terminate ? Secondly, can a terminated thread hold the lock (I observed this strange behavior with the traditional way of using threads which is commented) ?!

Adithya
  • 2,923
  • 5
  • 33
  • 47

1 Answers1

3

Well, consumer and producer sharing a lock is a little bit strange to me, but your problem is that the producer puts a single EOF and you've got 2 consumers. So a consumer(not your producer) can not break out the while loop.

grape_mao
  • 1,153
  • 1
  • 8
  • 16