• 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

Print ab1, ab2, ab3 With Threads

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on some code for my Advanced Java class, however, I cannot figure out why this code is not giving me the output, ab1, ab2, ab3, ab4, etc. Could you guys please help me?

The code:



And I call it like this:

PS: I'm new to Java ranch, so if I do anything wrong, please tell me and I will correct it.

 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keith Mattix wrote:I am working on some code for my Advanced Java class, however, I cannot figure out why this code is not giving me the output, ab1, ab2, ab3, ab4, etc. Could you guys please help me?



Because all the threads are running concurrently and the order is arbitrary.
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While that is true, the point of the assignment is to produce the desired out put using Thread synchronization, Locks, or Semaphores.
 
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

Keith Mattix wrote:While that is true, the point of the assignment is to produce the desired out put using Thread synchronization, Locks, or Semaphores.




Hint: How many synchronization locks do you have?

Henry
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, so what you're saying is that I need a lock for each block of code I need to run?
 
Henry Wong
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

Keith Mattix wrote:Ahh, so what you're saying is that I need a lock for each block of code I need to run?



No.

And since, I don't have anymore hints to give, the reason is .... Your three threads are using three different locks. There is no synchronization going on between them.

Henry
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the same lock. I instantiated it in the constructor. I just used the same lock.
 
Henry Wong
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

Keith Mattix wrote:It's the same lock. I instantiated it in the constructor. I just used the same lock.



Look again, You have three different threads, each with it's own runnable class (and each with it own lock).

Henry
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I make the lock and condition static, then I shouldn't have that problem.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keith Mattix wrote:So if I make the lock and condition static, then I shouldn't have that problem.



It'll be better, there will be synchronization. There will still be a possible problem with the order of operations. Since all the Runnables use the same Condition then you will lose control over what order they get the signal to run in. Whenever you send the signal any one of the other threads waiting on the condition will be awoken, so there is no guarantee that they will go in the order of a -> b-> number. You will need to either use multiple conditions to ensure the correct order, or use an additional conditional to make sure only the intended thread wakes up.
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't have to necessarily be in that order. It could be ab1, b1a, etc.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about ab1 ababab2 ?
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it has to be where you see a and b with a number (in numerical order) in any order.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic