java - Wait notify multithreading producer consumer issue -
i working on basic thread-producer consumer problem.
now in code assuming 1) threads go wait state , moment task comes 1 of tasks notified , process task , again wait threads going runnable states abruptly. understanding correct?
public static void main(string[] args) { asynctaskexecutorimpl executorimpl = new asynctaskexecutorimpl(10, 5); (int = 0; < 200; i++) { runnable task = new createtask(); system.out.println("added task no" + i); executorimpl.execute(task, 10); } } import java.util.concurrent.arrayblockingqueue; public class myarrayblockingqueue<t> { private volatile arrayblockingqueue<runnable> internaltaskqueue = new arrayblockingqueue<runnable>( 10); public boolean isempty() { synchronized (this) { return internaltaskqueue.isempty(); } } public void add(runnable paramrunnable) throws interruptedexception { synchronized (this.internaltaskqueue) { this.internaltaskqueue.put(paramrunnable); this.internaltaskqueue.notifyall(); } (thread t : thread.getallstacktraces().keyset()) { if (t.getname().startswith("t") || t.getname().startswith("m")) { system.out.println(t.getname() + "----" + t.getstate()); } } } public runnable poll() { runnable task = null; try { synchronized (this.internaltaskqueue) { while (this.internaltaskqueue.isempty()) { this.internaltaskqueue.wait(); } task = this.internaltaskqueue.poll(); } } catch (interruptedexception e) { e.printstacktrace(); } return task; } }` import java.util.concurrent.callable; import java.util.concurrent.future; import org.springframework.core.task.asynctaskexecutor; public class asynctaskexecutorimpl implements asynctaskexecutor { private myarrayblockingqueue<runnable> taskqueue= new myarrayblockingqueue<runnable>(); // here creating thread pool of number of threads required public asynctaskexecutorimpl(int no_of_threads, int taskqueuesize) { (int = 0; < no_of_threads; i++) { individualthread thread = new individualthread(this.taskqueue); thread.start(); } (thread t : thread.getallstacktraces().keyset()) { if (t.getname().startswith("t") || t.getname().startswith("m")) { system.out.println(t.getname() + "----" + t.getstate()); } } } @override public void execute(runnable paramrunnable, long paramlong) { if (paramrunnable instanceof runnable) { // pick thread threadpool , execute try { this.taskqueue.add(paramrunnable); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } }` class createtask implements runnable { @override public void run() { system.out.println(thread.currentthread().getname() + "got task"); }
notifyall "wake up" waiting threads. should logically ok because compete 'sychronized' block , 1st thread gets access find 'not empty', pull data, exit 'synchornized' block.
other thread synchronized block, see 'empty' & waiting (unless there several 'add' actions, of course, in case several threads see 'not empty'). in other words, if spin lock correctly designed, threads become runnable brief fraction of second.
there's 'object.notify' wake 1 thread, afaik it's considered unsafe spin-locks such yours.
Comments
Post a Comment