DelayQueue, ThreadPoolExecutor, Generics n' a cast O'My w/concurrent packag
Everyone,
I have been looking at this all day. This example runs, but I have a cast where a Generic should be enough. I think my problem starts with the ShortTask object which because I want to slam this in the DelayQueue it needs to implement Delayed as seen below:
public class ShortTask implements Runnable, Delayed {
My ShortTask object is contrived and lame, but just know it needs to go into the DelayQueue.
Okay then in my other GenerateTasks object I create a bunch of ShortTasks and add them to the DelayQueue...my problem is this:
I add the ShortTasks into the DelayQueue which is Generic'd (Is that even a word) as ShortTask as seen next:
delayQue = new DelayQueue<ShortTask>();
The add works swimingly , however I need to cast as seen at the end of the Constructor for the ThreadPoolExecuto (in bold for easy spotting):
threadPoolExecutor = new ThreadPoolExecutor(1, 1, 100L, TimeUnit.MILLISECONDS, <b>(BlockingQueue)</b>delayQue);
How do I get rid of that cast, and enable Generics since the custructor wants a Runnable.
I see this other post http://forum.java.sun.com/thread.jspa?threadID=620333 . but it did not help me out since the Concurrent package seems to enforce Generics which is good, but...well...this is driving me crazy.
Also since my ShortTask implements Runnable why does using the Generic ShortTask not work, why do I have to cast it since the Generic is <ShortTask> and a ShortTask implements Runnable?
Oh esteemed programmers that are so much smarter then me, please help me, and enlighten me, or just tell me I am dumb for asking something that is probably so simple I will cry when I am shown it.
Have a great day!!!!
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
publicclass GenerateTasksimplements Runnable{
private DelayQueue<ShortTask> delayQue =null;
private ThreadPoolExecutor threadPoolExecutor =null;
privateint tasksToGenerate = 10;
public GenerateTasks()
{
delayQue =new DelayQueue<ShortTask>();
threadPoolExecutor =new ThreadPoolExecutor(1, 1, 100, TimeUnit.MILLISECONDS, (BlockingQueue)delayQue);
threadPoolExecutor.prestartCoreThread();
}
publicvoid run(){
for (int c = 0, d = tasksToGenerate; c < tasksToGenerate; c++, d--){
ShortTask shortTask =new ShortTask("Task: " + c, d * 1000);
delayQue.add(shortTask);
}
}
publicstaticvoid main(String args[]){
Thread thread =new Thread(new GenerateTasks());
thread.start();
try{
synchronized(thread)
{
new Thread().sleep(12000);
}
}catch (InterruptedException e){
e.printStackTrace();
}
System.err.println("Finishing Run");
}
}
import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
publicclass ShortTaskimplements Runnable, Delayed{
private String name =null;
privatelong waitTime = 0;
privatelong startTime = 0;
public ShortTask(String name,int waitTime)
{
this.name = name;
this.waitTime = waitTime;
this.startTime = System.currentTimeMillis();
}
publicvoid run(){
try{
System.err.println("Cause this is lame, " + name +", " + waitTime);
System.err.println(new Date());
synchronized(this)
{
new Thread().sleep(100);
}
}catch (InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String toString()
{
StringBuilder stringBuilder =new StringBuilder();
stringBuilder.append("Name: ");
stringBuilder.append(name);
stringBuilder.append(" waiting " );
stringBuilder.append(waitTime);
stringBuilder.append("ms. ");
stringBuilder.append("Start Time: ");
stringBuilder.append(startTime);
return stringBuilder.toString();
}
publiclong getDelay(TimeUnit unit){
long currentTime = System.currentTimeMillis();
return unit.convert(startTime + waitTime - currentTime, TimeUnit.NANOSECONDS);
}
publicint compareTo(Delayed o){
return (int)(getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS));
}
}

