• 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

Thread Scheduler

 
Ranch Hand
Posts: 38
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When the below code is executed it is guaranteed that C will be printed at the end. The output will be either ABC or BAC.
What changes needs to be done to ensure that t2 runs only after execution of t1 ,So the expected output is ABC .
Possible options include
1) set t1 a higher priority than t2.
2) Create threadpool and submit thread t1 to executor and wait for completion using awaitTermination method. Once t1 is
completed proceed with t2.
3) Future object.
4) Countdown latch.

What is the recommended concurrent API class I should use ?.



Regards,
Anil
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what the "recommended" approach would be (this is a toy example, so anything is fair game), but you should never rely on thread priorities.
 
Anil Kumar
Ranch Hand
Posts: 38
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let us assume we have the following use case.

1) Main thread services depends on T1, T2 and T3.

2) T2 depends on T1.

3) T3 depends on T2.

So the final sequence of starting the threads is T1-->T2-->T3-->Main.
The approach that I was thinking was to use Countdown latch.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Kumar wrote:
So the final sequence of starting the threads is T1-->T2-->T3-->Main.
The approach that I was thinking was to use Countdown latch.



Not sure what you mean by "sequence of starting". If that is the order that the threads should start, then you already failed, as the main thread was started first by the JVM.

If you mean that that is the order which should finish, ie. T2 has to wait for T1 to complete before it can complete, then using the join() method would work here. Why not have T2 call join() on the T1 thread object? etc. After all, that is what the join() method was designed for -- to wait for the completion of another thread.

Henry
 
Anil Kumar
Ranch Hand
Posts: 38
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So below code snippet will ensure that thread 2 can start only if thread 1 is completed and Main method will be the last method to finish executing.


Regarding Main I meant main service and not main method. It was w.r.t sample use case. Kindly ignore.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Kumar wrote:So below code snippet will ensure that thread 2 can start only if thread 1 is completed and Main method will be the last method to finish executing.





Well, to be honest, if you are going to wait until a thread has finished before even starting the next one, perhaps it would be a good idea to change the code to not use threads (or limit its use for that portion).

Henry
 
Anil Kumar
Ranch Hand
Posts: 38
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,
Completely agreed with you,the code is as good as single threaded code. Actually the code snippet that I posted was part of mock test, so I was just figuring out what changes in the code is required to ensure that T1 thread runs to its completion and then T2 executes.Thanks for your help.
reply
    Bookmark Topic Watch Topic
  • New Topic