0

我已经创建了一个优先阻塞队列。插入是根据优先级正确完成的。当我删除队列的头部时,队列的最后一个元素成为队列的头部,过程重复。假设我在队列中插入了 123456789。

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

等等。

我需要实现未维护的队列属性。

即第一次删除:1 个队列元素:23456789
第二次删除:2 个队列元素: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);

        }
    });
}

}

还有一堂课

 `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;
            }

        }

}`
4

2 回答 2

2

如果在您的示例中,所有插入的元素都具有相同的优先级,则检索顺序没有明确定义。从PriorityQueuePriorityBlockingQueue派生出相同的排序规则):

此队列的头部是相对于指定排序的最小元素。如果多个元素以最低值绑定,则头部是这些元素之一——绑定被任意打破。

(重点补充)

于 2012-06-26T07:36:58.517 回答
0

如果您使用的优先级队列是您个人的实现,那么请尝试使用数据结构。

正如维基页面所说:

堆是一种称为优先级队列的抽象数据类型的最有效实现。

这是该页面的链接。

请记住,在 Java 中您不需要实现它,如果您需要它在多线程程序中工作,您可以使用标准的PriorityQueue类或PriorityBlockingQueue 。

于 2012-06-26T07:19:47.477 回答