• 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

Interview question on multi-threading

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading a question on "careercup" and come across following problem:

A server receives requests from different clients...each client send a Runnable job and time on which this job should be run. Write a java program that would
accept these jobs and run each job at the required time.



I am able to do the same using DelayQueue.

But i am thinking to solve it from other ways as well. Like using priority queue or linked blocking queue. I thought about the prons-cons about them. Kindly let me know
about your thoughts. My thoughts about them as below:

1) Priority Queue: The idea is to remove shortest time required to run the job, schedule it in the new thread and move to other.
2) LinkedBlockingQueue: Remove one by one and start schedule them using new thread.

Cons for both the approach: Many threads needs to be opened. Secondly as we have to schedule the thread so it doesn't matter whether we remove small
interval or largest interval.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not a ScheduledExecutorService?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the "time" important? So that assigning short tasks/jobs to queues first or follow FIFO approach?

How many threads do the server expect to do at the same time (similar to a pool)? For example 5 threads can be run, and say the tasks are
A 5
B 2
C 10
D 7
E 15
F 8

The distribution can look like:
A D
B F
C
E

Only 4 threads are needed. Or using all 5 threads
A
B F
C
D
E
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob says....
Why not a ScheduledExecutorService?



Yeah, this what i am thinking once i fetch the object from the queue but which queue should i use?

Secondly, the number of threads required can be very much if the jobs run after long time. What do you say?

The distribution can look like:
A D
B F
C
E



@Tsang sorry but did not understand this. Shouldn't it be B A D F C E.

Also your approach is good for short tasks? what about longer tasks?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long tasks have dedicate threads. In my example C and E are consider long.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to implement the same using Priority queue but after giving stop producer doesn't stop. It keeps producing the same. Any idea or improvement?














Output:

Consumer started
Producer started
Queue size: 0
Running jobpool-2-thread-1 scheduled for 1075 milliseconds
Queue size: 0
Queue size: 0
Queue size: 0
Queue size: 0
Running jobpool-2-thread-2 scheduled for 1571 milliseconds
Queue size: 0
Queue size: 0
Running jobpool-2-thread-3 scheduled for 2402 milliseconds
Running jobpool-2-thread-4 scheduled for 3481 milliseconds
................
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I figured it out. The problem is with the consumer. I dint start different thread for the consumer, that's why in the ServerMain class control is not
going in next line. I start different thread for the consumer and problem is solved.
 
reply
    Bookmark Topic Watch Topic
  • New Topic