• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print Queue using Priority Q simulation  RSS feed

 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Im quite the newbie on here and very new to Java. Please go gentle on me ;)
I have a task to accomplish however not quite sure how it would work. I have a class "ExecuteJob" which has Print Q in the form of Priority Q.
You can keep adding job to the Q by calling one of the method in the class. However, and object cant do things simultaneity can it? While im adding a new job to the print queue, can it be executing and existing job in the print Q.

To achieve that, I would need to implement process and threads? I believe am I right? So that adding a job is independent to being removed?

Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It's certainly possible to have multiple threads use the same object. So, if you have a program with two threads, then one thread could be adding something to the queue of your object while another thread is busy executing a job in the print queue. Multiple methods would then be executing on the object at the same time, in different threads.

What makes multi-threaded programming hard is that it's possible to have more than one thread trying to modify the same object at the same time. You have to be very careful that the threads don't mess each others changes to the object up. That's why you need synchronization to make sure that only one thread at a time can make changes to the object.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Are you the Harish Shivaraj whom I last met at King's Cross?
There are classes which can support such queues and help sort out the producer‑consumer problem. Read about this class and the interface which it implements.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper - Thanks very much! Quite certainly extra precautions will have to taken to make sure if several object manipulate on a common data. As you said by using one of the synchronization methods.

So if there were multiple threads/processes working a common object, take for example a class with a queue attribute which can insert/append and do other manipulation to it. There need to be just one instance of this class, such that the threads can have one common object or would it be two object for each thread and static queue within the class? Whats the the best way to it? Or is it just an novice question, where in either way it would make no difference?

@Campbell - Precisely the same Harish you met at Kings Cross . Your memory is as good as a computer. Never forget the AI classes and your delicious roast dinner ;) Hope your well.

I’m aware of the rich collection of classes which Java had to provide. However my professor - has put constraints on, such that we don’t use any data structure from the collections including ArrayList ending up writing my own dynamic Array using Linked List.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish Shivaraj wrote:So if there were multiple threads/processes working a common object, take for example a class with a queue attribute which can insert/append and do other manipulation to it. There need to be just one instance of this class, such that the threads can have one common object or would it be two object for each thread and static queue within the class? Whats the the best way to it? Or is it just an novice question, where in either way it would make no difference?

There's no correct answer to this question - programs can be designed in multiple ways and each solution has its pros and cons. But however you design your program, if you have multiple threads that need to modify the same object (whether they have a reference to the same object, or you have two objects that have common data in a static member), you will always need to be careful with synchronization so that the threads don't interfere with each other.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!