File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

will the code running concurrently or not

 
vinoth muthuswamy
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vinoth muthuswamy
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your suggestion,will i use this keyword within the synchronized block make impact on the code....
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic