• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Multithreading Dilemna

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below code throw outofBound Exception on arraylist if executed continuously(immediately)2-3 times.
I am aware that this exception can be avoided by synchronizing alterlist() methods or synchronized bocks using different objectslock1 and lock2 I have created. I want to understand why this exception is thrown. Arraylist auto increase its size when needed.

Can anyone explain what is happening behind the scene







Exception:

Starting...
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 25
at java.util.ArrayList.add(ArrayList.java:352)
at com.suri.threads.MultipleLocks.alterList2(MultipleLocks.java:75)
at com.suri.threads.MultipleLocks.process(MultipleLocks.java:56)
at com.suri.threads.MultipleLocks$2.run(MultipleLocks.java:36)
at java.lang.Thread.run(Thread.java:662)
Time taken : 2067
List 1 size : 1013 List 2 Size : 1013
 
author
Posts: 23892
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The ranch office forum is used for discussions regarding the ranch itself ... let's move this topic to a more appropriate forum.

Henry
 
Henry Wong
author
Posts: 23892
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surinder Mehra wrote:
Can anyone explain what is happening behind the scene



The behavior of using classes in a non-thread-safe manner is completely undefined, so, I would argue that it is probably better to just *not* do that, than to figure out how broken code is behaving. Regardless...

Surinder Mehra wrote:
I am aware that this exception can be avoided by synchronizing alterlist() methods or synchronized bocks using different objectslock1 and lock2 I have created. I want to understand why this exception is thrown. Arraylist auto increase its size when needed.



A quick look at the code shows that the add() method checks if there is enough room, increases it if there isn't, and then add the element. So, if there is room for one more element, we can speculate that the two threads checks and both confirm that there is enough room for its one element. The first thread then adds and complete successfully, and the second fails.

In my opinion, I am surprised you didn't get something more complicated. It could be possible to corrupt data structures, and you could wind up with all sorts of other errors.

Henry
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic