Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:49:06+00:00 2026-06-06T07:49:06+00:00

I Have made a prority blocking queue.Inserting is properly done based on priority.when i

  • 0

I Have made a prority blocking queue.Inserting is properly done based on priority.when i delete the head of the queue,last element of the queue becomes the head of the queue ,procedure repeats.
Suppose i inserted 123456789 in the queue.

First deleted : 1;  queue elements :92345678  
Second deleted:  9; queue elements :8234567  

and so on.

I need to achieve queue property, which is not being maintained.

i.e
First delete : 1 queue elements :23456789
Second delete :2 queue elements :3456789

package com.block.ui;

import java.util.Iterator;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class PriorityBlockQueSamplActivity extends Activity {
/** Called when the activity is first created. */

EditText et;
EditText et2;
Button b1;
Button b2;

Controller c ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et=(EditText)findViewById(R.id.editText1);
    et2=(EditText)findViewById(R.id.editText2);
    b1 =(Button)findViewById(R.id.button1);
    b2 =(Button)findViewById(R.id.button2);
    c= new Controller();


    Message m = new Message();
    m.mPriority=1;
    m.Subject="1";
    c.insert(m);

    Message n = new Message();
    n.mPriority=1;
    n.Subject="2";
    c.insert(n);
    Message p = new Message();
    p.mPriority=1;
    p.Subject="3";
    c.insert(p);

    Message q = new Message();
    q.mPriority=1;
    q.Subject="4";
    c.insert(q);


    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Iterator<Message> iter = c.getQueue().iterator();
            while(iter.hasNext()){
                Message m =iter.next();
                Log.i("MESSAGE", "Element "+m.Subject+" priority :"+m.mPriority);
            }

        }
    });
    b2.setOnClickListener(new  OnClickListener() {

        @Override
        public void onClick(View v) {
            Message m=c.getMessage();
            c.delete(m);

        }
    });
}

}

and one more class

 `public class Controller {

private int queueSize=300;
Comparator<Message> comparator = new PriorityComaparator();
PriorityBlockingQueue<Message> qu = new PriorityBlockingQueue<Message>(queueSize,comparator);

            public Controller() {

}

public void insert(Message msg) {

    boolean status = qu.add(msg);
    if (status)
        log("Added successfully " + msg.Subject + " priority "
                + msg.mPriority + " size :" + qu.size());
    else
        log("Failed to add " + msg.Subject + " with priority "
                + msg.mPriority);

}

public void delete(Message msg) {
    if (qu.contains(msg)) {
        boolean status = qu.remove(msg);
        if (status)
            log("deleted successfully " + msg.Subject + " priority " + msg.mPriority
                    + " size :" + qu.size());
        else
            log("Failed to delete " + msg.Subject + " with priority "+ msg.mPriority);
    }
}

public Message getMessage() {
    return qu.peek(); 
}

public PriorityBlockingQueue<Message> getQueue(){
    return qu;
}
void log(String msg) {
    Log.i("QUEUE", msg);
}

class PriorityComaparator implements Comparator<Message> {

    @Override
    public int compare(Message m, Message n) {
        int y = ((Message) m).mPriority; // 1 -highest 3-least
        int z = ((Message) n).mPriority;


        if (y < z) {
            return -1;
        }
        if (y > z) {
            return 1;
            }
            return 0;
            }

        }

}`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:49:09+00:00Added an answer on June 6, 2026 at 7:49 am

    If the priority queue you are using is your personal implementation, then try to an heap data structure.

    As the wikipage says:

    The heap is one maximally efficient implementation of an abstract data type called a priority queue.

    Here is the link to the page.

    Remember that in Java you don’t need to implement it, you can use the standard PriorityQueue class or PriorityBlockingQueue if you need that it works in a multithread program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site that will show the priority of a user made in
i have made an window based application in C# and MySQL is my database.now
I have made a jQuery toggle for a menu that I had in mind.
I have made an application for IPad in objective C. In this I am
i have made an application having entity framewrok. It is wpf application, now it
I have made a code that read data from flash Nand (without filesystem). fd
I have made a cart and the cart has remove item button, but the
I have made a website using( Asp.net, c# ) and its content in English
I have made a map of functions. all these functions are void and receive
I have made a String[] array by using String.split(.) . I am now trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.