Skip to content

Commit

Permalink
Updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed May 3, 2024
1 parent 86ba118 commit 061be04
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Call class="org.slf4j.LoggerFactory" name="getLogger">
<Arg>org.eclipse.jetty</Arg>
<Call name="info">
<Arg>All Virtual threads are enabled.</Arg>
<Arg>Virtual threads enabled. Using all virtual threads.</Arg>
</Call>
</Call>
</Configure>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.eclipse.jetty.util.component.Dumpable;

@ManagedObject("Tracking Executor wrapper")
class TrackingExecutor implements Executor, Dumpable
public class TrackingExecutor implements Executor, Dumpable
{
private final Executor _threadFactoryExecutor;
private final Set<Thread> _threads = ConcurrentHashMap.newKeySet();
Expand All @@ -40,14 +40,15 @@ public void execute(Runnable task)
{
_threadFactoryExecutor.execute(() ->
{
Thread thread = Thread.currentThread();
try
{
_threads.add(Thread.currentThread());
_threads.add(thread);
task.run();
}
finally
{
_threads.remove(Thread.currentThread());
_threads.remove(thread);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool,
private String _name = null;
private Executor _virtualExecutor;
private Thread _main;
private boolean _externalExecutor;
private boolean _tracking;
private boolean _detailedDump;

Expand Down Expand Up @@ -98,7 +99,7 @@ public void setDetailedDump(boolean detailedDump)
@Override
protected void doStart() throws Exception
{
_main = new Thread("virtual main")
_main = new Thread("keepalive")
{
@Override
public void run()
Expand All @@ -118,10 +119,14 @@ public void run()
};
_main.start();

_virtualExecutor = Objects.requireNonNull(StringUtil.isBlank(_name)
? VirtualThreads.getDefaultVirtualThreadsExecutor()
: VirtualThreads.getNamedVirtualThreadsExecutor(_name));
if (_tracking)
if (_virtualExecutor == null)
{
_externalExecutor = false;
_virtualExecutor = Objects.requireNonNull(StringUtil.isBlank(_name)
? VirtualThreads.getDefaultVirtualThreadsExecutor()
: VirtualThreads.getNamedVirtualThreadsExecutor(_name));
}
if (_tracking && !(_virtualExecutor instanceof TrackingExecutor))
_virtualExecutor = new TrackingExecutor(_virtualExecutor, _detailedDump);
addBean(_virtualExecutor);
super.doStart();
Expand All @@ -131,7 +136,9 @@ public void run()
protected void doStop() throws Exception
{
super.doStop();
_virtualExecutor = null;
removeBean(_virtualExecutor);
if (!_externalExecutor)
_virtualExecutor = null;
_main = null;

try (AutoLock.WithCondition l = _joinLock.lock())
Expand All @@ -149,7 +156,10 @@ public Executor getVirtualThreadsExecutor()
@Override
public void setVirtualThreadsExecutor(Executor executor)
{
throw new UnsupportedOperationException("cannot set VirtualThreadExecutor");
if (isRunning())
throw new IllegalStateException(getState());
_externalExecutor = executor != null;
_virtualExecutor = executor;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,15 @@ public void run()
try
{
_running.countDown();

while (_spin && getCount() > 0)
Thread.onSpinWait();

if (!await(10, TimeUnit.SECONDS))
throw new IllegalStateException();

}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
finally
{
System.err.println("RAN!" + Thread.currentThread());
}
}
}
}

0 comments on commit 061be04

Please sign in to comment.