• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Synchronization not working??

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking for some assistance. I have been playing around with threads and synchronizaton trying to prove that synchronization works, but it doesn't seem to be. I'm using Netbeans 6.5 on Windows Vista (have also tried XP). It's just a simple example. It mixes the thread results up when ran. Since it is sync'd, I wouldn't expect that. Sleep is not supposed to release the lock, but since I am getting bad results, I even tried removing the try/catch block (including the sleep) and I still get mixed up results. I also tried putting a sync'd block right inside the run method, but it still didn't work. Can anyone shed some light on this for me, please? My code is below. Thanks....
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you want your threads to run in parallel? Why are you synchronizing the run() method?

Check these

-- https://coderanch.com/t/232143/Threads-Synchronization/java/Thread-with-synchronized-run-method

-- https://coderanch.com/t/233751/Threads-Synchronization/java/Any-reason-synchronize-run-method
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone shed some light on this for me, please? My code is below. Thanks....



For two threads to block on each other, they need to synchronize on the same lock. Here you have five different threads synchonizing on 5 different counterthread instances....

Henry
 
Andrea Darnell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example wasn't really one that made sense. Parallel would be something i would have wanted to do, but I was having trouble with my synchronization, so I started playing with a bare-bones example. Setting up all the threads on the same object fixed it..... Thanks, Henry. It just goes to prove sometimes I just need to smack myself in the face and look at it again. Thanks, again.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic