File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Print ab1, ab2, ab3 With Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Print ab1, ab2, ab3 With Threads" Watch "Print ab1, ab2, ab3 With Threads" New topic
Author

Print ab1, ab2, ab3 With Threads

Keith Mattix
Greenhorn

Joined: Mar 30, 2012
Posts: 7
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

Joined: Oct 31, 2011
Posts: 498

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.


PM Me If You Want to get Urgent Help on Java Programming
Keith Mattix
Greenhorn

Joined: Mar 30, 2012
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Mattix
Greenhorn

Joined: Mar 30, 2012
Posts: 7
Ahh, so what you're saying is that I need a lock for each block of code I need to run?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

Joined: Mar 30, 2012
Posts: 7
It's the same lock. I instantiated it in the constructor. I just used the same lock.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

Joined: Mar 30, 2012
Posts: 7
So if I make the lock and condition static, then I shouldn't have that problem.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Keith Mattix
Greenhorn

Joined: Mar 30, 2012
Posts: 7
It doesn't have to necessarily be in that order. It could be ab1, b1a, etc.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

how about ab1 ababab2 ?
Keith Mattix
Greenhorn

Joined: Mar 30, 2012
Posts: 7
No, it has to be where you see a and b with a number (in numerical order) in any order.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Print ab1, ab2, ab3 With Threads