修复通用类型用法的警告消息

时间:2016-06-14 10:09:01

标签: java generics

我能够成功运行此程序。

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class FixedBlockingQueueGenerics<T> {
    final BlockingQueue<T> queue;
    private int capacity;

    public FixedBlockingQueueGenerics(int capacity){
        super();
        this.capacity = capacity;
        queue = new ArrayBlockingQueue<T>(capacity);
        System.out.println("Capactiy:"+this.capacity);
    }
    public BlockingQueue<T> getQueue(){
        return queue;
    }
    public void addElement(T element){
        try{
            queue.put(element);
        }catch(Exception err){
            err.printStackTrace();
        }
    }
    public void startThreads(){
        ExecutorService es = Executors.newFixedThreadPool(1);
        for ( int i =0; i < 10; i++){
            es.submit(new MyProducer<T>(this));
        }
        //es.submit(new MyConsumer(queue));
        new Thread(new MyConsumer<T>(this)).start();
    }

    public static void main(String args[]){
        FixedBlockingQueueGenerics f = new FixedBlockingQueueGenerics<Integer>(1);
        f.startThreads();
    }
}

class MyProducer<T> implements Runnable{

    private FixedBlockingQueueGenerics<T> queue;
    public MyProducer(FixedBlockingQueueGenerics<T> queue){
        this.queue = queue;     
    }
    public void run(){
        for ( int i=1; i< 5; i++){
            queue.addElement((T)new Integer(i));
            System.out.println("adding:"+i);
        }
    }
}

class MyConsumer<T> implements Runnable{
    private BlockingQueue<T>  queue;
    Integer firstNumber = 0;
    private final ReentrantLock lock = new ReentrantLock();

    public MyConsumer(FixedBlockingQueueGenerics<T> fQueue){
        this.queue = fQueue.getQueue();
    }
    /* TODO : Compare two consecutive integers in queue are same or not*/
    public void run(){  
        Integer secondNumber = 0;
        while ( true){
            try{
                lock.lock();
                System.out.println("queue size:"+queue.size());

                if ( queue.size() > 0) {
                    secondNumber = (Integer)queue.remove();
                    System.out.println("Removed:"+secondNumber);
                    System.out.println("Numbers:Num1:Num2:"+firstNumber+":"+secondNumber);
                    if ( firstNumber.intValue() ==  secondNumber.intValue()){
                        System.out.println("Numbers matched:"+firstNumber);
                    }
                    firstNumber = secondNumber;
                }

                Thread.sleep(1000);
            }catch(Exception err){
                err.printStackTrace();
            }finally{
                lock.unlock();
            }
        }
    }
}

但我想通过使用-Xlint:unchecked

来修复编译报告的警告

编译器报告的警告是:

FixedBlockingQueueGenerics.java:51: warning: [unchecked] unchecked cast
                        queue.addElement((T)new Integer(i));
                                            ^
  required: T
  found:    Integer
  where T is a type-variable:
    T extends Object declared in class MyProducer
1 warning

由于程序运行成功,我无法找到此警告消息的修复程序。

queue.addElement(new Integer(i));

无效,我将行更改为

queue.addElement((T)new Integer(i));

以上更改修复了编译问题,但警告保持不变。

只有在删除通用模板后,我才会删除警告。

1 个答案:

答案 0 :(得分:1)

编译器警告您正在做一些可能不是类型安全的事情。在某些情况下可能可以 - 例如如果TObjectInteger - 只是不一般 - 例如T可以是String,也可以是任何其他类。

如上所述,

MyProducer不需要类型变量。删除类型变量,并将queue(以及要分配给queue的参数)更改为FixedBlockingQueueGenerics<? super Integer>类型。

class MyProducer implements Runnable{

    private FixedBlockingQueueGenerics<? super Integer> queue;
    public MyProducer(FixedBlockingQueueGenerics<? super Integer> queue){
        this.queue = queue;     
    }
    public void run(){
        for ( int i=1; i< 5; i++){
            queue.addElement(new Integer(i));
            System.out.println("adding:"+i);
        }
    }
}