running program, multithreaded, seems lead thread interference though used keyword synchronized methods have effect on part problem lies.
i have static nested class station has following methods in it:
public synchronized boolean addqueuer(string id) { if (!buffer.isfull()) { buffer.enqueue(id); return true; } return false; } private synchronized void removequeuer() { buffer.dequeue(); }
buffer simple queue wrapping.
in 2 different threads, call these methods. in encapsulator, main method has following lines of code:
while (true) { stations[0].addqueuer("m"); thread.sleep(freq_timer); }
where stations array of station containg single station in test
and in station have following run() function implementation of runnable (i start thread creating instance of class, call start() thread):
public void run() { try { thread.sleep(estimated_service_time); while (!buffer.isempty()) { removequeuer(); } if (isopen) run(); } catch (interruptedexception interruptexception) { threadmessage("this station interrupted."); } }
what going wrong:
i have adjusted constants buffer should fill quicker gets emptied. however, when run program buffer might start @ 7, go down 6 @ next run in next loop, stay @ 6 forever.
first, not possible 2 invocations of synchronized methods on same object interleave. when 1 thread executing synchronized method object, other threads invoke synchronized methods same object block (suspend execution) until first thread done object. - java tutorials
why not enough put synchronize? if additional context/information required, please comment , i'll try abide.
edit: added the entire code.