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

will the code running concurrently or not

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic