java - Main thread hangs because of BlockingQueue's consumer (which I thought was on another thread) -
i have test blocking (first block of code). have few elements working together. have blocking queue put events on to, have consumer takes them off , sends them amazon kinesis. pretty sure test getting blocked because queue blocking consumer though thought running on separate thread.
// test.java @test public void testwhileloop() throws interruptedexception { arrayblockingqueue<event> testq = new arrayblockingqueue<event>(1024); // mockkinesis mock @ class level. kplposter kpl = new kplposter("teststream", mockkinesis, testq); event event = new event("testmessage", "testpartition"); listenablefuture<userrecordresult> fakereturn = mockito.mock(listenablefuture.class); final atomicinteger numberofwhileloops = new atomicinteger(); mockito.doanswer(invocation -> { numberofwhileloops.incrementandget(); return fakereturn; }) .when(mockkinesis) .adduserrecord("teststream", "testpartition", bytebuffer.wrap("testmessage".getbytes())); kpl.run(); // hangs here for(int = 100; > 0; i--){ testq.put(event); } kpl.stop(); kpl = null; assert(numberofwhileloops.tostring()).equals("100"); }
here run method of basekinesisposter kplposter inherits. should noted basekinesisposter implements runnable interface.
//basekinesisposter.java @override public void run() { shutdown = false; while (!shutdown && !(thread.currentthread().isinterrupted())) { try { this.runonce(); } catch (interruptedexception e) { thread.currentthread().interrupt(); }catch (exception e){ e.printstacktrace(); } } }
finally, here part of kplposter's (which extends basekinesisposter) relevant runonce()
method.
// kplposter.java @override protected void runonce() throws exception { event event = inputqueue.take(); //other stuff in method }
how make sure blocking on queue consumer doesn't block test/main thread?
when call
thread.run();
it invokes method called. nothing special happens , method run in current thread.
when call
thread.start();
this starts thread in turn calls run() in new thread.
btw thread.stop()
throw unsupportedoperationexception
in java 8. shouldn't use it. should allow finish naturally.
Comments
Post a Comment