• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

writing parall into a common array by two threads

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have certain values in a arry. Now while trying to apply multi threading i have to update array. Should I divide the arry to multiple sub array or could write safely to array. for example thrad 1 would write to arr[0] and thread 2 would write to arr[1]?

Thanks in advance
Rudra
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could put the code that accesses the array in a synchronized block or method. Alternative is to used some synchronized collection, souch as CopyOnWriteArrayList. I would preffer a synchronized block.
 
rudra tripathy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks martin..My intention is that since the updataion would be index wise and there would be overlap of data, hence data inconsistency is not there.Only doubt I have whether it would throw any exception.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure whether data overriding by multiple threads over a specific index is alright for you or not but as far as exceptions are concerned there will not be any if we are not going beyong he size of the array.

Gajendra,
SCJP,SCWCD,ITIL
 
rudra tripathy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gajendra for clarifying the doubt. Yeah, each index has some other meaning for me. If Iam not getting excpetion or deadlock while updating 2 indeces by 2 threads, its fine for me.

Thanks again
Rudra
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic