Calling a method from one thread to another to execute in the other thread

I'm making a server in java that has a MySQL connection in another thread, so that it's event driven and won't block the main thread.

So far it works well, it works great for queries that don't return (insert, etc) but I'm not sure how to implement a callback method that will run in the other thread. If I simply call a method from the database connection thread, that method is run in that thread.

What would be the best way to do this?


There's java.util.concurrent.FutureTask.

If not, perhaps your DB thread could implement a command/query pool and for each command/query be able to call a given object's callback method using reflection. This will allow non-blocking command queueing and notification on command/query completion.

Need Your Help

Inputstream readUTF can't read UTF

java sockets nio eofexception

I am a total noob at Java, so apologies if this is a rookie mistake.

Better solution to case class inheritance deprecation?

scala pattern-matching

I am working through the (admittedly somewhat old) Programming Scala [Subramaniam, 2009] and encountered a troubling compiler warning when working through section 9.7 "Matching using case classes" ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.