#ExecutorService cant run submitted task for some server

1 messages · Page 1 of 1 (latest)

opaque marsh
#

So i have this code:

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.submit(() -> System.out.println("test"));

I submitted a runnable and it run on my PC, but when i run it on a server, it didn't, it run actually but with 50 seconds of delay which is something that i don't expect to happen. Any idea why won't the ExecutorService run the submitted task directly?

toxic briar
#

i've never tried using ScheduledExecutorService, but bukkit/spigot provide a BukkitScheduler, would you be able to use it instead? it works as expected every time i've used it

opaque marsh
#

i wanted to but the issue about it is the racing condition,

daring tree
#

What are you trying to do?

opaque marsh
#

i want to do async in one single thread

daring tree
#

If you’re trying to emulate a race condition then that’s the wrong way

#

You’d use a semaphore

opaque marsh
#

i don't understand

daring tree
#

If it’s a normal thread executor

#

Use the Executors::newSingleThreadedExecutor

#

Usually we don’t use Scheduler executor services to run tasks

opaque marsh
#

i needed it to be a scheduled tho cause i also use the service to schedule repeating task

daring tree
#

You usually delegate it to a cached thread pool

#

You don’t need it

#

You should have 1 scheduled executor service which schedules a task to be executed on another executor service

opaque marsh
#

wouldn't it stack the task if the main executor is busy and the scheduled keep submitting tasks and not waiting till the main executor ready?

daring tree
#

wut

#

Which one is the main executor here

#

And no not really

#

I mean sure it uses a synchronous queue for the tasks

#

However

#

There’s more than one thread picking of said queue

#

If it’s a cached thread pool

#

Then it’ll in principle consume the queue rather immediately

opaque marsh
#

but the cached thread pool won't synchronize the running tasks r8? cause thats what bukkit async task executor use

daring tree
#

Yes

#

So in that case use a single threaded executor service

#

It can be a daemon thread

#

And ofc if u use a single threaded one the tasks are gonna get blocked meaning

#

Only one task at a time will be executing as code runs linearly in response to a thread

opaque marsh
#

its ok, i'm doing db actions on that thread, and i wanted it to be synchronized

daring tree
#

I mean

#

You could use a locking implementation

#

Altho for the time being a single threaded executor service might be simpler

#

Anyways I suggest taking a look at ReentrantLock, ReentrantReadWriteLock and StampedLock as well as synchronized keyword

opaque marsh
#

would you recommend to use daemon thread for task such as modifying the database?

daring tree
#

Well I meant daemon executor service

#

And yeah