wood burning stoves*
The moose likes Threads and Synchronization and the fly likes synchronisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronisation" Watch "synchronisation" New topic
Author

synchronisation

harry harrry
Greenhorn

Joined: Aug 04, 2010
Posts: 2
can anybody tell me how to synchronize two or more theads....by giving the example...
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 04, 2010
Posts: 2
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

"harry harrry",
please check your Private Messages for an important administrative matter.


[My Blog]
All roads lead to JavaRanch
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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

Joined: Nov 04, 2009
Posts: 2066

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!
 
 
subject: synchronisation