• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print ab1, ab2, ab3 With Threads

 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.

 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
So if I make the lock and condition static, then I shouldn't have that problem.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about ab1 ababab2 ?
 
Keith Mattix
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic