Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

writing parall into a common array by two threads  RSS feed

 
rudra tripathy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Gajendra Tomer
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!