• 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

Synchronization problem

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Poonam Advani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Poonam Advani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Poonam Advani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic