• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timer

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have to implement a priority queue, in a printer simulation...

and i have to assume the printer prints 10pages/minute...

how do I go about timing it?

i was thinking Timer class, but it's not a graphical interface..

Justin
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a Timer class in java.util
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, I know that.

But i ghought it was only for action listeners(graphics).

Justin
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the util Timer for anything you like. The awt Timer has some special hooks for UI. You might have a BlockingQueue for print jobs and a timer task that pulls one off the queue every 6 seconds. Is that the kind of simulation you have in mind?
[ July 20, 2006: Message edited by: Stan James ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well each print job is put into an event class by time...

by time I mean, job 4 gets put into the priority queue at 2 min..

and has 138 pages...

and according to what priority the job has depends on where it gets placed..

because we're implementing a queue using doubly linked lists...

their are 4 priorities....

enum priority{priority,system,client,development};

and if you insert a job with any priority less than "priority" then you

have to insert a boost behind it, with time 30min, so that if it has not printed in 30 mins, you changes its priority to higher.

well 30min if its development, 25 if its client, and 20 if its system..

but there is also a main timer that goes to 100 min. so it can simulate page printing, then quits...

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but say you have the priority queue as follows..

|p|p|S|C|C|D|

where p = priority, S = system, C = client, D = development;


and say you boost the C, which has to be the C right after S, because

its evidently been there longer....

it stays in the same place....

nevermind, i see why you need it....

Justin
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds pretty interesting. Are you still stuck on something?

Just curious, can an "S" ever print if there is a "p" in front of it? If so, is there some risk Development jobs will never print but always get bumped by other jobs?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, only the p's print, because their priority is more important, but if

their are none, then goes down to system, etc.

but their is always the chance that one will get boosted all the way up to

the priority lvl.

but I looked at Timer and TimerTask, now if i wanted to use this to

just count up to 100 min. how would i do that?

and if i need to compare the time to the time a job was supposed to be

input into the priority queue, basically, how does the Timer and TimerTask work?

can you just set a Timer like so...

Timer time = new Timer(1000, count ++)

where count will keep track of the seconds?

since timer counts in milliseconds....?

thx Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh.... no every thing should get printed,

but when you put in a development job into the queue, it gets a

"boost" aka timer object put behind it, and if that "boost's" time

gives up before the job gets printed, then it gets boosted to the next up

priority....

same for client and system, but client's boost is 25 min, and system is 20.

and the "printer" is supposed to print 10pgs/min...

so about 1 pg every 6 seconds..

i think thats right....

so i need a constant timer for the Eventqueue, priority queue, and boost..

because the Eventqueue holds all the jobs in order by the time they are supposed to be inserted into the priority queue.

so say job 1 has time 2, then when the timer is at 2 min, job 1 gets placed into the priority queue, at the right postion and if its the first job, then it starts the printTimer, and if its not, then it goes to the correct priority in the queue..

Justin
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd make a TimerTask for every print job so that the print job and the timer task have references to each other. When the job starts printing it cancels its task. If the timer goes off before the job prints, the task boosts the priority, re-arranges the job queue and schedules itself again.

The printer can run in yet another TimerTask. It pulls a job off the queue, computes how long the job will take (page count * 6 seconds), schedules itself to run again in that many seconds. If that works, you could get cute and make it display "Printing page n of n" every 6 seconds.

You only need one Timer to manage all the tasks.

A few interesting problems remain. What do you do if the queue is empty? How do you shut the thing down before the queue is empty?

This is a fun assignment. Keep us posted on progress!
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the program is only gonna run for 100 minutes...

then the loop will end i guess...

Justin

could someone give me a small example of how to implement a timer...

that goes for 100 min. then stops??

and maybe does a task every 10 minutes?

thx,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, i'm straight up stuck, i have no idea how to use the Timer/TimerTask,

it's really weird...

Justin
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's back up then. Read up on the Sun thread tutorial, and the section on Timers. If this page doesn't make sense, take the "start of tutorial" link and work your way back here from the top.

The basic idea is you give a Task to the Timer and tell the Timer when to call run() on the Task. See if this sounds good:

See if you can make that run with the simplest possible queue - just one job loaded ahead of time.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok everytime I try to create the new ..... like this..



now i have to use this as a inner class, so i can mess with EventQueue..

but I keep getting static errors when i try to do the following

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understand the whole giving a task to a timer, but I'm messing up syntatically bad..

and their are no example print simulation on the web lol

all the timer/timertask examples are very non-trivial and won't work for

me for some reason...

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of makes me angry. My professor just hands us programs, and just says, "it's due in a week".

doesn't go over them or anything..

It's frustrating because I've never even heard of util.Timer or
util.TimerTask, and im about to be in software Engineering and
more upper level classes...

My point being, basically, is that im not going to be very prepared.

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how am I supposed to instatiate a instance of TimerTask,
if its abstract?


or even if i make a new class like the example above and extend
TimerTask, when i create a new instance of that class, i'm still
going to get a non-static static reference error..

Justin
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Justin Fox:
how am I supposed to instatiate a instance of TimerTask,
if its abstract?


You can't ofcourse, because it's abstract. You create a new subclass of TimerTask and implement the abstract method run() in your own subclass, and create an instance of your own subclass which you give to the Timer, just like the tutorial shows you.

or even if i make a new class like the example above and extend
TimerTask, when i create a new instance of that class, i'm still
going to get a non-static static reference error..


You create a new instance of a class by using the new operator:

By the way, this code you wrote doesn't look too good:

What happens here? In the constructor of class printEvent you create another printEvent object, by calling a non-existent no-args constructor (you'll get a compiler error for that). You are also missing semi-colons at the end of your statements.

Just create the printEvent object and the Timer in your main method. Look closely at the example in the tutorial and structure your class in the same way. Note that in the example, the class RemindTask is an inner class of the class Reminder. Class Reminder is not the TimerTask subclass (like you are trying to do it in your printEvent class).

Don't make the Timer object a member variable of the printEvent class and don't initialise the timer in a constructor of that class, it messes things up.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW: I went the way of Timer because that was the topic You could also solve this with plain old Threads. I'm not sure which would be easier, but I kind of like the way TimerTasks are working out here. I think I'll like them even better for promoting jobs that have been in queue too long.

I'd find it simpler to use separate source files and top level classes than to use inner classes. Sometimes it seems like more typing and more files, but the concepts are just easier for me to keep up with.

A good challenge is to find a very small part of this you can make as simple as possible. That's why I hinted at a minimal queue that has only one item in it. That lets you focus on the printer and ignore the queuing stuff until later. I'm sure we could find even smaller bits to try first with a little time.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thx for the info, but i just went with thread

i need some help with a piece of my code tho...

// method void startPrinting();




this is the code that subtracts a page from the current printjob every
6 seconds...

and after its done, it deletes the node its at, and then if the priority

queue has more in the queue, it keeps on printing those, but if it doesnt,

on to the system queue, if system has none, on to client etc, but if system does have a job, it prints it, and when its done, checks to see if

priority has gotten a job, and if not goes to nextNode to see if it has another...

same for the others...

here is the code that is calling startPrinting...



this code is just incrementing Arrival every minute, and comparing it
to the job time of the current Event, and if it is equal, then it inserts

it into the correct queue, and then "startPrinting()"...

but I am getting this error, and I've been looking for it for awhile,

i thought maybe the values were being inserted into the queues, but they are, here is the error msg..



as you can see job one arrived, but right after that it calls

startPrinting() and gets the nullPointerException,

help meh please,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there anyway to thread two different things at the same time?...

like thread the Event list, while im threading the job queue?

because while im threading and inserting the jobs from the EventQueue to the

Job Queue's

i need to be seaching for the best job to print, and i need to thread

the pages that are being printed....

thx,
Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i got the timertest, to work, but how do i get it to do something,

over and over again?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to see code that tells the story better. Focus on writing for human readers. Here's a different direction with no TimerTasks (yet)

stillRunning() could check any condition to know when to stop. In this case it might check to see if we've been running for more than 100 seconds:

You can always take the first job in the queue regardless of its priority. If you sort the queue by priority you'll always want to print the top job next.

I decided if the queue was empty to wait 1 second and try again. You might decide to break out of the loop.

The print() method can be just as clear:

Any of that give you ideas? See if you can make something like that work with a simple queue that doesn't shift around when jobs age. Then we can work the priority issue separately.
[ July 25, 2006: Message edited by: Stan James ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now, the stillRunning() is awesome, didnt know you could do that....

but as for the pause(long milli) what is that a method to?...

thread?

cause i know you can do Thread.sleep(long milli);

it works good, except that it pauses the whole application...

i need the application to be inserting jobs by priority, AND

printing the highest priority at the same time..

now TimerTask would be perfect for the printing of the pages..

and thread works great for inputing the jobs by priority....

i still need some kind of method/getter to traverse the queue to extract

the number of pages....

hmm, which is:

either Priority,Sys,Client, or Development.getCurrentValue().getPages();

well, i have to go to work, ill be back later with more posts believe me lol

Justin,
and thanks a lot dude..
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pause() hints at a method with the sleep code you had like this:

See how pause(1000) reads better than 7 or 8 lines of code and avoids repetition? BTW: Make an empty catch clause only if you seriously know what's going on. It's almost always interesting to know that you've had an exception!

If you need the rest of the application to keep going while this happens, run it on its own thread. Say the code I showed was in a class Printer that implements Runnable ... your main() might do this ...


I'm curious about that doubly linked list for the queue. Is that a solid requirement?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic