Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronisation

 
harry harrry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me how to synchronize two or more theads....by giving the example...
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How it's done depends on what you want to happen. Are the threads
independent and you just want to avoid inconsistent data, or do you
want to coordinate the activities of the two threads in some way?

Jim ... ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harry harrry wrote:can anybody tell me how to synchronize two or more theads....by giving the example...


What do you mean? synchronizing two methods? Couldn't understand you question. If you can, explain some extend!
 
harry harrry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to use 3 threads first printing A 20 times,second printing B 20 times,third printing C 20 times...i want to synchronize them ie output should be ABCABCABCABC.....(there should be no intermixing of the output like ABCAABBCCACBACCCCBBBAACB....) can you please explain me the code...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"harry harrry",
please check your Private Messages for an important administrative matter.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry : What code have you tried and what problems are you having? Our job
on JavaRanch is to help you learn by offering suggestions on what you have
attempted on your own. Is this a homework assignment?

Jim ... ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harry harrry wrote:i want to use 3 threads first printing A 20 times,second printing B 20 times,third printing C 20 times...i want to synchronize them ie output should be ABCABCABCABC.....(there should be no intermixing of the output like ABCAABBCCACBACCCCBBBAACB....) can you please explain me the code...


Try to create your own thread(let's take ThrreadA), and this thread HAS-A Runnable reference, so you assigned the other thread(let's to ThreadB), to which you want to follow the execution. And in you run method(ThreadA's), join the thread behind the thread(ThreadB). Apply this to other threads. you can get a 'synchronized' output!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic