• 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

How to call two threads alternatively

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I have two threads, say Thread-1(print 1) and Thread-2(print 2). How I can call them in loop such that they will execute alternatively,
Output---  1 2 1 2 1 2 .....

Below code give me a output ----- 1 2 2 2 2 2 1 and then release the lock but no thread available to get the lock .



How we I can modify the code ?
Thanks
 
Sheriff
Posts: 17651
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to do that? If you need operations to be executed in a specific order, using threads is not how to do it. That's diametrically opposite of what threads are meant for, which is independent processing.

Use the right tool instead of trying to find a way to use the wrong tool to fit your purpose.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true. But is there any option so that we can achieve this.
 
Junilu Lacar
Sheriff
Posts: 17651
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhra Kar wrote:That's true. But is there any option so that we can achieve this.



If you're going to do it with two threads, the first thing that comes to mind is a semaphore that the two threads can check/set. Seems like it would be kind of kludgy though. What methods of thread have you studied so far -- that might give us a clue as to what your instructor is looking for.

See semaphores and mutexes

Again, I don't see this as a typical use case for threads. In fact, I think it's antithetical.

 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
               Thanks for the idea. My knowledge limited to wait, notify , coundownlatch , cyclicbarrier .Semhaphoe and mutex still I haven't studied.I will check those and get back for any query.

Thanks
Abhra
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic