Asyncronous task queue

I have one thread that delegates tasks over to another thread, which runs those tasks in a queue. These tasks are blocking tasks (database), that I want to be run in the correct order, first in first out. Currently using BlockingQueue, but when testing it using junit, it can freeze the whole test. That's because after each test, this thread is waited on until it finishes. I have not found a good way to enable/disable the queue How would one do this properly?
32 Replies
JavaBot
JavaBot9h ago
This post has been reserved for your question.
Hey @Thorinwasher! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st9h ago
Can you show the relevant code? Just the code that's necessary to reproduce the issue
Thorinwasher
Thorinwasher9h ago
Sure, don't laugh at it though 🤣
dan1st
dan1st9h ago
And if possible, please also show where exactly the code blocks the tests - Is it somewhere during the test or after running the tests?
Thorinwasher
Thorinwasher9h ago
Oh, too large...
dan1st
dan1st9h ago
I am fairly sure your code is better than the code of many other people - after all you are writing tests you can also try to upload the files
Thorinwasher
Thorinwasher9h ago
Good point, I'm not educated enough in concurency though Ah
dan1st
dan1st9h ago
or ideally only include the relevant code
Thorinwasher
Thorinwasher9h ago
let me do that then
dan1st
dan1st9h ago
concurrency is hard
dan1st
dan1st9h ago
The most complicated chapter of the JVM specification (which specifies how JVMs have to work) is pretty much about what should happen in multithreaded environments
Thorinwasher
Thorinwasher9h ago
I think you need more info than that though
dan1st
dan1st8h ago
Can you show the test and where it blocks?
Thorinwasher
Thorinwasher8h ago
Sure, this is one test that blocks everything, but that relates to the test that ran before it It's quite layered though
@Test
void portalInvalidBlockPlaceTest() {
Location bottomLeft = new Location(world, 0, 7, 0);
Location insidePortal = new Location(world, 0, 9, 0);
Block signBlock = PortalBlockGenerator.generatePortal(bottomLeft);
Block irisBlock = new Location(world, 1, 9, 0).getBlock();


((Directional) signBlock.getBlockData()).setFacing(BlockFace.SOUTH);
blockEventListener
.onSignChange(new SignChangeEvent(signBlock, player, new String[]{"test", "", CUSTOM_NETNAME, ""}));

BlockPlaceEvent event = new BlockPlaceEvent(irisBlock, irisBlock.getState(), irisBlock,
new ItemStack(Material.ANDESITE), player, false);
blockEventListener.onBlockPlace(event);
blockEventListener.onBlockBreak(new BlockBreakEvent(insidePortal.getBlock(), player));
Assertions.assertTrue(event.isCancelled());
}
@Test
void portalInvalidBlockPlaceTest() {
Location bottomLeft = new Location(world, 0, 7, 0);
Location insidePortal = new Location(world, 0, 9, 0);
Block signBlock = PortalBlockGenerator.generatePortal(bottomLeft);
Block irisBlock = new Location(world, 1, 9, 0).getBlock();


((Directional) signBlock.getBlockData()).setFacing(BlockFace.SOUTH);
blockEventListener
.onSignChange(new SignChangeEvent(signBlock, player, new String[]{"test", "", CUSTOM_NETNAME, ""}));

BlockPlaceEvent event = new BlockPlaceEvent(irisBlock, irisBlock.getState(), irisBlock,
new ItemStack(Material.ANDESITE), player, false);
blockEventListener.onBlockPlace(event);
blockEventListener.onBlockBreak(new BlockBreakEvent(insidePortal.getBlock(), player));
Assertions.assertTrue(event.isCancelled());
}
Want results from more Discord servers?
Add your server