-1

Please, i have some problems with my web application.

I can't paste my code here ( too big and i have the difficulty to reproduce the error ) but, this is my issus.

I have a object that contains a collection. I use BlockquingQueue to share this objet betwen some thread. the second kind of thread is a servlet.

When i put my objet in the queue, the collection is not empty and i can display thier element.

But, when i take the same element, the collection size is not null, but it don't have elements.

NB: I don't have problems to get the objet in queue. My problems it which their attribute of type Collection. It show me a strange behavoir.

a big part of a code:

public class HttpCollectionConsumer extends JCasAnnotator_ImplBase{
    private static BlockingQueue<Answer> queue = new LinkedBlockingQueue<>();
    private static boolean hasNext = true;

    public void initialize(UimaContext context) throws ResourceInitializationException{
        super.initialize(context);
    }

    @Override
    public void process(JCas jcas) throws AnalysisEngineProcessException {
        // TODO Auto-generated method stub
        edu.cmu.lti.oaqa.type.input.Question q = TypeUtil.getQuestion(jcas);
        System.out.println("get Text " + q.getText());
        Question question = new Question(q.getId() , q.getText());
        Focus focus = TypeUtil.getFocus(jcas);
        Collection<LexicalAnswerType> types = TypeUtil.getLexicalAnswerTypes(jcas);

        Answer a = new Answer();

        a.setQuestion(question);
        a.setFocus(focus);
        a.setTypes(types);

        try {
            System.out.println("identifiant : ( " + a + " ) types "  + a.getTypes().iterator().next());
            System.out.println("the answer type is not empty : " + a.getTypes().iterator().hasNext());
            synchronized(this){

                queue.put(a);
                Thread.sleep(1000);
            }
            System.out.println("putting finished " );
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static synchronized void put(Answer question) throws InterruptedException{
        System.out.println("new answer : " + question);
        queue.offer(question);
    }

    public synchronized static Answer take() throws InterruptedException{
        Answer a = queue.take(); 
        Thread.sleep(2000);
        System.out.println(" someone takess ( " + a + " ) , remaining: " + queue.size());
        System.out.println("the answer type is not empty : " + a.getTypes().iterator().hasNext());
        return a;
    }

    public synchronized static BlockingQueue<Answer> getQueue(){
        return queue;
    }

    public synchronized static void stop(){
        hasNext = false;
    }

}

Someone can know why ?

neo m
  • 222
  • 1
  • 4
  • 13
  • 1
    If you are saying that you are not able to get elements from a Queue twice, then well, queues are not meant to be used for such use case. Queues are FIFO data structures. Use List instead. – adi May 05 '17 at 16:29
  • No, i the first times. I can't get the element, but the attributes of type collection is corrupted. He return me non zero size, but don't have element. – neo m May 05 '17 at 16:35
  • 1
    First of all, `BlockingQueue` is an interface, not a class. Which implementation are you using? – D M May 05 '17 at 16:46
  • LinkedBlockingQueue – hubert May 05 '17 at 16:46
  • I'm also tried ArrayBlockingQueue and ConcurrentLinkedQueue – hubert May 05 '17 at 16:47
  • Wait, what? Do you have two accounts? – QBrute May 05 '17 at 16:48
  • I can't ask questions with this account. – hubert May 05 '17 at 16:49
  • Your code may be too long to show, but can you show the lines where you put and take from the queue? – D M May 05 '17 at 16:52
  • i edited the question with code. – neo m May 05 '17 at 16:56
  • OK. So it's the line `Answer a = queue.take();` that is not returning an element even though the queue is not empty? – D M May 05 '17 at 17:00
  • it return the element. but the element have an attribut of type Collection. This attribute have a strange behavoir. – neo m May 05 '17 at 17:05
  • the first occurence of this line `System.out.println("the answer type is not empty : " + a.getTypes().iterator().hasNext());` show me `true` and the second in take() function display `false` – neo m May 05 '17 at 17:06
  • I suppose it's possible that some other thread is putting something else in the queue between your first `hasNext()` check and `queue.put(a);`, because your `hasNext()` isn't inside your `synchronized(this)` block. – D M May 05 '17 at 17:32
  • I don't thinks because the hasNext() appears only one time and only this function `process` put data in queue. – neo m May 06 '17 at 09:38

1 Answers1

0

You can only get a item from a Queue once, First Object In First Out. If you want to access a object more than once or directly you should use a List and transform that into a Collection. Also, if you share your object between Threads only one of the Threads will be able to access the mentioned object, since after being processed by one it won't be in the Queue anymore.

ArthurG
  • 335
  • 2
  • 11
  • I can get my element. But their attribute is corrupted. the collection show me a strange behavoir. It return non zero size but contains anything. – neo m May 05 '17 at 16:37