Asynchronous tasks
#1
It is possible to run things asynchronly (in a other thread) and go back to synchronous (/ callbacks)?
I need this because I want to do database requests and one requests took 0.2 - 1.2ms.

My implementation in java:
package tv.rewinside.database.bukkit.util;

import com.google.common.collect.Queues;
import java.util.Queue;
import java.util.logging.Level;
import tv.rewinside.database.bukkit.DatabasePlugin;

public class DatabaseQueue extends Thread {
	private final Queue<Runnable> runnables = Queues.newConcurrentLinkedQueue();

	public DatabaseQueue() {
		super("Database Queue");
		this.setDaemon(true);
	}

	public synchronized void addRunnable(Runnable runnable) {
		this.runnables.add(runnable);
		this.notify();
	}

	private synchronized void blockCurrentThread() {
		try {
			while (this.runnables.isEmpty()) {
				this.wait();
			}
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

	public int getCount() {
		return this.runnables.size();
	}

	@Override
	public void run() {
		while (!this.isInterrupted()) {
			this.blockCurrentThread();

			while (!this.runnables.isEmpty()) {
				try {
					this.runnables.poll().run();
				} catch (Exception ex) {
					DatabasePlugin.getInstance().getLogger().log(Level.WARNING, "Exception while execute database task", ex);
				}
			}
		}
	}

}

public interface DatabaseCallback<T> {

	public void run(T result);

}

And a example use of the queue:
public void getPlayer(final String uuid, final DatabaseCallback<DatabasePlayer> callback) {
		DatabasePlayer dbPlayer = this.cachedPlayers.get(uuid);
		if (dbPlayer != null) {
			callback.run(dbPlayer);
			return;
		}

		// Load player -> Async
		this.plugin.getQueue().addRunnable(new Runnable() {
			@Override
			public void run() {
				DatabaseCache.this.syncCallbackCall(callback, DatabaseCache.this.loadPlayer(uuid));
			}
		});
	}

private DatabasePlayer loadPlayer(String uuid) {
		DatabasePlayer dbPlayer = this.plugin.getConnection().getPlayer(uuid);
		if (dbPlayer == null) return null;

		this.cachedPlayers.put(uuid, dbPlayer);
		this.cachedPlayersFromName.put(dbPlayer.getLastName(), dbPlayer);
		return dbPlayer;
	}

private <T> void syncCallbackCall(final DatabaseCallback<T> callback, final T arg) {
		if (Bukkit.isPrimaryThread()) {
			callback.run(arg);
			return;
		}

		Bukkit.getScheduler().runTask(this.plugin, new Runnable() {
			@Override
			public void run() {
				callback.run(arg);
			}
		});
	}
Reply
Thanks given by:


Messages In This Thread
Asynchronous tasks - by Howaner - 03-11-2015, 05:58 AM
RE: Asynchronous tasks - by NiLSPACE - 03-11-2015, 06:12 AM
RE: Asynchronous tasks - by worktycho - 03-11-2015, 06:16 AM
RE: Asynchronous tasks - by NiLSPACE - 04-12-2015, 12:39 AM
RE: Asynchronous tasks - by xoft - 04-12-2015, 01:31 AM
RE: Asynchronous tasks - by NiLSPACE - 04-12-2015, 09:54 PM
RE: Asynchronous tasks - by xoft - 04-13-2015, 12:50 AM
RE: Asynchronous tasks - by xoft - 04-13-2015, 12:54 AM
RE: Asynchronous tasks - by NiLSPACE - 04-13-2015, 01:29 AM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 09:49 AM
RE: Asynchronous tasks - by NiLSPACE - 04-15-2015, 05:46 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 07:45 PM
RE: Asynchronous tasks - by xoft - 04-15-2015, 08:06 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 08:24 PM
RE: Asynchronous tasks - by NiLSPACE - 04-15-2015, 08:26 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 08:34 PM
RE: Asynchronous tasks - by NiLSPACE - 04-15-2015, 09:13 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 09:16 PM
RE: Asynchronous tasks - by NiLSPACE - 04-15-2015, 09:24 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 09:36 PM
RE: Asynchronous tasks - by xoft - 04-15-2015, 09:48 PM
RE: Asynchronous tasks - by worktycho - 04-15-2015, 09:59 PM
RE: Asynchronous tasks - by NiLSPACE - 09-28-2015, 08:10 PM
RE: Asynchronous tasks - by xoft - 09-28-2015, 09:53 PM
RE: Asynchronous tasks - by NiLSPACE - 09-28-2015, 10:00 PM
RE: Asynchronous tasks - by worktycho - 09-28-2015, 10:09 PM



Users browsing this thread: 1 Guest(s)