Out of memory error or program unresponsive during multithreading JAVA -
i new multi threading. literally. , pretty new java overall. however, making simulation , thread seem working except have program become unresponsive , once said out of memory error. assume because creating many threads? @ point have on 2000 threads running @ same time. can adjust code (i.e reduce number of customers or increase time between ticks) release pressure on system wondering if there doing ease burden within each thread itself. i'm pretty sure isn't deadlock since code runs fine if above run longer same amount of customers come through. show relevant parts of code , if more required happy put up. please don't understand well, simple , helpful explanations appreciated, since if comment complex wont' know how implement it.
this main class creates thread.
checkoutfloor checkoutfloor = new checkoutfloor(); checkoutfloor.addcheckoutoperators(checkoutoperators); int tick = 0; int customers = 0; while (tick < simulationtime) { (int = 0; < 2; i++) { random rand = new random(); int random = rand.nextint(100) + 1; if (random <= 50) { customerrunnable customer = new customerrunnable(checkoutfloor); thread t = new thread(customer); t.start(); tick++; } else { tick++; }
here runnable code:
public void run() { try { customer.addrandomshoptime(); while (customer.shoptime > 0) { thread.sleep(1); customer.setshoptime(); } checkoutoperator checkoutoperator = checkoutfloor.weightedcheckoutdeterminator(); checkoutoperator.addcustomer(customer); while (customer.checkouttime > 0) { thread.sleep(1); if (checkoutoperator.customerlist.get(0) == customer) { customer.setcheckouttime(); } } checkoutoperator.removecurrentcustomer(); } catch (interruptedexception e) { }
and here relevant methods.
public synchronized void addcustomer(customer newcustomer) { customerlist.add(newcustomer); system.out.println("no. of customers" + counter); counter++; } public synchronized checkoutoperator weightedcheckoutdeterminator() { int totalcustomers = 0; int totalcustomersadj = 0; (int = 0; < checkoutoplist.size(); i++) { totalcustomers += getcheckoutoperator(i).getcustomerlistlength(); } if (totalcustomers == 0) { return getcheckoutoperator(0); } else { totalcustomersadj = (checkoutoplist.size() * totalcustomers) - totalcustomers; int randomnumber = 0; try { randomnumber = new random().nextint(totalcustomersadj) + 1; } catch (illegalargumentexception ex) { return checkoutoplist.get(0); } (int = 0; < checkoutoplist.size(); i++) { int operatorcustlength = (getcheckoutoperator(i).getcustomerlistlength()); if (randomnumber <= (totalcustomers - operatorcustlength)) { return getcheckoutoperator(i); } else { randomnumber = randomnumber - (totalcustomers - operatorcustlength); } } return checkoutoplist.get(0); //hopefully unreachable!!! }
i'm wondering if, due lack of knowledge, there missing woudl flow bit more , run faster?
[edit: should add under normal conditions never have 2000 threads running , me pushing system max. suspect problem , guess asking if there else missing due lack of knowledge. don't idea if changes code make more intensive might crash.].
Comments
Post a Comment