0

Suppose I want my 20 consumers to remove elements from ConcurrentLinkedQueue until their time quantum expires.That is i want each consumer to just run only once until its time slice reaches and then die. Following is my code but it is not working and consumers are not quitting from loop. Obviously it means that isInterrupted() is not affected at time quantum expiry. How I can accomplish this task? Following is my consumer.

import java.util.concurrent.ConcurrentLinkedQueue;    
public class Consumer extends Thread{
    ConcurrentLinkedQueue<Object> queue;//=new ConcurrentLinkedQueue<Object>();    
    public Consumer(ConcurrentLinkedQueue<Object> queue){
        this.queue=queue;           
    }

    public void run(){

        while(!isInterrupted()){

            String s=(String)queue.poll();
            }
        System.out.println("Quitting");
    }
}

And this is the producer

import java.util.concurrent.ConcurrentLinkedQueue;
public class Producer extends Thread {
ConcurrentLinkedQueue<Object> queue;
public Producer(ConcurrentLinkedQueue<Object> queue){
        this.queue=queue;
    }
public void run(){
        while(true){
            queue.add("Hi");
        }
    }
}

And this is the main main class.

import java.util.concurrent.ConcurrentLinkedQueue;
public class ProducerConsumerTest {
    ConcurrentLinkedQueue<Object> queue=new ConcurrentLinkedQueue<Object>();
public ProducerConsumerTest(){
    Producer producer=new Producer(queue);
    producer.start();

    for(int i=1;i<=20;i++){
        Consumer consumer=new Consumer(queue);
        consumer.start();
        }
}
public static void main(String d[]){
    new ProducerConsumerTest();
}
}
Faisal Bahadur
  • 498
  • 3
  • 19
  • To be clear, you want each consumer to consume only one message from the queue and then terminate, yes? Seems a bit odd, that, but if you want that, then have the consumers wait on a semaphore, then consume a unit, then terminate. Have the producer post a message and then a semaphore unit in its loop. – Martin James Nov 28 '15 at 15:19
  • No..I want consumer to consume until time slice and then die. Seems odd but i need it for some other purpose and the code i wrote is just for conviniece and understanding. I dont want to use mutex i need CAS queue. – Faisal Bahadur Nov 28 '15 at 15:26
  • Perhaps you should explain your situation a bit further. – Kayaman Nov 28 '15 at 16:03
  • To be clear,Can you please tell me what part of the question needs further explanation. – Faisal Bahadur Nov 28 '15 at 16:13
  • In spinlock a threads consumes cpu-cycles until its time quantum expires and when its time quantum is expired it is interrupted forefully by OS to leave the cpu. Will this cause isInterrupted() to be true? – Faisal Bahadur Nov 28 '15 at 16:20
  • 2
    No. This is typical of the misunderstanding that arises when languages overload words like 'interrupt'. Java 'isInterrupted()' has little correspondence to the hardware interrupt from a a driver. – Martin James Nov 28 '15 at 17:04
  • 1
    Also, your consumer can have no idea whether or not it has been preempted by a driver interrupt. That's kinda the point! – Martin James Nov 28 '15 at 17:06
  • 1
    ..and even if driver interrupts happen, and a reschedule run is made, the thread made ready by the driver may get run on another core and your spinlock thread just shoved back on as running. – Martin James Nov 28 '15 at 17:10
  • 1
    spinlocks, in general, suck. They are only advantageous if a thread running on another core holds the lock for a short time, (eg. to push a pointer onto a queue). Otherwise, they waste both CPU cycles and memory bus cycles, eating up one core and delaying access to shared memory for all other cores. – Martin James Nov 28 '15 at 17:14

0 Answers0