• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronize  RSS feed

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my program.


I want one of the threads to finish the first 50 loop iterations and then the next thread will continue the iteration.
I want the output to be in ordered as below so I used the synchronize keyword but the output is shuffled.
Kindly, help me if there is anything I need to change in the code to make the output ordered.


final output
1
2
3
.
.
.
.
50--- one thread has completed its execution then,
51
52
.
.
.
.
100 -- second thread has completed its iteration.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Pandian wrote:
I want one of the threads to finish the first 50 loop iterations and then the next thread will continue the iteration.
I want the output to be in ordered as below so I used the synchronize keyword but the output is shuffled.
Kindly, help me if there is anything I need to change in the code to make the output ordered.



Question : What does the synchronized keyword do?

You have two threads. You want one of your threads to increment balance 50 times before another thread can increment it. Sure your public synchronized void increment() method is ensuring that a thread that is executing that method completes the method and only then another thread can be executing that method. But you don't want other threads to be able to increment balance till the first thread is done with 50 such iterations. So what do you think you should do?
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i understood,

i want the output to be in order so i used I am using synchronize here.

But, even though after using synchronize I am not able to print the output in order. Before one thread completes it turn the other thread comes and start it part .
So i am confused
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Pandian wrote:Before one thread completes it turn the other thread comes and start it part .
So i am confused


So when this second thread comes in, what is the first statement it executes and in which method is this statement present?

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the first thread t1 starts to execute after providing some series like 1 2 3 then thread t2 starts to join and produce output like 4 5 6 then again A continues and then in vice-verse.

But i dodnt want this kind of output and need to know the error in my code.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan Pandian wrote:When the first thread t1 starts to execute after providing some series like 1 2 3 then thread t2 starts to join and produce output like 4 5 6 then again A continues and then in vice-verse.

But i dodnt want this kind of output and need to know the error in my code.


Fine. You have two easy options.
Put the following block of code


in a synchronized block that locks the this object. And this will make the second synchronized keyword redundant. So you should remove it from the method public synchronized void increment().

Your second option is to make the run method synchronized and remove the synchronized keyword from the increment method.

Any of those will work.
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Heena

Got the solution
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a synchronization done when we extends the thread ??

Because while implementing Runnable its possible but I dont know is there way to synchronize when we extend Thread also??


 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can synchronize any method, yes. However you should make sure you know what object you are synchronizing on, which I guess might be an issue arising from that question.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... Keeping in mind that almost always extending Thread is the wrong thing to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!