Thursday, 15 May 2014

multithreading - When the CPU is very busy, my multithreaded java program gets an unexpected Exception -



multithreading - When the CPU is very busy, my multithreaded java program gets an unexpected Exception -

i have been doing multithreaded java programme create cpu usage gradually reach 100%. when cpu usage low, programme running correctly; when cpu near 100%, programme throws nullpointerexception.

i set objects in arraylist. when object token arraylist, there [sometimes] null object, when invoke method on object, throws nullpointerexception.

but have checked program, , no null object ever set in arraylist.

so, guess because of 100% cpu usage. can explain me reason?

writing multi-threaded code (multiple threads access same info structures) hard , requires knowledge , experience goes beyond can provide in answer.

the java memory model defines how memory operates when multiple threads access same data.

that said, description can tell 1 thing can change. not utilize arraylist straight multiple threads. arraylist not protect against so-called info races.

instead, utilize collections.synchronizedlist(...) create list protects internal info structures against info races:

list list = collections.synchronizedlist(new arraylist());

this ensure no longer null object out of list when haven't set null object in it.

however doesn't resolve more complicated info races when multiple threads operate on objects retrieve list.

java multithreading nullpointerexception cpu-usage

No comments:

Post a Comment