This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes will the code running concurrently or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "will the code running concurrently or not" Watch "will the code running concurrently or not" New topic
Author

will the code running concurrently or not

vinoth muthuswamy
Greenhorn

Joined: May 24, 2011
Posts: 4
Hi,
I have a code.In that i am trying to hit two URLs and getting and store their datas in separate objects.My question is will it be processing concurrently.please give me suggestion

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

The code you posted spins off two new threads, which will be running concurrently.
However, the locking approach implemented to ensure thread-safe access to the shared resource "resultList", is seriously flawed.
Synchronizing on the intrinsic lock of a particular instance of URLConnectionReader to protect a static resource is useless.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
vinoth muthuswamy
Greenhorn

Joined: May 24, 2011
Posts: 4
Hi,
Thanks for your suggestion,will i use this keyword within the synchronized block make impact on the code....
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

No, you should synchronize on a common lock while accessing the shared static resource.
In this case a valid candidate would be the intrinsic lock of the resource itself i.e synchronized(resultList) rather than synchronized(this).
Also, you should synchronize for both reads and writes, which your code currently does not do.
vinoth muthuswamy
Greenhorn

Joined: May 24, 2011
Posts: 4
Hi,
actually my need is to hit both URLs at the same time and get the data at the same time as well,that'swhy i am using two threads to hit the urls.Synchronize is for just storing the data in the object one after another,if it has any mistakes will you please modify the code and send me back.

Thanks in advance.....
 
Don't get me started about those stupid light bulbs.
 
subject: will the code running concurrently or not