Running a thread inside a thread in Lucee 4


I had the need to run a cfthread from inside another cfthread in Lucee recently. @micstriit mentioned that this was already possible in Lucee 5.1, however my project was using Lucee 4.5 and unable to upgrade at the moment.

It is fairly easy to fire off a Java thread by creating a CFC with a “void run()” method and using createDynamicProxy() to create it as an instance of “java.lang.Runnable”. Then it can be passed into a java.lang.Thread() constructor and started with “start()”. This works well, but the thread runs outside of the page context and doesn’t get access to settings like CF mappings and the “thread” scope which makes it difficult to use.

In a Slack conversation with Micha he said,

when you create a new thread and you wanna run CFML code in that thread, you need to create a PageContext for that thread and register that PageContext with that thread and you also need to register the ConfigWeb object with that thread.

I don’t know what it takes “register” a package context or a configWeb object with a thread, so I’m creating this post here so he can hopefully explain that and the answer will be documented for others.


Are you sure this works? I tried it and I get:

ce 	lucee.runtime.exp.NativeException: class java.lang.Runnable is invalid or doesn't exist
 	at lucee.commons.lang.PhysicalClassLoader.findClass(
 	at lucee.commons.lang.PhysicalClassLoader.loadClass(
 	at lucee.commons.lang.PhysicalClassLoader.loadClass(
 	at java.lang.ClassLoader.defineClass1(Native Method)
 	at java.lang.ClassLoader.defineClass(
 	at java.lang.ClassLoader.defineClass(
 	at lucee.commons.lang.PhysicalClassLoader._loadClass(
 	at lucee.commons.lang.PhysicalClassLoader.loadClass(
 	at lucee.transformer.bytecode.util.JavaProxyFactory.createProxy(
 	at lucee.runtime.functions.other.CreateDynamicProxy._call(
 	at java_executor_test_cfm855$
 	at lucee.runtime.PageContextImpl._doInclude(
 	at lucee.runtime.PageContextImpl.doInclude(
 	at lucee.runtime.PageContextImpl.doInclude(
 	at lucee.runtime.PageContextImpl.doInclude(
 	at application_cfc$cf.udfCall(/Application.cfc:6)
 	at lucee.runtime.type.UDFImpl.implementation(
 	at lucee.runtime.type.UDFImpl._call(
 	at lucee.runtime.ComponentImpl._call(
 	at lucee.runtime.ComponentImpl._call(
 	at lucee.runtime.listener.ModernAppListener._onRequest(
 	at lucee.runtime.listener.MixedAppListener.onRequest(
 	at lucee.runtime.PageContextImpl.execute(
 	at lucee.runtime.PageContextImpl._execute(
 	at lucee.runtime.PageContextImpl.executeCFML(
 	at lucee.runtime.engine.CFMLEngineImpl.serviceCFML(
 	at lucee.loader.engine.CFMLEngineWrapper.serviceCFML(
 	at lucee.loader.servlet.CFMLServlet.service(
 	at javax.servlet.http.HttpServlet.service(
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
 	at org.apache.tomcat.websocket.server.WsFilter.doFilter(
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
 	at org.apache.catalina.core.StandardWrapperValve.invoke(
 	at org.apache.catalina.core.StandardContextValve.invoke(
 	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
 	at org.apache.catalina.core.StandardHostValve.invoke(
 	at org.apache.catalina.valves.ErrorReportValve.invoke(
 	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
 	at org.apache.catalina.core.StandardEngineValve.invoke(
 	at org.apache.catalina.connector.CoyoteAdapter.service(
 	at org.apache.coyote.http11.AbstractHttp11Processor.process(
 	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
 	at java.util.concurrent.ThreadPoolExecutor.runWorker(
 	at java.util.concurrent.ThreadPoolExecutor$
 	at org.apache.tomcat.util.threads.TaskThread$


Hi @kliakos sorry for the lack of example in my first post. Here is the code to try this out on your own.

Create a component called myRunnable.cfc with the following code

component {
	function run() {
		systemOutput( 'Starting thread.', true );
		sleep( 2000 );
		systemOutput( 'Stopping thread.', true );

Then run this code from another file:

myRunnable = createDynamicProxy( new myRunnable(), 'java.lang.Runnable' );
myThread = createObject( 'java', 'java.lang.Thread' ).init( myRunnable );

You’ll see this in your console log:

Starting thread.
Stopping thread.


Do you have this.javaSettings in your Application.cfc?

I figured out my problem was this ( actually the lack of it ). Found the solution from here


Nope, no javasettings defined in my Application.cfc. What version of Lucee are you running this on? I’m on 4.5


I am on 5.1