wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization problem" Watch "Synchronization problem" New topic
Author

Synchronization problem

Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
Hi,
can anybody help me out with the following program...


As such there are no errors in the code , but the output i expected from this is printing
A 1, A2 ... 10 times, followed by B1, B2... 10 times, followed by C 1 , c 2... 10 times.




Please let me know where am i getting wrong ?
Thanks
Kirti
[ November 02, 2004: Message edited by: Kirti Singh ]
Georg Nieuwoudt
Greenhorn

Joined: Nov 02, 2004
Posts: 13
Could you clarify what you want it to do, im not clear on what you want to know?

if you want :
From A1
From A2
From A3
From A4
From A5
ect....

to be printed out then change this line of code:
System.out.println("From " + Thread.currentThread().getName());

to :
System.out.print("From " + Thread.currentThread().getName());

remember you first have to print the from then the thread name so dont use the println rather use the print if u want to get the number to be next to the thread name or even do this :
System.out.println("From " + Thread.currentThread().getName() + i);


[SCJP 1.4]
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
well, i want the output to be printed as :

A
1
A
2
...
A
9
B
1
B
2
B
3
...
B
9
C
1
...

C
9

Kirti
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You must synchronize on the same object for this to work. Each of your threads is synchronizing on its own object. So the threads are able to proceed in any order.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
I'm sorry Barry ,
but i didn't get u. May be i'm missing something very important, and need all ur help

thanks
Kirti
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
hi Kirti,

the output you are looking for cannot be guaranteed with Thread Synchronization. Because you never know which thread is in the queue first and which one is later.

whereas I got the output as

OUTPUT
======

From A
0
From A
1
From A
2
From A
3
From A
4
From A
5
From A
6
From A
7
From A
8
From A
From B
From C
0
9
From B
1
From B
2
From B
3
From B
4
From B
5
From B
6
From B
7
From B
8
From B
9
0
From C
1
From C
2
From C
3
From C
4
From C
5
From C
6
From C
7
From C
8
From C
9

=========


It is pretty much the same way your are looking for but in the middle we have "From A From B From C 0 9".

There is only one option that is thread priority, which can be used to control the sequence of execution of threads.

Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
Thanks Kaps,

well, i could get the output using "join()" method. but i was looking for something that uses the "synchronized"

Kirti
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ok, you are starting three threads. Each one of those threads is synchronized on the lock on its own instance (this). For synchronization to work you should synchronize on one shared object.

So in the main program you can create any object and pass it to each of your Thread objects as an argument, and then synchronize on it in your run method (instead of this).

I could give you the code, but it's better for you to find it out for your self.
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
Hey, Thanks Barry

I got the output as i wanted. In my code , instead of extending the class from Thread, i used "implements Runnable" , and created three threads, using the one - same runnable object.

Was this the same u wanted me to do? or is there any other way to get the same solution ?


Thanks

Kirti
[ November 03, 2004: Message edited by: Kirti Singh ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In the case of your example your solution sounds OK. I was thinking of creating an arbitrary object like "Object lockObject = new Object();" in the main method and passing the object to the constructor for your thread objects to synchronize on. Each of your thread objects would have to keep a reference to this lock object so that the run method could refer to it.
[ November 03, 2004: Message edited by: Barry Gaunt ]
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38


Barry, can u please provide me with the code . I think, its time I really need that...

Thanks
Kirti
[ November 04, 2004: Message edited by: Kirti Singh ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Synchronization problem
 
Similar Threads
Thread's Priority
How to make one thread die before another thread
Threads: synchronizing a block of code (K&B 1.5)
Thread doubt
Threads and synchronisation